Solved

HOW TO GENERATE pdf FILE DYNAMICALLY USING ITEXT

Posted on 2006-10-25
4
253 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

860 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