繁体   English   中英

在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"

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

现在您可以简单地将生成的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仅限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();
    });
}

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

代码示例2jsfiddle

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

编辑,更新

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

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");
}

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

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

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

@clarkk我建议使用一些功能更强大的功能,我的建议是使用http://pdfmake.org/#/

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

我暂时没有使用它,但这是我以前用来从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();

使用javascript或jquery打印pdf

在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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM