Search code examples
jsppdfprintingapache-fop

Printing PDF in jsp in iframe contents


I have researched several options for printing a pdf inside an iframe and none seem to be working.

Simple Details:

  1. I get some search parameters from a user.
  2. I do a database search and then use Apachi FOP to generate a PDF of the results.
  3. They are directed to a webpage that has a print and cancel button.
  4. When the user clicks the Print button, it will open a window displaying the PDF.
  5. A print dialog will open up for the user to print the PDF.
  6. The PDF file is deleted from the server.
  7. The windows close

Advanced Details:

  • This only needs to work on IE8.
  • The FOP integration does not use any XSLT translations. It only uses a StringReader of the inputted FOP XML formatted as a string
  • The window displaying the PDF is actually two JSP pages.
  • The first page:
    • Has an iframe with the second JSP page as a source
    • Runs a printPDF() function on load that prints the PDF in the iframe
  • The second page:
  • Uses Java BufferedOutputStream and ServletOutputStream
    • Will delete the file after outputting
    • Uses out = pageContent.pushBody();

Here is part of the first jsp page (the run that calls the print function):

<body onload='printPDF()'>
<table>
    <tr>
        <td class="content">
            <%
            // get myfilename from the myfile parameter on the URL
            String myfile = request.getParameter("myfile");
            out.print("<iframe src='fc_view_letter.jsp?myfile="+ myfile + "' id='pdfFrame'></iframe>");
            %>
        </td>
    </tr>
</table>
<script>
    function printPDF()
    {
        var id = 'pdfFrame';
        var iframe = document.frames ? document.frames[0] : document.getElementById(id);
        var ifWin = iframe.contentWindow || iframe;

        ifWin.focus();
        ifWin.printPage();
        //ifWin.print();
    }
</script>
</body>

Here is most of the second JSP Page (the one that shows the pdf):

<%@ page session="false" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.URLDecoder" %>
<html>
<head>
</head>
<body>
<%
String myfile = request.getParameter("myfile");
String myfiledecoded = "";
myfiledecoded = URLDecoder.decode(myfile, "UTF8");
String myfilename = myfiledecoded;
String extension;
int dotPos = myfilename.lastIndexOf(".")+1;
extension = myfilename.substring(dotPos);
int slashPos = myfilename.lastIndexOf("/")+1;
String secondparam = "filename=" + myfiledecoded.substring(slashPos);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", secondparam);
try {
        ServletOutputStream sout = response.getOutputStream();
        response.setHeader("Content-Disposition", secondparam);

        File  file  = new File(myfilename);
        FileInputStream fstream = new FileInputStream(file);

        BufferedInputStream bis = null;
        bis = new BufferedInputStream(fstream);


        BufferedOutputStream bos = null;
        bos = new BufferedOutputStream(sout);

        byte[] buff = new byte[1024];
        int bytesRead;

        while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
             bos.write(buff, 0, bytesRead);
        }

         bis.close();
         bos.close();

         sout.flush();
         sout.close();

         //file.delete();

}
catch (Exception e)  {    
 System.out.println("Exception Occured...................." );
}
   out.clear();
    out = pageContext.pushBody();
%>
</body>
</html>

What I think is the issue: I'm thinking that the buffer eliminates all of the html and only displays the PDF. Or at least it does that in IE. When I looked in Firefox it embedded the PDF file. Maybe I cannot grab the contents of the iframe because it is no longer HTML.

Here are my sources so far:

Javascript Print iframe contents only

How to open print dialog after pdf generated?

http://www.ehow.com/how_7352227_use-javascript-print-pdf.html

http://www.webmasterworld.com/forum91/4086.htm

how to print pdf inside embed/iframe using javascript

Printing contents of a dynamically created iframe from parent window


Solution

  • What I ended up doing was generating the PDF then using iText I added some print JavaScript that ran when the PDF loaded