简体   繁体   English

在所有流行的浏览器中使用Javascript打印PDF文件

[英]Print PDF file with Javascript in all popular browser

How to print a PDF file using Javascript across all popular browser? 如何在所有流行的浏览器中使用Javascript打印PDF文件? I have done quite extensive research on this question and there is no real solutions. 我对此问题进行了广泛的研究,没有真正的解决方案。

First approach is to use iFrame, however, iFrame is not a good practise. 第一种方法是使用iFrame,但是iFrame并不是一个好习惯。 Code is listed as following. 代码如下。

function printIframePdf(){
    window.frames["printf"].focus();
    try {
        window.frames["printf"].print();
    }
    catch(e){
        window.print();
        console.log(e);
    }
}

Second approach is to use <embed> or <object> tag to display pdf and use document.getElementById(pdfID).print() to print. 第二种方法是使用<embed><object>标记显示pdf并使用document.getElementById(pdfID).print()进行打印。 However, Chrome does not support this function. 但是,Chrome不支持此功能。 Code is listed as following. 代码如下。

function printObjectPdf() {
    document.getElementById('idPdf').Print();
}

Is there a good solution for this? 有一个好的解决方案吗?

Did you consider PDF.js? 您是否考虑过PDF.js?

https://mozilla.github.io/pdf.js/ https://mozilla.github.io/pdf.js/

This is web standards-based platform. 这是基于Web标准的平台。

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

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