如何使用html打印按钮在iframe中打印此URL的pdf
我使用这些线程的答案尝试了两种解决方案:
我的html是上面的pdf网址的iframe,我的js看起来像:
<embed
type="application/pdf"
src="the url that is above. too long to fit here"
id="pdfDocument"
width="100%"
height="100%" />
<button id="printButton" onclick="javascript:printPage()"
>
我的js:
function printPage(documentId) {
var doc = document.getElementById('pdfDocument');
//Wait until PDF is ready to print
if (typeof doc.print === 'undefined') {
setTimeout(function(){printPage(documentId);}, 1000);
} else {
doc.print();
}
}
我没有运气。该按钮无响应。 js肯定有问题。他们有一个printjs库。也许这是一条更好的路线。任何建议赞赏。谢谢。
您拥有的URL未返回PDF文档。它确实返回一个嵌入了PDF的HTML文档。因此,您无法使用iframe或Print.js库正确打印文档。要实现这一点,您需要有一个返回实际PDF文档的URL。