【问题标题】:FPDF+FPDI autoprintFPDF+FPDI 自动打印
【发布时间】:2018-07-05 22:04:33
【问题描述】:

我需要将import pdf 归档到fpdf 和print it silently。我使用 fpdi 扩展来加载现有的 pdf,但我不知道如何自动打印它。

这就是 fpdf 中自动打印的工作原理 - 围绕 fpdf 的 2 个附加类(来自他们的示例)。

require('lib/fpdf/fpdf.php');
require('lib/fpdi-1.6.1/fpdi.php');


class PDF_JavaScript extends FPDF {

    protected $javascript;
    protected $n_js;

    function IncludeJS($script, $isUTF8=false) {
        if(!$isUTF8)
            $script=utf8_encode($script);
        $this->javascript=$script;
    }

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

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

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



class PDF_AutoPrint extends PDF_JavaScript
{
    function AutoPrint($printer='')
    {
        // Open the print dialog
        if($printer)
        {
            $printer = str_replace('\\', '\\\\', $printer);
            $script = "var pp = getPrintParams();";
            $script .= "pp.interactive = pp.constants.interactionLevel.full;";
            $script .= "pp.printerName = '$printer'";
            $script .= "print(pp);";
        }
        else
            $script = 'print(true);';
        $this->IncludeJS($script);
    }
}



$pdf = new PDF_AutoPrint();

$pdf->AddPage();
$pdf->SetFont('Arial', '', 20);
$pdf->Text(90, 50, 'Print me!');
$pdf->AutoPrint();
$pdf->Output();

它工作正常,但我需要它与此结合:

require('lib/fpdf/fpdf.php');
//require('lib/fpdi-2.0.1/src/autoload.php');
require('lib/fpdi-1.6.1/fpdi.php');

$pdf = new FPDI();
$pdf->setSourceFile("BCEB_20171207_100_A_1_51.pdf");

$tplIdx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 

$pdf->Output();

我真的不知道如何将两者结合起来(使用 autoload 类 aroud fpdi?)。有帮助的替代方案 - 将准备好的 pdf 加载到 fpdf 并输出的另一种方法。

【问题讨论】:

    标签: php pdf fpdf fpdi


    【解决方案1】:

    基于Print PDF in Firefox

    <?php
    
    
    require('lib/fpdf/fpdf.php');
    require('lib/fpdi-1.6.1/fpdi.php');
    
    
    class PDF_JavaScript extends FPDI {
    
        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>>');
            }
        }
    }
    
    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)
        {
            $script = "document.contentWindow.print();";
            $this->IncludeJS($script);
        }
    }
    
    $pdf=new PDF_AutoPrint();
    $pageCount = $pdf->setSourceFile("BCEB_20171207_28_A_1_04_long.pdf");
    // $pageCount = $pdf -> setSourceFile("BCEB_20171207_100_A_1_51.pdf");
    //Open the print dialog
    //$tplIdx = $pdf->importPage(1, '/MediaBox'); //this you need to do on every page
    
    for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
        $tplIdx = $pdf->importPage($pageNo);        
        $pdf->addPage();
        $pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 
    }
    
    $pdf->AutoPrint(true);
    $pdf->Output();
    ?>
    

    【讨论】:

    • 这将创建一个包含输入文件页数的 PDF,但只有第一页会放在所有页面上 - 我猜是一个简单的错误。几乎是一个正确的答案,但请不要使用旧版本!请使用所有相关课程的最新版本。使用的FPDI版本是2015年的,请将代码更新为FPDI 2!
    • 你是对的!我没有注意到在星期五结束时所有页面都是相同的:P 要解决这个问题,您需要在 for 的开头添加 $tplIdx = $pdf-&gt;importPage($pageNo);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多