在 Firefox 中打印 PDF

Print PDF in Firefox

如何在 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();
    }
}

错误

Error: Permission denied to access property "print"

编辑、更新

尝试使用 window.onload 事件、document.createElement()onload 事件、setTimeout() 并将 duration 设置为 2000,设置 src of iframe 在将元素附加到 document

之后
window.onload = function() {
    function print_pdf(url){
        var id = "iframe", frame = document.createElement("iframe");
        frame.setAttribute("id", id);
        frame.setAttribute("width", "800px");
        frame.setAttribute("height", "600px");
        frame.setAttribute("allowfullscreen", "true");
        frame.setAttribute("name", "printframe");
        document.body.appendChild(frame);
        frame.onload = function() {
          this.requestFullScreen = this.mozRequestFullScreen 
                                   || this.webkitRequestFullScreen;
          this.requestFullScreen();
          setTimeout(function() {
            print()
          },2000)
        }
        frame.setAttribute("src", url);
    }
    print_pdf("http://zeitreisen.zeit.de/wp-content/uploads/2014/09/pdftest2.pdf");
}

plnkrhttp://plnkr.co/edit/mHBNmc5mdM0YJRwRbYif?p=preview

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

这是一个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修改现有的pdf。我会给你一个例子,说明我是如何让它与 PHP.

一起工作的

使用 FPDI and PDF_JS

内联 javascript(自动打印)生成 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');

现在您只需将生成的 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');

Chrome: 安全错误(跨域)

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


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

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

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

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) 来附加任何事件处理程序。

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

@clarkk 我建议使用更强大的东西,很多人都提到过我的建议是使用 http://pdfmake.org/#/ .

我在我的数据表中使用了这个 pdfmake,它工作得非常完美。请记住,如果您从表格或其他内容中打印超过 10 000 行,它 运行 会超出浏览器的内存 :)

Print a pdf with javascript or jquery

在 html 中创建一个 iframe:

<iframe id="pdf-iframe">

然后更改该 iframe 的 src 并在加载时打印它:

$('#pdf-iframe').attr("src", pdf_url).load(function(){
    document.getElementById('pdf-iframe').contentWindow.print();
});

或者,您可能想尝试 https://mozilla.github.io/pdf.js/

我已经有一段时间没有使用它了,但这是我过去用来从 iframe 打印 pdf 的方法...

function printfile() {
    window.frames['objAdobePrint'].focus();
    window.frames['objAdobePrint'].print();
}

<iframe src="urlOfPdf" name="objAdobePrint" id="objAdobePrint"></iframe>
<button onclick="printfile();">Print</button>

您可以在不创建新 iframe 的情况下实现打印功能(仅限 css)以防止安全问题:

var style = document.createElement("style");
style.setAttribute("media", "print"); //style.setAttribute("media", "screen,print");
style.appendChild(document.createTextNode(""));
document.head.appendChild(style);
var width = $("#printDiv").width();
var height = $("#printDiv").height();
style.sheet.insertRule("body { width: 210mm !important, height: 25.4mm !important; visibility: hidden; }", 0);
style.sheet.insertRule("#printDiv { visibility: visible; position: fixed !important;top: 5px;  left: 5px; width:" + width + "px;height:" + height + ";  page-break-after: avoid;}", 0);

window.focus();
window.print(true);
style.remove();

PDF 有 Javascript 支持。当创建 PHP 生成的 PDF 时,我需要具有自动打印功能,并且我能够使用 FPDF 使其工作:

http://www.fpdf.org/en/script/script36.php