1

I have been searching for days for a solution to this problem.

Description : I have a website which loads a PDF dynamically via an iFrame. The PDF is saved on the server and the user of the website can view the pdf on the website.

Problem : Introduce a Print button on website which prints the PDF which was created dynamically and saved on the server.

Is this even possible ? I am looking at a cross-browser implementation as well to make things worse. I have tried n number of JS options from the web but none of them seem to work. I can not seem to get the PDF printed in the same way as it looks. To put it short, I am trying to emulate the print button which appears on the PDF when it is loaded. Is there an option to pass the pdf document from the server to the print dialog box ?

steve mais
  • 53
  • 4
  • If you have the pdf already saved on the server and looking like you want it to you can provide a regular link to it that will take the user to the pdf. It would be easy to print it from there. However, if you're hoping for a 'one click and it's coming out of the printer' solution the bad news is there isn't one. Related: http://stackoverflow.com/questions/5627035/how-to-print-the-contents-of-an-external-file-in-php – lostphilosopher Jul 17 '13 at 20:02
  • I'm assuming you've already determined that this (http://www.javascriptkit.com/howto/newtech2.shtml) doesn't work for you? – lostphilosopher Jul 17 '13 at 20:07
  • > Is there an option to pass the pdf document from the server to the print dialog box ? ... of course not, no more than you want anybody else to be able to blindly send something onto your machine and do something. – Kevin Brown Jul 17 '13 at 23:49
  • @lost philosopher - window.print prints the entire web page along with a blank iframe. – steve mais Jul 18 '13 at 06:48

2 Answers2

2

Description : I have a website which loads a PDF dynamically via an iFrame. The PDF is saved on the server and the user of the website can view the pdf on the website.

Problem : Introduce a Print button on website which prints the PDF which was created dynamically and saved on the server.

Solution : I could not find an exact solution to this problem, but here is how I solved the problem -

  1. Create the 'Print' as per req and redirect that to another page which has only the PDF.
  2. Copy the previous PDF & Create new PDF with JS - this.print() such that when it opens up, the print dialog pops up directly to the user.

In the new page -

    if ("Location of PDF " != null)
        {
            sPdf = "Location of PDF ";
            PdfReader pReader = new PdfReader(sPdf);
            Document document = new Document
                (pReader.GetPageSizeWithRotation(ApplicationConstants.INDEX_ONE));
            int n = pReader.NumberOfPages;
            FileStream fs = new FileStream
                ("New PDF location",
                FileMode.Create, FileAccess.Write);
            PdfCopy copy = new PdfCopy(document, fs);
            // Write to pdf 
            document.Open();
            for (int i = ApplicationConstants.INDEX_ONE; i <= n; i++)
            {
                PdfImportedPage page = copy.GetImportedPage(pReader, i);
                copy.AddPage(page);
            }
            copy.AddJavaScript("this.print(true);", true);
            document.Close();
            pReader.Close();

            inStr = File.OpenRead("New PDF location");
            while ((bytecnt = inStr.Read
                (buffer, ApplicationConstants.INDEX_ZERO, buffer.Length))
                > ApplicationConstants.INDEX_ZERO)
            {
                if (Context.Response.IsClientConnected)
                {
                    Context.Response.ContentType = "application/PDF";
                    Context.Response.OutputStream.Write(buffer, 
                        ApplicationConstants.INDEX_ZERO, buffer.Length);
                    Context.Response.Flush();
                }
            } 
        }

Please note that I am using itextsharp to inject the JS script into the new PDF. Hope this helps someone else. I am trying to find another solution without the usage of itextsharp or any other dll but this will have to do for now.

steve mais
  • 53
  • 4
1

I am not sure if this will work, but you could try launching a popup window with a special version of your PDF file that opens the print dialog when opened. Then close the popup afterwards. This last part might be tricky since I think there is no clean way to know if the print dialog has been closed.

Community
  • 1
  • 1
yms
  • 10,361
  • 3
  • 38
  • 68
  • Thanks yms. I ended up doing something similar. – steve mais Jul 29 '13 at 11:29
  • Note : I have found it almost impossible to display a print dialog from PDF embedded in an iFrame inside a web page. My solution was to open up another window containing only the PDF when user clicks on 'Print' button. Following link helped - http://stackoverflow.com/questions/270674/print-pdf-from-asp-net-without-preview – steve mais Jul 30 '13 at 07:03
  • @steve mais, maybe you should write a new answer to your own question with all the details of what you did. It may help others facing the same problem. – yms Jul 30 '13 at 13:43