如何打印pdf.js文件? [英] How to print pdf.js document?
问题描述
我已经用pdf.js生成了一个文档,它可以正确显示.我没有打印按钮.如何添加按钮以允许用户进行打印?我正在使用Chrome.
I've generated a document with pdf.js and it shows correctly. I'don't have print button. How to add the button to allow users to print it ? I was using Chrome.
推荐答案
尝试使用javascript函数 window.print();
,这会打开打印对话框.
Try using the javascript-function window.print();
, which opens the print-dialog.
您将必须在html中添加一个按钮,这会触发命令-在pdf中是不可能的.
You will have to add an button to your html, which triggers the command - its not possible within the pdf.
由于这个原因,您将需要一个iFrame,并使用如下所示的内容:
For this reason, you will need an iFrame, and use something like this:
function printIt() {
var wnd = window.open('http://your-pdf-url');
wnd.print();
}
<input type="button" value="Print" onclick=printIt()>
window.print()
不起作用,因为它还会打印周围的html.
window.print()
wouldn't work, because it would also print the surrounding html.
从您的评论中,我现在知道,您想打印画布元素的内容-这要容易得多.
From your comment, I now know, that you want to print the content of a canvas-element - which is much easier.
您不需要iframe,可以将按钮放在同一页上,然后使用 window.print();
.
You don't need an iframe, you can put the button on the same page, and use window.print();
.
为了只打印画布元素并隐藏周围环境(如按钮),可以使用css-Syntax像这样:
In order to only print the canvas-element, and to hide the surroundings (like the button), you can use css-Syntax like this:
@media print
{
.no-print, .no-print *
{
display: none !important;
}
}
@media print
指定CSS代码,该代码仅适用于网页(如果已打印).如果现在将 .no-print
类分配给除canvas元素之外的所有内容,则仅会打印pdf.
@media print
specifies css-code, which only applies for a webpage, if it gets printed. If you now asign the class .no-print
to everything except the canvas-element, only your pdf will be printed.
如果更容易,您也可以使用此CSS代码:
You can also use this css-code, if it's easier:
@media print
{
*
{
display: none;
}
canvas
{
display: inline;
}
}
这篇关于如何打印pdf.js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!