如何在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"发布于 2015-11-20 10:15:21
Firefox:拒绝访问属性"print“的权限
这是一个firefox中的bug。在本地,可以通过转到about:config并将pdfjs.disabled属性设置为true来禁用它。唯一可行的解决办法是使用服务器端脚本并修改pdf。使用php,您可以使用fpdf和嵌入扩展来实现js (包括print()函数),或者简单地将pdf转换成图像,返回url并打印出来。您可以使用FPDI修改现有的pdf。我将给您一个例子,说明我如何让它与PHP一起工作。
使用 FPDI 和 PDF格式生成带有内联javascript (自动file )的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()函数。你甚至不用再手动调用它了。但是,在火狐中,这只适用于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');铬:安全错误(跨原点)
pdf应位于同一主机上。Firefox在我的测试中可以接受其他域,但是chrome给了我跨源错误。
火狐:打印页面仅包括about:blank
您将在火狐(小提琴)中得到一个空页面,因为它会在加载任何内容之前打印iframe。前面提到的方法(比如$(document).onload() )不会有帮助,因为它们只是等待DOM加载,而setTimeout()仍然会导致错误,因为您不知道加载iFrame需要多长时间。
您可以通过使用jQuery的load()来解决这个问题。(文档)这将使您可以使用回调函数作为参数。
如果提供了“完整”回调,则在执行后处理和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() (文档)附加任何事件处理程序。
代码示例2 (小提琴)
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();
});
}发布于 2015-11-18 20:08:12
编辑,更新
尝试使用window.onload事件、document.createElement()、onload事件、将duration设置为2000的setTimeout()、在将元素附加到document后设置src of iframe
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
发布于 2015-12-09 10:13:33
PDF有Javascript支持。在创建PHP生成的PDF时,我需要具有自动打印功能,并且我能够使用FPDF使其工作:
http://www.fpdf.org/en/script/script36.php
https://stackoverflow.com/questions/33254679
复制相似问题