Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 63881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:42:09+00:00 2026-05-10T18:42:09+00:00

EDIT: See my working code in the answers below. In brief: I have a

  • 0

EDIT: See my working code in the answers below.


In brief: 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: File does not begin with ‘%PDF-‘.

In detail: 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: File does not begin with ‘%PDF-‘. 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?

To create the PDF, I’m using Apache FOP. 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 basic usage pattern and this example code.

Here’s my JSP file:

<%@ taglib uri='utilTLD' prefix='util' %> <%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c' %> <%@ taglib uri='http://java.sun.com/jsp/jstl/xml' prefix='x' %> <%@ page language='java' session='false' %> <%@ page contentType='application/pdf' %>  <%-- Construct and initialise the PrintReportsBean --%> <jsp:useBean id='printReportsBean' scope='request' class='some.package.printreports.PrintReportsBean' /> <jsp:setProperty name='printReportsBean' property='*'/>  <c:set scope='page' var='xml' value='${printReportsBean.download}'/> 

Here’s my Java Bean method:

//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());          }     } } 

I realise that this is a very specific problem, but any help would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:42:10+00:00Added an answer on May 10, 2026 at 6:42 pm

    The way I have implemented this type of feature in the past is to make a servlet write the contents of the PDF file out to the response as a stream. I don’t have the source code with me any longer (and it’s been at least a year since I did any servlet/jsp work), but here is what you might want to try:

    In a servlet, get a handle on the response’s output stream. Change the mime type of the response to ‘application/pdf’, and have the servlet do the file handling you have in your example. Only, instead of returning the File object, have the servlet write the file to the output stream. See examples of file i/o and just replace any outfile.write(…) lines with responseStream.write(…) and you should be set to go. Once you flush and close the output stream, and do the return, if I remember correctly, the browser should be able to pick up the pdf from the response.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 52k
  • Answers 52k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I believe you can only add variables to the Watch… May 11, 2026 at 6:38 am
  • added an answer As timheuer said, [Scriptable] on your Silverlight methods. Call this… May 11, 2026 at 6:38 am
  • added an answer Actually, I think I got it: you're only seeing a… May 11, 2026 at 6:38 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.