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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.