Solved

HOW TO GENERATE pdf FILE DYNAMICALLY USING ITEXT

Posted on 2006-10-25
4
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…
Suggested Courses

739 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