在Firefox中打印PDF

cla*_*rkk 46 javascript printing firefox jquery pdf-generation

如何在Firefox中打印PDF?

此功能适用于Chrome,但不适用于Firefox

function print_pdf(url){
    var id = 'iframe', html = '<iframe id="'+id+'" src="'+url+'" style="display:none"></iframe>';
    $('#main').append(html);
    $('#'+id).load(function(){
        document.getElementById(id).contentWindow.print();
    }
}
Run Code Online (Sandbox Code Playgroud)

错误

Error: Permission denied to access property "print"
Run Code Online (Sandbox Code Playgroud)

osh*_*ell 34

Firefox:拒绝访问属性"打印"的权限

这是firefox中的一个错误.在本地,可以通过转到about:config并将属性设置pdfjs.disabled为true 来禁用它.唯一可行的解​​决方法是使用服务器端脚本并修改pdf.使用PHP,你可以使用FPDF和嵌入扩展来实现JS(inclunding的print()功能),或者简单的PDF转换为图像,返回URL并打印.您可以使用FPDI修改现有的pdf.我将举例说明如何使用PHP.

使用FPDIPDF_JS生成带有内联javascript(autoprint)的PDF文件

require_once('fpdf.php');
require_once('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();
$pdf->setSourceFile("mozilla.pdf");
//Open the print dialog
$tplIdx = $pdf->importPage(1, '/MediaBox');
$pdf->addPage();
$pdf->useTemplate($tplIdx, 10, 10, 90);
$pdf->AutoPrint(true);
$pdf->Output('generated.pdf', 'F');
Run Code Online (Sandbox Code Playgroud)

现在您可以简单地将生成的pdf附加到您的页面,并且包含的​​javascript将调用该print()函数.您甚至不必再手动调用它.但是,在firefox中,这只能使用visibility: hidden而不能使用display: none.

function print_pdf(url){
    var iFrameJQueryObject = $('<iframe id="iframe" src="'+url+'" style="visibility: hidden"></iframe>');
    $('#foo').append(iFrameJQueryObject);
}
print_pdf('mozilla_generated.pdf');
Run Code Online (Sandbox Code Playgroud)

Chrome:安全错误(跨域)

pdf应位于同一主机上.Firefox在我的测试中对其他域名没问题,但chrome给了我跨域错误.


Firefox:about:blank仅包含打印页面

您将在firefox(jsfiddle)中获得一个空页面,因为它会在加载任何内容之前打印iframe.提到的方法$(document).onload()无济于事,因为它们只等待DOM加载并setTimeout()仍然可能导致错误,因为您不知道加载iFrame需要多长时间.

您可以使用jQuery简单地解决此问题load().(doc)这将使您可以使用回调函数作为参数.

如果提供了"完整"回调,则在后处理和执行HTML插入之后执行.对jQuery集合中的每个元素触发一次回调,并this依次设置为每个DOM元素.

代码示例1

function print_pdf(url){
    var id = 'iframe', html = '<iframe id="'+id+'" src="'+url+'" style="display:none"></iframe>';
    $('body').append(html);
    // wait for the iFrame to fully load and call the print() function afterwards
    $('#' + id).load(function () {
        document.getElementById(id).contentWindow.print();
    });
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以直接创建一个jQuery对象并使用jQuery on()(doc)来附加任何事件处理程序.

代码示例2(jsfiddle)

function print_pdf(url){
    var iFrameJQueryObject = $('<iframe id="iframe" src="'+url+'" style="display:none"></iframe>');
    $('body').append(iFrameJQueryObject);
    iFrameJQueryObject.on('load', function(){
        $(this).get(0).contentWindow.print();
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 我在Firefox中遇到这个错误:`错误:访问属性"打印"`的权限被拒绝 (5认同)
  • 如果您将实际的PDF文件放入该函数中,将无法正常使用http://jsfiddle.net/zrp6sd6h/4/`错误:拒绝访问属性“ print”的权限” (2认同)
  • - 我认为它与Firefox有关,它使用自己的javascript PDF查看器`pdf.js` (2认同)