Wednesday, April 20, 2016

Print whole HTML page including pdf file inside iframe

Leave a Comment

I've got an assignment to embed relatively small pdf file inside html page and print the entire html pade including the pdf file inside the iframe. Here is the structure of my html page: enter image description here

Here is my code:

@media print{  	body * {display:block;}      .toPrint{display:block; border:0; width:100%; min-height:500px}
<body>      <button onclick="window.print()">Print</button>    	<h3>MUST BE PRINTED</h3>      <p> MUST BE PRINTED</p>      <iframe class="toPrint" src="https://nett.umich.edu/sites/default/files/docs/pdf_files_scan_create_reducefilesize.pdf" style="width:100%; height:97vh;"></iframe>  	<h3>MUST BE PRINTED</h3>      <p> MUST BE PRINTED</p>  </body>

Currently I'm printing the page using css @media query. But unfortunately this media query prints the pdf's first page only.

What can I do print the entire pdf file?

4 Answers

Answers 1

The reason it's not printing the whole PDF is because it's in an iframe and the height is fixed. In order to print the whole PDF you'll need the iframe height to match its content height (there should be no scrollbar on the iframe).

Another option is to print only the iframe. Add id to your iFrame:

<iframe id="toPrint" class="toPrint"></iframe> 

Focus the iframe and print its content:

var pdfFrame = document.getElementById("toPrint").contentWindow;  pdfFrame.focus(); pdfFrame.print(); 

Answers 2

Try this, it includes some JS but thats always good. HTML:

<body>         <h3>MUST BE PRINTED</h3>         <p> MUST BE PRINTED</p>         <div id="pdfRenderer"></div>         <h3>MUST BE PRINTED</h3>         <p> MUST BE PRINTED</p>     </body> 

JS:

var pdf = new PDFObject({   url: "https://nett.umich.edu/sites/default/files/docs/pdf_files_scan_create_reducefilesize.pdf",   id: "pdfRendered",   pdfOpenParams: {     view: "FitH"   } }).embed("pdfRenderer"); 

This should work. Let me now if i doesnt

Answers 3

If you can use an object tag, then this article: http://forums.asp.net/t/1926965.aspx?how+to+print+embedded+PDF+file+using+javascript+which+works+in+all+browsers+ may be useful to you.

Answers 4

Use https://github.com/itext/itextsharp itextsharp or https://github.com/MrRio/jsPDF jsPDF. It is easy to use by plugin

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment