Firefox:访问属性“打印”的权限被拒绝
这是bug in firefox。在本地可以通过转到about:config 并将pdfjs.disabled 的属性设置为true 来禁用它。唯一可能的解决方法是使用服务器端脚本并修改 pdf。使用php你可以使用fpdf并嵌入extensions来实现js(包括print()函数)或简单地将pdf转换为图像,返回url并打印。您可以使用FPDI 来修改现有的 pdf。我会给你一个例子,说明我是如何让它与 PHP 一起工作的。
使用FPDI 和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) 这将使您可以使用回调函数作为参数。
如果提供了“完成”回调,它会在后处理和 HTML 插入完成后执行。 jQuery 集合中的每个元素都会触发一次回调,并依次为每个 DOM 元素设置this。
代码示例 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();
});
}