• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Generating PDF from jsp using iText

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All
I am trying to generate PDF from jsp and i am using iText library.
Here i am pasting a sample code.
<%@ page contentType="text/html"
import="java.io.*,
java.util.*,
java.text.*,
java.sql.*,
com.sybase.jdbcx.EedInfo,
com.sybase.jdbcx.SybDriver,
com.sybase.jdbcx.Debug,
com.lowagie.text.*,
com.lowagie.text.pdf.*"
%>
<html>
<body bgcolor="white">
<%
Document document = new Document(PageSize.A4.rotate(),
10, 10, 10, 10);
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
%>
HelloWorld
</body>
</html>
its giving an exception saying that
org.apache.jasper.JasperException: getOutputStream() has already been called for this response
Please help me.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't call response.setContentType(...) after you have written something to the output stream. Besides, it won't do you much good if you're embedding the PDF file's contents to the HTML document's <body> section, I'm afraid.
 
Radha MahaLakshmi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
How to avoild this.If i dont call response.setContentType() method then is there nay other way i can direct the browser as its a PDF file.
Please help me.
Radha
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short, remove all HTML stuff from your JSP, set the content type to "application/pdf" and use the iText API to print out the PDF file.
 
Radha MahaLakshmi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I removed every thing and tried.Its not giving any error but not displaying the pdf also.
Here is my code
<%@ page contentType="text/html;charset=ISO-8859-1"
import="java.io.*,
com.lowagie.text.*,
com.lowagie.text.pdf.*"
%>
<%!
private void addCell(PdfPTable table, String str,int verAlig,
int holAlig,int colSpan,Font font)
{
if (str == null) str = "";
PdfPCell cell = new PdfPCell(new Phrase(str, font));
cell.setVerticalAlignment(verAlig);
cell.setHorizontalAlignment(holAlig);
cell.setColspan(colSpan);
cell.setBorderWidth(0);//Border arround the heading
table.addCell(cell);
}
%>
<%
try
{
Document document = new Document(PageSize.A4.rotate(),10, 10, 10, 10);
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
PdfPTable headerTable = new PdfPTable(1);
headerTable.setWidthPercentage(100);
headerTable.getDefaultCell().setBorderWidth(3);

Font f1 = new Font(Font.HELVETICA, 22,Font.UNDEFINED,
java.awt.Color.blue);
addCell(headerTable, "HelloWorld",
Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,0,f1);
document.add(headerTable);
document.close();

}catch(Exception e)
{
System.err.println(e.getMessage());
e.printStackTrace();
}
%>

Also i noticed.
I added <html></html> tags but still its not giving any error.
I am not able to find any reason.
Regards
Radha
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should at least remove <%@ page contentType="text/html;charset=ISO-8859-1" from the beginning of the JSP (or change it to "application/pdf"). If this is still not working, you might want to switch into a servlet (the JSP compiler might produce some nasty out.println's which do no good for PDF documents considering the document structure relies on byte offsets).
Here is an example for a servlet generating PDF output and you might also want to take a look at this faq entry.
 
author & internet detective
Posts: 42164
937
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, don't forget to flush the output stream.
 
Radha MahaLakshmi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I tried with servle but nothing is getting displayed on the screen.
Browser window is gettng hanged.There is no exception also.
Is there any other way i can do??
Here is my code

import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.html.HtmlWriter;
public class Chap0105 extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// we retrieve the presentationtype
String presentationtype = "pdf";
// step 1
System.out.println("---inside servelit----");
Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
try {
// step 2: we set the ContentType and create an instance of the corresponding Writer
if ("pdf".equals(presentationtype)) {
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
}
else if ("html".equals(presentationtype)) {
response.setContentType("text/html");
HtmlWriter.getInstance(document, response.getOutputStream());
}
else {
response.sendRedirect("http://www.lowagie.com/iText/tutorial/ch01.html#step2");
}
// step 3
document.open();
// step 4
document.add(new Paragraph(new Date().toString()));
}
catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: we close the document (the outputstream is also closed internally)
document.close();
}
}
Regards
Radha
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basic idea is there is no good way to write binary data such as a PDF or an image from a JSP. Just go ahead and learn a little bit about servlets, they don't bite.
Bill
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radha!
I don't think you are sending the pdf document. you are creating the pdf. But you should a stream object like ServletOutputStream to send the generated pdf.
Plz go thru the following article on Dynamically Creating PDFs in a Web Application first.
http://www.onjava.com/lpt/a/3924
Remember that one should use streams (not readers/writers) while dealing with bytes. Use readers/writers when you deal with chars.
 
Radha MahaLakshmi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Murthy Narasimha
Thank you for the link.I have been struggling with this since 2 days.
The given example worked perfectly.Now i have to do my application accordingly.
Anyway thanks for the help.
Regards
Radha
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic