1

I'm trying to have a link which open up a print preview for a PDF (the PDF is placed near the actual .html page)

I've tried:

<script>
function printpdf()
{
    var w = window.open('example.pdf');

    w.onload = function()
    {
        w.print();
    };
}
</script>

but it doesn't fire the browser print page. However, the PDF opens up

I also tried the JQuery approach:

<script>
function printpdf()
{
    var w = window.open('example.pdf');

    $(w).ready(function(){
        w.print();
    });
}
</script>

no luck. The PDF opens but the print preview is not triggered.

What I am missing here?

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
  • Seems this is already answered: http://stackoverflow.com/questions/16239513/print-pdf-directly-from-javascript – David R Sep 13 '16 at 10:10
  • 1
    @DavidR no, it's not the same question: the other one asks for printing without the user seeing the PDF or opening a PDF viewer. They ask for a silent print which I don't need – Gianluca Ghettini Sep 13 '16 at 10:11
  • Try this: http://stackoverflow.com/a/17831586/519413 – Rory McCrossan Sep 13 '16 at 10:19
  • I don't think `window.print()` is the correct approach here, you better of use a library like https://github.com/mozilla/pdf.js and use its api to call the print command. – Quannt Sep 13 '16 at 10:20
  • download and possibly modify [pdf.js](http://mozilla.github.io/pdf.js/) - I know I've managed to use that to trigger a print when the pdf is fully rendered – Jaromanda X Sep 13 '16 at 10:24
  • @JaromandaX yes that is correct, I've read the source code and it looks like pdf.js also uses `window.print()` internally so his job is now even more simple, just catch the event once the pdf is rendered and call `window.print()` – Quannt Sep 13 '16 at 10:28

0 Answers0