简体   繁体   English

iPad:如何在javascript中的iframe中打印pdf

[英]iPad: How to print a pdf in an iframe in javascript

Using javascript -- in an iPad html5 web app, is it possible to print a pdf which is loaded in an iframe? 使用javascript-在iPad html5网络应用程序中,是否可以打印加载到iframe中的pdf?

I have tried numerous things without success such as: 我尝试了很多没有成功的事情,例如:

var iframe = document.getElementsByTagName('iframe')[0]

if (iframe.attachEvent) {
    iframe.attachEvent("onload", function(){
      console.log("Local iframe is now loaded.");
      iframe.contentWindow.print()
    });
}
else {
    iframe.onload = function(){
        console.log("sLocal iframe is now loaded.");
        iframe.contentWindow.print()
    };
}

The iframe's url is '/data/xyz.pdf'; iframe的网址是“ /data/xyz.pdf”;

The goal is for the airprint dialog to open. 目的是打开“打印”对话框。 This is a major issue for me, please help!!! 这对我来说是一个重大问题,请帮忙!!!

I think you are nearly get the solution. 我认为您几乎可以解决。 The only left is just how you print the iframe. 唯一剩下的就是您如何打印iframe。 Try to use this. 尝试使用这个。

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

Hope it helps :) 希望能帮助到你 :)

A different way to the other two answers is to use CSS to print only the iframe: 其他两个答案的另一种方法是使用CSS仅打印iframe:

@media print {
    * {
        display: none;
    }

    iframe {
        display: block;
        width: 100%;
        height: 100%;
    }
}

This basically makes the iframe the full width and height of the page, and hides other elements, so that the iframe is the only thing in view when the user clicks print. 这基本上使iframe达到了页面的整个宽度和高度,并隐藏了其他元素,因此,当用户单击“打印”时,iframe是视图中唯一的东西。 The advantage over the other options is that it will work with JavaScript turned off, which some users may have done for security reasons. 与其他选项相比的优势在于,它可以在关闭JavaScript的情况下使用,出于安全原因,某些用户可能已这样做。

I'm pretty confident about safari supporting PDFObject. 我对Safari支持PDFObject很有信心。 It's just an object to declare, pretty easy to use. 它只是一个声明的对象,非常易于使用。

Even if not exactly an iframe, you can use a div this way : 即使不完全是iframe,您也可以通过以下方式使用div:

<div id="pdfIframe"></div>

var pdf = new PDFObject({
  url: "/data/xyz.pdf",
  id: "pdfRendered",
  pdfOpenParams: {
    view: "FitH"
  }
}).embed("pdfIframe");

Source : http://pdfobject.com/ 资料来源: http : //pdfobject.com/

EDIT : I did not see it was a printing issue, so my answer isn't really one. 编辑:我没有看到这是一个印刷问题,所以我的回答不是真的。 BTW, i remember a friend using it, he had no problem to print it, so maybe there is an included printing support function. 顺便说一句,我记得有一个朋友在使用它,他没问题打印,所以也许有附带的打印支持功能。

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

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