8

here is my code to print a pdf file. here while printing time iam getting one page only i need a solution for that

  function printPdf(){
     var ifr = document.getElementById("frame1");
         //PDF is completely loaded. (.load() wasn't working properly with PDFs)
     ifr.onreadystatechange = function () {
        if (ifr.readyState == 'complete') {
              ifr.contentWindow.focus();
              ifr.contentWindow.print();
           }
       }
 }
ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
Vijay P.V
  • 145
  • 2
  • 6
  • 17
  • This may helps: http://stackoverflow.com/questions/12642630/modal-pdf-iframe-with-jquery http://stackoverflow.com/questions/12974115/to-open-pdf-file-in-iframe – Suresh Sep 19 '13 at 07:19

2 Answers2

6

I suspect that's because the whole window gets printed (which has the current view of the iframe with the 1st page of the PDF rendered). Use <object> instead:

<!DOCTYPE html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>

    <script>
    function PrintPdf() {
        idPrint.disabled = 0;
        idPdf.Print();
    }

    function idPdf_onreadystatechange() {
        if (idPdf.readyState === 4)
            setTimeout(PrintPdf, 1000);
    }
    </script>

</head>

<body>
    <button id="idPrint" disabled=1 onclick="PrintPdf()">Print</button>
    <br>
    <object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"
        width="300" height="400" type="application/pdf"
        data="test.pdf?#view=Fit&scrollbar=0&toolbar=0&navpanes=0">
        <span>PDF plugin is not available.</span>
    </object>
</body>

This code is verified with IE. Other browsers will still render the PDF, but may not print it.

[UPDATE] If you need dynamic loading and printing, the changes to the above code are minimal:

<!DOCTYPE html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>

    <script>
    function PrintPdf() {
        idPdf.Print();
    }

    function idPdf_onreadystatechange() {
        if (idPdf.readyState === 4)
            setTimeout(PrintPdf, 1000);
    }

    function LoadAndPrint(url)
    {
        idContainer.innerHTML = 
            '<object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"'+
                'width="300" height="400" type="application/pdf"' +
                'data="' + url + '?#view=Fit&scrollbar=0&toolbar=0&navpanes=0">' +
                '<span>PDF plugin is not available.</span>'+
            '</object>';
    }
    </script>
</head>

<body>
    <button id="idPrint" onclick="LoadAndPrint('http://localhost/example.pdf')">Load and Print</button>
    <br>
    <div id="idContainer"></div>
</body>
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • thank you for your reply. actually before i was using object then i changed to iframe because maybe IE9 will create problem use of object can you try for iframe.. – Vijay P.V Sep 19 '13 at 09:36
  • I don't think it's possible. The best you can print is the whole window, not a specific ` – noseratio Sep 19 '13 at 09:52
  • but its not working for me.. i want to print after loading the object – Vijay P.V Sep 20 '13 at 13:28
  • Some ideas: try `idPdf.gotoLastPage`, `idPdf.printAll()`, or increase the timeout: `setTimeout(PrintPdf, 2000)` (this is a delay to get the initial PDF view rendered after the file has been loaded). Here is a [related post](http://stackoverflow.com/a/18306632/1768303) which lists all methods available on the PDF ``. – noseratio Sep 20 '13 at 15:11
  • No problem. Which one of the above options has helped, exactly? – noseratio Sep 20 '13 at 15:30
  • but one thing happened i gave setTimout(PrintPdf,20000) for 800 pages pdf its coming after loading but if i load 5 page pdf its taking time to print how to manage that issue – Vijay P.V Sep 20 '13 at 15:34
  • I don't think it's possible to track the printing job progress with PDF `` plugin from JavaScript. You're going to have to ask the user to start a new download/printout. – noseratio Sep 20 '13 at 15:37
  • yes that only but for coming that printdialog box taking same time if i load 5 page document or if i give 800 page document because iam giving setTimout(PrintPdf,20000)thats why i want to manage this. how can i manage?? – Vijay P.V Sep 20 '13 at 15:44
  • I don't think you can figure out the total number of pages in PDF from JavaScript. So, you cannot fully manage this. – noseratio Sep 20 '13 at 15:57
  • i just want to know one thing what is the meaning of this readyState === 4?????? – Vijay P.V Sep 21 '13 at 12:22
  • Here you go: [readyState](http://msdn.microsoft.com/en-us/library/ie/ms534360(v=vs.85).aspx). It's `4` when the object has finished loading its content. – noseratio Sep 21 '13 at 12:54
1
<iframe src="teste.pdf" id="meupdf" width="800" height="600" />

function printPdf) {

    var PDF = document.getElementById("meupdf");
    PDF.focus();
    PDF.contentWindow.print();
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423