26

I have a web page with embedded PDF on it. My code looks like this:

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

I have this javascript code for print my PDF:

function printDocument(documentId) {

    //Wait until PDF is ready to print    
    if (typeof document.getElementById(documentId).print == 'undefined') {

        setTimeout(function(){printDocument(documentId);}, 1000);

    } else {

        var x = document.getElementById(documentId);
        x.print();
    }
}

When this code is executed Acrobat plug-in opens the well-known print dialog. Something like this:

PrintDialog

Two questions:

  • How to improve the way to detect that PDF is loaded and ready for print?
  • How to avoid showing print dialog?

A little more info about my system:

OS: Windows XP

Browser: Internet Explorer 7

PDF Plugin: Acrobat Reader 9

I'm nidhin
  • 2,592
  • 6
  • 36
  • 62
sourcerebels
  • 5,140
  • 1
  • 32
  • 52
  • @gideon for some reason the this does not work .... in ie8 ff or chrome ... ie give error Webpage error details Message: 'null' is null or not an object Line: 26 Char: 5 Code: 0 URI: http://localhost/mpdf/pdf.php if i put in alert () within if typeof the alert continues to keep popping up even after the document is loaded – Apurva Jun 26 '15 at 05:34

4 Answers4

20

You are not going to be able to print silently with plain old JavaScript. How would you like your printer to start printing out 100000000 pages of all black. Not a good thing. If you want to print silently and have it work for Internet Explorer only, there are ActiveX controls out there that can do it. This requires higher security settings for your page and for your users to really trust your site.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Thanks, its for corporate intranet not for the Internet, they must trust! :-) Can you point me to this ActiveX. I will evaluate it. – sourcerebels Jun 10 '09 at 14:30
  • 1
    I think his point was that if the browser allowed silent printing it would be a security hole. So it is likely by design that a browser/plugin doesn't allow this. – JohnFx Nov 25 '10 at 04:11
  • Technology has changed quite a bit since this solution was accepted. Here are some silent printing solutions, some of which can silently print the PDF by URL exactly as requested. http://stackoverflow.com/questions/27057816 – tresf Mar 05 '16 at 02:16
5

This is possible in a trusted, Intranet environment.

<object id="pdfDoc" style="position:absolute;z-index:-1;" name="pdfDoc" classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="900px" height="100%">
        <param name="SRC" value="yourdoc.pdf" />
    </object>

<input type="button" ... onclick="pdfDoc.printAll();" />

This will bypass the print dialog and send directly to the default printer.

PushCode
  • 3,077
  • 4
  • 25
  • 26
  • 5
    I was not able to get this method to work in FF or Safari. Is this IE only? – Karl Apr 08 '11 at 22:21
  • clsid:CA8A9780-280D-11CF-A24D-444553540000 refers to Adobe Reader ActiveX plugin. ActiveX is not supported by Firefox. – mischka Sep 26 '18 at 13:18
2

You can do this in Firefox by changing about:config. Add print.always_print_silent and set it to true.

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
mike
  • 37
  • 1
2

I wonder if you actually need to wait before printing -- won't the print job handle that for you? And I truly hope no modern browser will allow you (or any website for that matter) to print without that confirmation dialog (some old browsers used to do that, a long time ago).

Arjan
  • 22,808
  • 11
  • 61
  • 71
  • Hi Arjan, Thanks for your quick reply. If I call .print method before PDF file is loaded i get a Javascript error something like 'this method is not allowed for this object' (I get the error message in spanish). On your second apreciation I think that PDF is not printed by browser is printed by Acrobat plug-in :-) – sourcerebels Jun 10 '09 at 13:50
  • But that plugin could be present in anybody's browser, right? If so, then I don't expect that confirmation to disappear. Does the onload event for the body element fire before your PDF is loaded? – Arjan Jun 10 '09 at 13:57
  • Oh, it seems to me that Internet Explorer supports onload() for embed as well: http://msdn.microsoft.com/en-us/library/cc197055(VS.85).aspx – Arjan Jun 10 '09 at 14:00
  • @Arjan: Its a corporate Intranet application, all clients have Acrobat reader version 9 installed and Internet Explorer 7 :-), yes, the onload is fired before PDF is loaded and onload event of embed its having same behaviour. – sourcerebels Jun 10 '09 at 14:29
  • 1
    Hmmm, that's odd, the onload firing too early. Let's assume you'll have more luck finding that ActiveX control then... – Arjan Jun 10 '09 at 14:41
  • Thanks Arjan. We are using a kiosk-like OS and the solution for this issue is provided by this software. Thanks for your reply – sourcerebels Jun 15 '09 at 09:28