我有一个带有嵌入式PDF的网页.我的代码看起来像这样:
<embed
type="application/pdf"
src="path_to_pdf_document.pdf"
id="pdfDocument"
width="100%"
height="100%">
</embed>
Run Code Online (Sandbox Code Playgroud)
我有这个javascript代码用于打印我的PDF:
function printDocument(documentId) {
//Wait until PDF is ready to print
if (typeof document.getElementById(documentId).print == 'undefined') {
setTimeout(function(){printDocument(documentId);}, 1000);
} else {
var x = document.getElementById(documentId);
x.print();
}
}
Run Code Online (Sandbox Code Playgroud)
执行此代码时,Acrobat插件将打开众所周知的打印对话框.像这样的东西:

两个问题:
关于我的系统的更多信息:
操作系统: Windows XP
浏览器: Internet Explorer 7
PDF插件: Acrobat Reader 9
我正在从我的服务器将一个Base64编码的pdf作为字符串加载到我的Javascript中.我的客户端应用程序使用的是AngularJS,HTML5.
我的HTML看起来像这样:
<div id="printablePdfContainer">
<iframe id="printablePdf" width="100%" height="100%"></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)
我的Javascript看起来像这样:
var pdfName = 'data:application/pdf;base64,' + data[0].PrintImage;
var embeddedPdf = document.getElementById('printablePdf');
embeddedPdf.setAttribute('src', pdfName);
$scope.printDocument(embeddedPdf);
Run Code Online (Sandbox Code Playgroud)
我的printDocument功能如下所示:
$scope.printDocument = function() {
var test = document.getElementById('printablePdf');
if (typeof document.getElementById('printablePdf').print === 'undefined') {
setTimeout(function(){$scope.printDocument();}, 1000);
} else {
var x = document.getElementById('printablePdf');
x.print();
}
};
Run Code Online (Sandbox Code Playgroud)
printDocument函数取自堆栈溢出中的预先存在的问题(静默打印嵌入式PDF),这是打印嵌入式pdf的答案.但是,这似乎不再起作用了.我总是得到'未定义'
typeof document.getElementById('printablePdf').print === 'undefined'
Run Code Online (Sandbox Code Playgroud)
校验.似乎.print不存在或什么的.
所以,我的问题是:如何使用Javascript在HTML5中打印嵌入式PDF,而无需打开弹出窗口?
问候,菲尔