【问题标题】:Print pdf from iframe从 iframe 打印 pdf
【发布时间】:2014-08-20 08:19:38
【问题描述】:

我在 iframe 中动态创建 pdf:

<iframe name="output" id="output"></iframe>

并尝试从中打印:

window.frames["output"].focus();
window.frames["output"].print();

但是在这个例子中你怎么看:

http://jsfiddle.net/FEvq6/78/

它打印一个空白站点。

【问题讨论】:

  • 我以为我可以添加一个 setTimeout 但 Chrome 给了我Uncaught SecurityError: Blocked a frame with origin "http://fiddle.jshell.net" from accessing a frame with origin "null". The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "data". Protocols must match.
  • 如果您可以将 pdf javascript 添加到文件本身以打印自己 - stackoverflow.com/a/13992297/295783

标签: javascript jquery iframe


【解决方案1】:

它会打印空白页,因为您正在打印 iframe 本身。这是你应该做的:

1. iframe 应包含将加载 PDF 的 EMBED 标签(由于某种原因堆栈溢出没有很好地格式化代码,请参阅下面的粘贴 bin 链接):

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

2. 然后你应该调用 EMBED 对象来打印文档。但由于它可能需要一些时间才能加载,因此您需要超时:

var doPrinting = (id){
    var pdfObject = document.getElementById(id);
    if (typeof(pdfObject.print) === 'undefined') {    
         setTimeout(function(){ doPrinting(id); }, 1000);
    } else {
        pdfObject.print();
    }
 }

 doPrinting('pdf');

这里是上面的粘贴箱:http://pastebin.com/s6qSTE8t

【讨论】:

  • var doc 设置在哪里?并且 url 是动态数据 URI
  • 对于URL,比如&lt;embed src="generate_pdf.php" ...这样使用就可以了。
  • 我的错,这不是一个跨浏览器的解决方案。它仅适用于 IE。
  • 是的,这就是问题所在!我实际上只需要 Chrome 的解决方案!谢谢!你最终知道另一个解决方案吗?谢谢!
  • 这适用于 IE 吗?这正是我需要的:D
【解决方案2】:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<strike>funny</strike>.

    <iframe src="file_to_print.pdf" class="frameSet" type="application/pdf" runat="server" name="I5" scrolling="yes" width="100%" height="400">  </iframe>

</body>
</html>

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 2023-03-10
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多