使用 Javascript 打印 HTML + 多个 PDF 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15167892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing HTML + Multiple PDF files with Javascript
提问by Jamie
I've got a project where I'm needing to print the HTML page the user is on, as well as multiple PDF files (multiple prompts is OK)
我有一个项目需要打印用户所在的 HTML 页面,以及多个 PDF 文件(多个提示都可以)
After hunting around on Google and Stackoverflow I found Printing multiple PDF files using JavaScriptwhich is close, but not quite working.
在 Google 和 Stackoverflow 上四处寻找后,我发现使用 JavaScript 打印多个 PDF 文件很接近,但效果不佳。
The code below prints the HTML page fine, and creates a print prompt for the PDF, but the PDF print prompt is for a blank page.
下面的代码可以很好地打印 HTML 页面,并为 PDF 创建打印提示,但 PDF 打印提示是针对空白页面的。
I've triple checked the example address and the fine is definitely at http://www.mydomain.co.nz/wp-content/uploads/Golden-China1.pdf.
我已经三次检查了示例地址,罚款肯定是在http://www.mydomain.co.nz/wp-content/uploads/Golden-China1.pdf。
Anyone spot what I'm doing wrong?
有人发现我做错了什么吗?
function PrintAll(file1, file2, file3) {
window.print();
var pages = [file1, file2, file3];
for (var i = 0; i < pages.length; i++) {
if (pages[i] != undefined) {
var oWindow = window.open(pages[i], "print");
oWindow.print();
oWindow.close();
}
}
}
$('.print-btn').click(function() {
PrintAll('/wp-content/uploads/Golden-China1.pdf');
});
回答by Ricardo Ortega Maga?a
I tested some code, and the problem i was getting with your code, is the timeload, added a delay so i let the PDF to complete load, then, send the print command:
我测试了一些代码,我在你的代码中遇到的问题是时间加载,添加了一个延迟,所以我让 PDF 完成加载,然后发送打印命令:
<!DOCTYPE html>
<html>
<body>
<script>
var pages = ["P1.pdf", "P2.pdf", "P3.pdf"];
var oWindow=new Array();
function PrintAll(){
for (var i = 0; i < pages.length; i++) {
oWindow[i].print();
oWindow[i].close();
}
}
function OpenAll() {
for (var i = 0; i < pages.length; i++) {
oWindow[i] = window.open(pages[i]);
}
setTimeout("PrintAll()", 5000);
}
OpenAll();
</script>
</body>
</html>