1

I would like to be able to print a PDF document automatically when a user clicks on a print button. Currently what the way I am doing it is I render the PDF and save to the server disk and have it appear in an iframe then I tried to print the content of the iframe using javascript:print(). however what is printed is an empty html page.

I am doing this because using the norm HTML print is wrecking the layout of the webpage i am trying to print. so i'm rendering the page to a pdf format to print the webpage. i don't want the users to be able to save the pdf hence i am trying to slient print the pdf page. hence i am loading it in an iframe by changing the src in the code behind and re-rendering the page and then triggering the js script.

function printPDF(){ document.iframe_printArea.focus(); document.iframe_printArea.print(); }

I am wondering if it is possible to print a pdf document loaded in an iframe using print() or whether this is even possible. I have extensively googled on this and have yet to come up with any solutions that works for a web application. Most of the resources are devoted to C# windows app. The platform I am using is .NET C#.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    Whoever is making you support IE6 needs to know they are supporting a dying cause. http://www.ie6countdown.com/ – epascarello Nov 07 '11 at 01:02
  • possible duplicate of [How to print a PDF from the browser](http://stackoverflow.com/questions/205180/how-to-print-a-pdf-from-the-browser) – epascarello Nov 07 '11 at 01:05
  • The print method of the browser does not have access to the content displayed by the PDF plugin. So, this will not print the PDF. It is not possible to directly print from a web page to a printer on the client because that breaks the [browser security model](http://www.gnostice.com/nl_article.asp?id=145&t=Limitations_of_Web_Applications). The best you can do is to create an [auto-print PDF document](http://www.gnostice.com/nl_article.asp?id=157&t=Create_an_Auto-Print_PDF) that when displayed in Adobe Reader (plugin or standalone application) will automatically trigger a Print dialog prompt. – BZ1 Nov 07 '11 at 04:58

1 Answers1

0

First of all I'm very sorry for whom that have to deal with IE6.

There is an non-standard DOM event developed by Microsoft that fire before print. It's onbeforeprint event (docs). What you can do is hiding everything but the iframe and shrink the iframe to the window size before print. and after print reverse the document to normal statues with onafterprint event.

function window.onbeforeprint()
{
    // hide other elements and shrink the iframe
}

function window.onafterprint()
{
   // unde what heppened onbeforeprint
}
Mohsen
  • 64,437
  • 34
  • 159
  • 186
  • I can make an example fiddle if you want more help – Mohsen Nov 07 '11 at 01:08
  • Why would you do this? You print the iframe or have the pdf set up to print automatically. Plus it will not print 100% of the iframe height, only the visible portion. – epascarello Nov 07 '11 at 01:14
  • because if you redirect to the PDF file url then you miss your javascript behind! Also I mean shrink the iframe `height` to it's `document.scrollHeight` not `100%`. – Mohsen Nov 07 '11 at 01:17
  • or you can do document.frames[0].print() with no issues other than loading time,just like the dupe link says along with other suggestions. – epascarello Nov 07 '11 at 01:21
  • Hi all, i am doing this because using the norm HTML print is wrecking the layout of the webpage i am trying to print. so i'm rendering the page to a pdf format to print the webpage. i don't want the users to be able to save the pdf hence i am trying to slient print the pdf page. – user1032968 Nov 07 '11 at 01:54
  • So how my Answer was useful so far? – Mohsen Nov 07 '11 at 03:43
  • Hi Mohsen, i tried using the code and it is still not working – user1032968 Nov 07 '11 at 05:04