0

Using javascript -- in an iPad html5 web app, is it possible to print a pdf which is loaded in an iframe?

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';

The goal is for the airprint dialog to open. This is a major issue for me, please help!!!

Brian McGinity
  • 5,777
  • 5
  • 36
  • 46

3 Answers3

3

I think you are nearly get the solution. The only left is just how you print the iframe. Try to use this.

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

Hope it helps :)

kororo
  • 1,972
  • 15
  • 26
  • This does work and does answer the question correctly. In safari, the pdf print dialog will open (tested with gen 1 ipad). And you've earned the bounty. My problem still persists and I should have detailed the exact problem: The web app is being served inside of an ios webview, which I thought would behave the same as plain Safari. So this solution does not work in an ios webview. I know it is possible to write ios code to interact with the iframe and trigger the print. My goal is to do it in javascript, if you know of anything, please help. – Brian McGinity Aug 27 '14 at 18:33
  • Hi Brian, glad it works in Safari, it is true, this window.print will not work if you try in other app than safari such as UIWebView because Apple did differently between two. Please have extends read about this URL, it is pretty much the full solution that you look after. http://stackoverflow.com/questions/22364543/uiwebview-response-to-window-print – kororo Aug 27 '14 at 23:20
  • What I used to do is create the pdf with a javascript inside of the PDF which would trigger printing. That worked in newer generation ipads until an ios patch was released--even in uiwebview. I guess I am stuck writing xcode and re-releasing the app. I am not the most fluent xcoder. – Brian McGinity Aug 28 '14 at 00:12
  • Ah yeah, forgot to put the extra side-effect on the solution, you need to update the app and republish it to the App Store. Otherwise, I hope from your app there is a way to open using iBooks options. User still able to get away from it. Although, it is bit round-trip. – kororo Aug 28 '14 at 01:33
2

A different way to the other two answers is to use CSS to print only the 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. The advantage over the other options is that it will work with JavaScript turned off, which some users may have done for security reasons.

Zak
  • 1,910
  • 3
  • 16
  • 31
1

I'm pretty confident about safari supporting 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 :

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

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

Source : 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.

Hotted24
  • 502
  • 3
  • 8