在 Firefox 中打印 PDF [英] Print PDF in Firefox

查看:30
本文介绍了在 Firefox 中打印 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Firefox 中打印 PDF?

How to print a PDF in Firefox?

此功能在 Chrome 中有效,但在 Firefox 中无效

This function works in Chrome but not in 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();
    }
}

错误

Error: Permission denied to access property "print"

推荐答案

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

这是一个 firefox 中的错误.在本地,可以通过转到 about:config 并将 pdfjs.disabled 的属性设置为 true 来禁用它.唯一可能的解决方法是使用服务器端脚本并修改 pdf.使用 php 你可以使用 fpdf 并嵌入 extensions 来实现js(包括print() 函数)或简单地将pdf 转换为图像,返回url 并打印它.您可以使用 FPDI 来修改现有的 pdf.我会给你一个例子,说明我是如何让它与 PHP 一起工作的.

Firefox: Permission denied to access property "print"

This is a bug in firefox. Locally it can be disabled by going to about:config and set the property of pdfjs.disabled to true. Only possible workaround is to use a server-side script and modify the pdf. Using php you could use fpdf and embed extensions to implement js (inclunding the print() function) or simply convert the pdf to an image, return the url and print it. You could use FPDI to modify the existing pdf. I will give you an example on how I got it to work with PHP.

使用 FPDI 生成带有内联 javascript(自动打印)的 PDF 文件和 PDF_JS

Generating a PDF file with inline javascript (autoprint) using FPDI and PDF_JS

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

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

Now you can simply append the generated pdf to your page and the included javascript will call the print() function. You do not even have to call it manually anymore. However, in firefox this will only work with visibility: hidden and not with 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');

<小时>

Chrome:安全错误(跨域)

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


Chrome: Security Error (cross-origin)

The pdf should be located at the same host. Firefox was okay with other domains in my tests, but chrome gave me cross-origin errors.

你会在 firefox 中得到一个空页面(jsfiddle),因为它会在之前打印 iframe它已加载任何内容.提到的方法像 $(document).onload() 不会有帮助,因为它们只等待 DOM 加载而 setTimeout() 仍然会导致错误,因为你不知道加载 iFrame 需要多长时间.

You will get an empty page in firefox (jsfiddle), because it will print the iframe before it has loaded any content. Mentioned methods like $(document).onload() won't help, since they only wait for the DOM to load and setTimeout() can still result in errors, since you do not know how long it takes the iFrame to load.

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

You can simply resolve this issue by using jQuery's load(). (doc) This will give you the possibility to use a callback function as parameter.

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

if a "complete" callback is provided, it is executed after post-processing and HTML insertion has been performed. The callback is fired once for each element in the jQuery collection, and this is set to each DOM element in turn.

代码示例 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();
    });
}

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

Alternatively you could directly create an jQuery object and use jQuery's on() (doc) to attach any event handler.

代码示例 2(jsfiddle)

Code Example 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();
    });
}

这篇关于在 Firefox 中打印 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆