Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a PDF file from a Java Bean to a JSP
    primarykey
    data
    text
    <p><strong>EDIT:</strong> See <a href="https://stackoverflow.com/questions/218909/returning-a-pdf-file-from-a-java-bean-to-a-jsp#221252">my working code</a> in the answers below.</p> <hr> <p><strong>In brief:</strong> I have a JSP file which calls a method in a Java Bean. This method creates a PDF file and in theory, returns it to the JSP so that the user can download it. However, upon loading the PDF, Adobe Reader gives the error: <em>File does not begin with '%PDF-'</em>.</p> <p><strong>In detail:</strong> So far, the JSP successfully calls the method, the PDF is created and then the JSP appears to give the user the finished PDF file. However, as soon as Adobe Reader tries to open the PDF file, it gives an error: <em>File does not begin with '%PDF-'</em>. Just for good measure, I have the method create the PDF on my Desktop so that I can check it; when I open it normally within Windows is appears fine. So why is the output from the JSP different?</p> <p>To create the PDF, I'm using <a href="http://xmlgraphics.apache.org/fop" rel="nofollow noreferrer">Apache FOP</a>. I'm following one of their most basic examples, with the exception of passing the resulting PDF to a JSP instead of simply saving it to the local machine. I have been following their <a href="http://xmlgraphics.apache.org/fop/0.95/embedding.html#basics" rel="nofollow noreferrer">basic usage pattern</a> and <a href="http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleFO2PDF.java?view=markup" rel="nofollow noreferrer">this example code</a>.</p> <p>Here's my JSP file:</p> <pre><code>&lt;%@ taglib uri="utilTLD" prefix="util" %&gt; &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt; &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %&gt; &lt;%@ page language="java" session="false" %&gt; &lt;%@ page contentType="application/pdf" %&gt; &lt;%-- Construct and initialise the PrintReportsBean --%&gt; &lt;jsp:useBean id="printReportsBean" scope="request" class="some.package.printreports.PrintReportsBean" /&gt; &lt;jsp:setProperty name="printReportsBean" property="*"/&gt; &lt;c:set scope="page" var="xml" value="${printReportsBean.download}"/&gt; </code></pre> <p>Here's my Java Bean method:</p> <pre><code>//earlier in the class... private static FopFactory fopFactory = FopFactory.newInstance(); public File getDownload() throws UtilException { OutputStream out = null; File pdf = new File("C:\\documents and settings\\me\\Desktop\\HelloWorld.pdf"); File fo = new File("C:\\somedirectory", "HelloWorld.fo"); try { FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); out = new FileOutputStream(pdf); out = new BufferedOutputStream(out); Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); //identity transformer Source src = new StreamSource(fo); Result res = new SAXResult(fop.getDefaultHandler()); transformer.transform(src, res); return pdf; } catch (Exception e) { throw new UtilException("Could not get download. Msg = "+e.getMessage()); } finally { try { out.close(); } catch (IOException io) { throw new UtilException("Could not close OutputStream. Msg = "+io.getMessage()); } } } </code></pre> <p>I realise that this is a very specific problem, but any help would be much appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload