简体   繁体   English

通过javascript打印pdf

[英]print a pdf via javascript

I have a HTML page that has a link to a PDF. 我有一个HTML页面,该页面具有指向PDF的链接。 This PDF opens in a new window. 该PDF在新窗口中打开。 The parent HTML page has a File->Print menu item. 父HTML页面具有“文件”->“打印”菜单项。 When the user clicks Print I want to be able to automatically open the PDF and print it. 当用户单击“打印”时,我希望能够自动打开PDF并进行打印。 The printing part is where I am stuck. 打印部分是我卡住的地方。

Any help would be much appreciated. 任何帮助将非常感激。

Thanks. 谢谢。

If you open that window with a specific name (eg target="nameofwin" ) in the link pointing at the PDF, you might get away with nameofwin.print() . 如果您在指向PDF的链接中使用特定名称(例如target="nameofwin" )打开该窗口,则可能会nameofwin.print() However, I'm not sure if that'd work since the PDF will be shown using a plugin, and not be a native part of the page. 但是,我不确定这是否行得通,因为PDF将使用插件显示,而不是页面的本机部分。 There may be some API that allows you to interface with the PDF display plugin, but then you're stuck wondering if you've got Acrobat or FoxIt or WhateverElse running in there. 可能有一些API可以让您与PDF显示插件进行交互,但是您想知道是否已在其中运行Acrobat或FoxIt或WhateverElse。

You can probably use PDF.js to render the PDF in a hidden div. 您可能可以使用PDF.js在隐藏的div中呈现PDF。 Then, on your "print" button, register a click in which you add a new print stylesheet to the document, show that div, print the window and then remove the print stylesheet. 然后,在“打印”按钮上,单击一次单击,将新的打印样式表添加到文档中,显示div,打印窗口,然后删除打印样式表。

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

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