直接从JavaScript打印PDF

Cra*_*ste 85 javascript pdf

我正在构建HTML格式的PDF列表.在列表中,我想要包含下载链接和打印按钮/链接.有没有办法在没有用户看到PDF或打开PDF查看器的情况下直接打开PDF的"打印"对话框?

将PDF下载到隐藏的iframe并触发使用JavaScript打印的一些变体?

nul*_*ity 54

此问题演示了一种可能对您有所帮助的方法:静默打印嵌入式PDF

它使用<embed>标记将PDF嵌入到文档中:

<embed
    type="application/pdf"
    src="path_to_pdf_document.pdf"
    id="pdfDocument"
    width="100%"
    height="100%" />
Run Code Online (Sandbox Code Playgroud)

然后.print()在加载PDF时在Javascript中调用元素上的方法:

function printDocument(documentId) {
    var doc = document.getElementById(documentId);

    //Wait until PDF is ready to print    
    if (typeof doc.print === 'undefined') {    
        setTimeout(function(){printDocument(documentId);}, 1000);
    } else {
        doc.print();
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以将嵌入放置在隐藏的iframe中并从那里打印出来,为您提供无缝体验.

  • 如果嵌入的文档位于不同的域中,则无效. (8认同)
  • 是的,我在所有未定义print()方法的浏览器上遇到此问题.这种方法已经过时了吗?还有其他解决方案吗? (6认同)
  • 更容易将javascript添加到pdf以在渲染时进行打印.这就是Google Docs的功能.这样浏览器加载并打印它,或adobe插件. (5认同)
  • 这不再有效。Chrome 最新版本,同一域中的 PDF。@nullability 如果您找不到解决方法,请进行编辑并明确指出过去所写的内容是真实的,但不再有效,这样人们就不会浪费时间尝试。 (4认同)
  • 此解决方案不起作用...我获得Chrome,FF的许可被拒绝 (2认同)
  • 你可能会用谷歌搜索它,但它只是一个添加到 pdf 的新脚本对象,其中 javascript 只是“window.print()” (2认同)
  • 对我不起作用,typeof doc.print 总是“未定义” Google Chrome #71 (2认同)

Nic*_*DIA 33

这是一个从iframe打印PDF的功能.

您只需将PDF的URL传递给该函数即可.一旦加载PDF,它将创建一个iframe并触发打印.

请注意,该函数不会破坏iframe.相反,它每次调用函数时都会重用它.破坏iframe很难,因为在打印完成之前需要它,并且打印方法没有回调支持(据我所知).

printPdf = function (url) {
  var iframe = this._printIframe;
  if (!this._printIframe) {
    iframe = this._printIframe = document.createElement('iframe');
    document.body.appendChild(iframe);

    iframe.style.display = 'none';
    iframe.onload = function() {
      setTimeout(function() {
        iframe.focus();
        iframe.contentWindow.print();
      }, 1);
    };
  }

  iframe.src = url;
}
Run Code Online (Sandbox Code Playgroud)

  • 如果 iframe 内容不是来自同一来源,Chrome 会阻止 .print() 方法。不幸的是,从 2021 年起就无法使用了 (5认同)
  • 谢谢你,因为你帮我解决了一个大问题:没有`setTimeout`,打印功能有时会失败。不知道为什么,希望有人能找到。 (3认同)

use*_*203 17

http://printjs.crabbly.com/下载Print.js

$http({
    url: "",
    method: "GET",
    headers: {
        "Content-type": "application/pdf"
    },
    responseType: "arraybuffer"
}).success(function (data, status, headers, config) {
    var pdfFile = new Blob([data], {
        type: "application/pdf"
    });
    var pdfUrl = URL.createObjectURL(pdfFile);
    //window.open(pdfUrl);
    printJS(pdfUrl);
    //var printwWindow = $window.open(pdfUrl);
    //printwWindow.print();
}).error(function (data, status, headers, config) {
    alert("Sorry, something went wrong")
});
Run Code Online (Sandbox Code Playgroud)

  • 不在IE,Edge或Firefox上打印PDF. (3认同)
  • 刚刚尝试了演示页面。它在 Firefox 中打印 (2认同)

小智 12

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

现场演示http://mozilla.github.io/pdf.js/

它可能就是你想要的,但我看不出这一点,因为现代浏览器包含这样的功能,而且它在移动设备等低功耗设备上的运行速度非常慢,顺便提一下,它们有自己的优化插件和应用程序.


Adn*_*hah 6

我使用此功能从服务器下载 pdf 流。

function printPdf(url) {
        var iframe = document.createElement('iframe');
        // iframe.id = 'pdfIframe'
        iframe.className='pdfIframe'
        document.body.appendChild(iframe);
        iframe.style.display = 'none';
        iframe.onload = function () {
            setTimeout(function () {
                iframe.focus();
                iframe.contentWindow.print();
                URL.revokeObjectURL(url)
                // document.body.removeChild(iframe)
            }, 1);
        };
        iframe.src = url;
        // URL.revokeObjectURL(url)
    }
Run Code Online (Sandbox Code Playgroud)


Pap*_*api 5

从 base64 字符串打印 pdf 的跨浏览器解决方案:

  • Chrome:打印窗口已打开
  • FF:打开带有 pdf 的新标签页
  • IE11:打开/保存提示被打开

.

const blobPdfFromBase64String = base64String => {
   const byteArray = Uint8Array.from(
     atob(base64String)
       .split('')
       .map(char => char.charCodeAt(0))
   );
  return new Blob([byteArray], { type: 'application/pdf' });
};

const isIE11 = !!(window.navigator && window.navigator.msSaveOrOpenBlob); // or however you want to check it

const printPDF = blob => {
   try {
     isIE11
       ? window.navigator.msSaveOrOpenBlob(blob, 'documents.pdf')
       : printJS(URL.createObjectURL(blob)); // http://printjs.crabbly.com/
   } catch (e) {
     throw PDFError;
   }
};

printPDF(blobPdfFromBase64String(base64String))
Run Code Online (Sandbox Code Playgroud)

奖励 - 在 IE11 的新选项卡中打开 blob 文件

如果您能够在服务器上对 base64 字符串进行一些预处理,则可以在某个 url 下公开它并使用printJS:) 中的链接