直接从JavaScript打印PDF [英] Print PDF directly from JavaScript

查看:143
本文介绍了直接从JavaScript打印PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a list of PDFs in HTML. In the list I'd like to include a download link and a print button/link. Is there some way to directly open the Print dialog for the PDF without the user seeing the PDF or opening a PDF viewer?

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

Some variation of downloading the PDF into a hidden iframe and triggering it to print with JavaScript?

推荐答案

这个问题演示了一种可能对你有帮助的方法:无声打印嵌入式PDF

This question demonstrates an approach that might be helpful to you: Silent print a embedded PDF

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

It uses the <embed> tag to embed the PDF in the document:

<embed
    type="application/pdf"
    src="path_to_pdf_document.pdf"
    id="pdfDocument"
    width="100%"
    height="100%" />

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

Then you call the .print() method on the element in Javascript when the PDF is loaded:

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();
    }
}

您可以将嵌入放置在隐藏的iframe中并打印它从那里,给你一个无缝的体验。

You could place the embed in a hidden iframe and print it from there, giving you a seamless experience.

这篇关于直接从JavaScript打印PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆