最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - FPDF - Printing without the dialog box while using PDF_JS - Stack Overflow

programmeradmin2浏览0评论

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 ments 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();
?>

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 ments 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();
?>
Share Improve this question edited Aug 15, 2016 at 19:16 Nick Peranzi 1,3751 gold badge9 silver badges24 bronze badges asked Aug 15, 2016 at 16:03 Marco LRamMarco LRam 611 gold badge1 silver badge7 bronze badges 2
  • Wele to StackOverflow. Please post the exact code you are currently using in order to help users better understand your issue. – Nick Peranzi Commented Aug 15, 2016 at 16:23
  • First of all, thank you for replying my post. I'll edit my post attaching the code i'm using. – Marco LRam Commented Aug 15, 2016 at 16:47
Add a ment  | 

3 Answers 3

Reset to default 1

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();

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 acplish 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.

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 mas, 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.

发布评论

评论列表(0)

  1. 暂无评论