Marco LRam
Marco LRam

Reputation: 61

FPDF - Printing without the dialog box while using PDF_JS

Just as the title says. Searching in here, I found an "extension" to FPDF that allows to print the documents. Now, I need to print directly (that is, without the dialog box). I am following the comments the author is leaving in this page but is not working for me :(. I have also tried copying and pasting but could not arrive at a solution.

NOTE: I've used FireFox (latest version) and the process did not work at all. I also tried using Chrome and Yandex browsers; both worked but still showed the dialog box.

PS: thanks for your time!

The code I am using is below.

pdf_js.php

require('fpdf.php');

class PDF_JavaScript extends FPDF {

    var $javascript;
    var $n_js;

    function IncludeJS($script) {
        $this->javascript=$script;
    }

    function _putjavascript() {
        $this->_newobj();
        $this->n_js=$this->n;
        $this->_out('<<');
        $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
        $this->_out('>>');
        $this->_out('endobj');
        $this->_newobj();
        $this->_out('<<');
        $this->_out('/S /JavaScript');
        $this->_out('/JS '.$this->_textstring($this->javascript));
        $this->_out('>>');
        $this->_out('endobj');
    }

    function _putresources() {
        parent::_putresources();
        if (!empty($this->javascript)) {
            $this->_putjavascript();
        }
    }

    function _putcatalog() {
        parent::_putcatalog();
        if (!empty($this->javascript)) {
            $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
        }
    }
}

ex.php

<?php
require('pdf_js.php');

class PDF_AutoPrint extends PDF_JavaScript
{
function AutoPrint($dialog=false)
{
    //Open the print dialog or start printing immediately on the standard printer
    $param=($dialog ? 'true' : 'false');
    $script="print($param);";
    $this->IncludeJS($script);
}

function AutoPrintToPrinter($server, $printer, $dialog=false)
{
    //Print on a shared printer (requires at least Acrobat 6)
    $script = "var pp = getPrintParams();";
    if($dialog)
        $script .= "pp.interactive = pp.constants.interactionLevel.full;";
    else
        $script .= "pp.interactive = pp.constants.interactionLevel.automatic;";
    $script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
    $script .= "print(pp);";
    $this->IncludeJS($script);
}
}

$pdf=new PDF_AutoPrint();
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Text(90, 50, 'Print me!');
//Open the print dialog
$pdf->AutoPrint(true);
$pdf->Output();
?>

Upvotes: 3

Views: 10806

Answers (3)

Marco LRam
Marco LRam

Reputation: 61

Thanks for the early reply. And sorry for the late response. Sadly, i made "half-worked". I've investigated a "lil" and found that all i had to do was to enable the "kiosk-printing" mode: 1. Create a desktop shortcut with Chrome, then go to the properties and look for the path, then at the end, after the commas, put --kiosk-printing

For example:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --kiosk-printing

And the code i provide was all OK.

Thanks for your time. If you have a better way to deal with this issue. Comment.

Upvotes: 0

SergioCS
SergioCS

Reputation: 58

Generally speaking, if what you want is to have your script bypass the browser's printer dialog and instead start printing immediately on whatever default printer has been set, then the answer is NO, you cannot bypass the browser's dialog as it would be a security issue, imagine malware websites sending documents to your printer without you even noticing. There are other non orthodox ways to accomplish this though, by creating a browser plugin or a windows/linux app that the user would need to download and install and run so that whenever your site requires it this intermediary would print for you. So, no matter what you put in your php code you won't skip the dialog.

Upvotes: 0

Nick Peranzi
Nick Peranzi

Reputation: 1375

You are setting the "dialog" variable to true. You need to set it to false.

The AutoPrint function accepts a "dialog" variable AutoPrint($dialog=false) which determines whether to show the print dialog.

In your code, you pass true for dialog. Change the last three lines of ex.php to the below:

//Do not open the print dialog
$pdf->AutoPrint(false);
$pdf->Output();

Upvotes: 1

Related Questions