如何使用 JavaScript 将 pdf 文件直接发送到打印机? [英] How to send a pdf file directly to the printer using JavaScript?

查看:28
本文介绍了如何使用 JavaScript 将 pdf 文件直接发送到打印机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 JavaScript 将 PDF 文件直接发送到打印机?

How to send a PDF file directly to the printer using JavaScript?

我在论坛中找到了两个答案:

I found two answers in a forum:

<embed src="vehinvc.pdf" id = "Pdf1" name="Pdf1" hidden>
<a onClick="document.getElementById('Pdf1').printWithDialog()" style="cursor:hand;">Print file</a>

<OBJECT id = "Pdf2" name="Pdf2" CLASSID="clsid:CA8A9780-280D-11CF-A24D-444553540000" WIDTH="364" HEIGHT="290">
     <PARAM NAME='SRC' VALUE="file.pdf">
</OBJECT>
<a onClick="document.Pdf2.printWithDialog()">Print file</a> 

但我的问题是它只适用于 IE,而不适用于 Firefox 或 Chrome.

But my problem is that it just works on IE, and doesnt work in Firefox or Chrome.

有什么解决办法吗?

推荐答案

我认为这个 JavaScript 库可能对您有所帮助:

I think this Library of JavaScript might Help you:

它叫做Print.js

It's called Print.js

首先包含

<script src="print.js"></script>
<link rel="stylesheet" type="text/css" href="print.css">

它的基本用法是调用printJS()并传入一个PDF文档url:printJS('docs/PrintJS.pdf')

It's basic usage is to call printJS() and just pass in a PDF document url: printJS('docs/PrintJS.pdf')

我所做的是这样的,如果PDF文档太大,这也会显示正在加载....".

What I did was something like this, this will also show "Loading...." if PDF document is too large.

<button type="button" onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})">
    Print PDF with Message
</button>

但是请记住:

Firefox 目前不允许使用 iframe 打印 PDF 文档.Mozilla 的网站上有一个关于此的开放错误.使用 Firefox 时,Print.js 会在新标签页中打开 PDF 文件.

Firefox currently doesn't allow printing PDF documents using iframes. There is an open bug in Mozilla's website about this. When using Firefox, Print.js will open the PDF file into a new tab.

这篇关于如何使用 JavaScript 将 pdf 文件直接发送到打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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