Solved

HOW TO GENERATE pdf FILE DYNAMICALLY USING ITEXT

Posted on 2006-10-25
4
247 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
Comment Utility
Try
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=name.pdf");
0
 

Author Comment

by:huzefaq
Comment Utility
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
Comment Utility
baos.writeTo(out); should be called after setting all headers
0
 
LVL 10

Expert Comment

by:jaggernat
Comment Utility
as hoomanv said

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

763 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

12 Experts available now in Live!

Get 1:1 Help Now