Skip to main content

Can a PDF file"s print dialog be opened with Javascript?



I know how to open a webpage in a new window and add javascript so the print dialog pops up. Is there a way to do a similar thing with a PDF file?





Source: Tips4all

Comments

  1. Probably not because the print command is being sent directly from the PDF application and not the browser(which the JS interacts with).

    ReplyDelete
  2. Yes you can...

    PDFs have Javascript support. I needed to have auto print capabilities when a PHP-generated PDF was created and I was able to use FPDF to get it to work:

    http://www.fpdf.org/en/script/script36.php

    ReplyDelete
  3. Just figured out how to do this within the PDF itself - if you have acrobat pro, go to your pages tab, right click on the thumbnail for the first page, and click page properties. Click on the actions tab at the top of the window and under select trigger choose page open. Under select action choose "run a javascript". Then in the javascript window, type this:

    this.print({bUI: false, bSilent: true, bShrinkToFit: true});


    This will print your document without a dialogue to the default printer on your machine. If you want the print dialog, just change bUI to true, bSilent to false, and optionally, remove the shrink to fit parameter.

    Auto-printing PDF!

    ReplyDelete
  4. I usually do something similar to the approach given by How to Use JavaScript to Print a PDF (eHow.com), using an iframe.


    a function to house the print trigger...

    function printTrigger(elementId) {
    var getMyFrame = document.getElementById(elementId);
    getMyFrame.focus();
    getMyFrame.contentWindow.print();
    }

    an button to give the user access...

    (an onClick on an a or button or input or whatever you wish)

    <input type="button" value="Print" onclick="printTrigger('iFramePdf');" />

    an iframe pointing to your PDF...

    <iframe id="iFramePdf" src="myPdfUrl.pdf" style="dispaly:none;"></iframe>





    Bonus Idea #1 - Create the iframe and add it to your page within the printTrigger(); so that the PDF isn't loaded until the user clicks your "Print" button, then the javascript can attack! the iframe and trigger the print dialog.



    Bonus Idea #2 - Extra credit if you disable your "Print" button and give the user a little loading spinner or something after they click it, so that they know something's in process instead of clicking it repeatedly!

    ReplyDelete
  5. embed:

    < object type="application/pdf" data="example.pdf" width="100%" height="100%" id="examplePDF" name="examplePDF" >< param name='src' value='example.pdf'/>< /object>

    < script>
    examplePDF.printWithDialog();
    < /script>


    May have to fool around with the ids/names.
    Using adobe reader...

    ReplyDelete
  6. If you know how PDF files are structured (or are willing to spend a little while reading the spec), you can do it this way.

    Use the Named Action "Print" in the OpenAction field of the Catalog object; the "Print" action is undocumented, but Acrobat Reader and most of the other major readers understand it. A nice benefit of this approach is that you don't get any JavaScript warnings. See here for details: http://www.gnostice.com/nl_article.asp?id=157

    To make it even shinier, I added a second Action, URI, directing the reader to go back to the page that originated the request. Then I attached this Action to the first Named action using its Next field. With content disposition set to "inline", this makes it so that when the user clicks on the print link:


    It opens up Adobe Reader in the same tab and loads the file
    It immediately shows the print dialog
    As soon as the Print dialog is closed (whether they hit "OK" or "cancel"), the browser tab goes back to the webpage


    I was able to do all these changes in Ruby easily enough using only the File and IO modules; I opened the PDF I had generated with an external tool, followed the xref to the existing Catalog section, then appended a new section onto the PDF with an updated Catalog object containing my special OpenAction line, and also the new Action objects.

    Because of PDF's incremental revision features, you don't have to make any changes to the existing data to do this, just append an additional section to the end.

    ReplyDelete
  7. Why not use the Actions menu option to set this?

    Do the following: If you have Acrobat Pro, go to your pages tab, right click on the thumbnail for the first page, and click page properties. Click on the actions tab at the top of the window and under select trigger choose page open. Under select action choose 'Execute a menu item'. Click the Add button then select 'File > Print' then OK. Click OK again and save the PDF.

    ReplyDelete
  8. if you embed the pdf in your webpage and reference the object id, you should be able to do it.

    eg.
    in your HTML:

    <object ID="examplePDF" type="application/pdf" data="example.pdf" width="500" height="500">


    in your javascript:

    <script>

    var pdf = document.getElementById("examplePDF");

    pdf.print();

    </script>


    I hope that helps.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex