FPDF - 使用PDF_JS时不使用对话框进行打印

时间:2016-08-15 16:03:58

标签: javascript php pdf fpdf

正如标题所说。在这里搜索,我找到了FPDF的“扩展”,允许打印文档。现在,我需要直接打印(即没有对话框)。我正在关注作者在this page中留下的评论但不适合我:(。我也尝试过复制和粘贴,但无法找到解决方案。

注意:我使用过FireFox(最新版本)并且该过程根本不起作用。我也尝试过使用Chrome和Yandex浏览器;两者都有效,但仍显示对话框。

PS:谢谢你的时间!

我正在使用的代码如下。

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

1 个答案:

答案 0 :(得分:0)

感谢您提前回复。对于迟到的回应感到抱歉。可悲的是,我做了半工作&#34;。我调查了一个&#34; lil&#34;并发现我所要做的就是启用&#34; kiosk-printing&#34;模式:1。使用Chrome创建桌面快捷方式,然后转到属性并查找路径,最后在逗号后面放置--kiosk-printing

例如:

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

我提供的代码都没问题。

感谢您的时间。如果您有更好的方法来处理这个问题。评价。