Solved

HOW TO GENERATE pdf FILE DYNAMICALLY USING ITEXT

Posted on 2006-10-25
4
248 Views
Last Modified: 2010-03-31
I am trying to generate a PDF file dynamically using iText (open source library).  I succeded in writing to a .pdf file which i can store in a specific directory on my computer. But I need to give the user the ability to store the file anywhere they like. I tried writing the code for that but when I run the code it doesn't show anythng. Any help will be greatly apprecited.
 
Following is the code which i am crrently working on and which shows nothing
-----------------------------------------------------------------------------------------------------------------
private void export_leadershipStaff3(HttpServletRequest request, HttpServletResponse response) {

        Document document = new Document(PageSize.A4);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, baos);
            writer.setViewerPreferences(
                    PdfWriter.AllowPrinting |
                    PdfWriter.PageLayoutSinglePage);
            document.open();
            document.add(new Paragraph("First page of the document."));
            response.setContentLength(baos.size());
            ServletOutputStream out = null;
                out = response.getOutputStream();
                baos.writeTo(out);
            out.flush();
        } catch (DocumentException ex) {
        }
        catch (IOException ex2) {
            }
    }
0
Comment
Question by:huzefaq
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
ID: 17805260
Try
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=name.pdf");
0
 

Author Comment

by:huzefaq
ID: 17805473
hoomanv I tried the following code but it still it shows a blank screen

---------------------------------------------------------------------------

    private void export_leadershipStaff2(HttpServletRequest request, HttpServletResponse response) {

        Document document = new Document(PageSize.A4);
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, baos);
            writer.setViewerPreferences(
                    PdfWriter.AllowPrinting |
                    PdfWriter.PageLayoutSinglePage);
            document.open();
            response.setContentLength(baos.size());
            ServletOutputStream out = null;
                out = response.getOutputStream();
                baos.writeTo(out);
                response.setContentType("application/pdf");
                response.setHeader("Content-Disposition","attachment; filename=name.pdf");
            out.flush();
        } catch (DocumentException ex) {
        }
        catch (IOException ex2) {
            }
    }
0
 
LVL 14

Assisted Solution

by:hoomanv
hoomanv earned 500 total points
ID: 17805536
baos.writeTo(out); should be called after setting all headers
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17806298
as hoomanv said

response.setContentType("application/pdf");
 
should work
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now