Solved

HOW TO GENERATE pdf FILE DYNAMICALLY USING ITEXT

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

628 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