Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java setResponseType and PDF files

Posted on 2001-07-12
12
Medium Priority
?
8,455 Views
Last Modified: 2013-11-24
I am using a PDF generation library by the ame of iText: http://www.lowagie.com/iText/

What I am trying to do is generate the PDF document to a servlets output stream, thus going directly to the browser window without being stored on disk.

I wanted to know if anybody out there has experienced this.

My code is as follows (DownloadPDF.jsp):

<%@ page contentType="application/pdf" %>
<%
  try {
    // Create the document
    com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.A4, 10, 10, 10, 36);
    ServletOutputStream outs = response.getOutputStream();
    com.lowagie.text.pdf.PdfWriter.getInstance(document, outs);
 
    // Open the document
    document.open();
   
    // Add some text to document
    document.add(new com.lowagie.text.Paragraph("Hello World!"));
   
    // Close the document
    document.close();
    outs.close();
  }
  catch (Exception ex) {
    out.println("An exception has occurred: " + ex.toString());
  }
%>

Thank you

Soroush

0
Comment
Question by:Soroush
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6277773
listening...
0
 

Author Comment

by:Soroush
ID: 6277801
Sorry, the whole point was that all IE5.5 did was load Acrobat and then display a white screen in the IE window. You cannot view source, or save/save as from the file menu. When vieing the properties from the file menu it says that its an Adobe Acrobat File.

No matter what I try I cant get the actual file to display on screen.
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6278529
Hi,

I'm not a JSP/Servlet expert, so can easily be wrong. It seems that you shouldn't close outs, just flush it or use Responce specific method to flush buffer. BTW, I wouldn't use JSP for such taks, Servlet would not require much more work.

Regards,
Igor Bazarny
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:superschlonz
ID: 6278645
You could make a zip file with the pdf file in it.
0
 
LVL 4

Accepted Solution

by:
sdussinger earned 800 total points
ID: 6278808
It turns out that closing the response output stream is not the best thing to do.  The problem is actually two-fold. In your code you're closing the output stream yourself explicitly. In addition, according to the documentation for lowagie's iText, the document.close () method also closes the streams that were associated with all of the document's writers.  Therefore, when you close the document, the response output stream is getting closed, anyway.

I got around this problem by using a temporary output stream (a ByteArrayOutputStream) for iText to write into. Once the document is closed, we can write out the bytes in the ByteArrayOutputStream to the actual response output stream.

Since I'm not expert in JSPs, I've implemented a small test program as an HttpServlet. The body of the doGet method should be usable in the body of your JSP, as long as you import the appropriate jar files into your page.

------------Code Snippet ---------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.lowagie.text.*;
import com.lowagie.text.html.*;
import com.lowagie.text.pdf.*;

public class PDFServlet extends HttpServlet {

    public void doGet(HttpServletRequest req,
              HttpServletResponse res) throws IOException, ServletException {
        // Specify content type.
        res.setContentType("application/pdf");
        // Access the output stream.
        ByteArrayOutputStream out = new ByteArrayOutputStream ();
        // Return PDF File.

    Document document = new Document (PageSize.A4, 50,50,50,50);

    try {
        // Create the PDF Writer instance
        // Notice that we're actually writing to a temporary
        // ByteArrayOutputStream here. That way when iText
        // closes the stream, we don't have problems. We can still
        // extract the bytes written to the temporary stream and write
        // them to the actual response output stream...
        PdfWriter.getInstance(document, out);

        // Open the document and add some text to it.
        document.open();
        document.add(new Paragraph("Hello world. This is a test."));
    }
    catch(DocumentException de) {
        System.err.println(de.getMessage());
    }

    // Close the document. This also closes the PDFWriter's output stream
    // which is currently pointing to our temporary ByteArrayOutputStream.
    document.close();

    // Now, we can get the bytes written into the ByteArrayOutputStream
    // and write them out to the response output stream.
    res.getOutputStream ().write (out.toByteArray ());
    }

}

------------- End of Code Snippet ------------------------

Hopefully this will solve your problem...

--Steve
0
 

Author Comment

by:Soroush
ID: 6279954
That is fantastic! Thank you very much.

Soroush
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6281054
Steve -- i copied your above code and all i get is a white screen... any ideas?  I'll post a question to give you points if you can help...

thanks
0
 

Author Comment

by:Soroush
ID: 6281091
My version used a JSP with very similiar code to the above. I have now found that when putting just a table on the PDF page, sometimes a white screen is all I get.
0
 
LVL 4

Expert Comment

by:sdussinger
ID: 6281100
Are you getting the acrobat reader when you connect to the servlet? Or are you just getting a blank screen from your browser? What browser are you using?

Send me some details and I'll see what I can do...
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6281113
blank screen... ie 5

i'm running apache and tomcat...  I think the problem is i have to set up apache to byte serve pdf files.  But i don't know how. ;)
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6281263
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6281328
Actually, forget it, i figured it out...

it's a bug with the i.e. plugin... all i have to do is set the bytearray size.

however, thanks for the help, i'll post a question entitled 'points for sdussinger'
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question