File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Generating PDF from jsp using iText Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Generating PDF from jsp using iText" Watch "Generating PDF from jsp using iText" New topic
Author

Generating PDF from jsp using iText

Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
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.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Mar 28, 2003
Posts: 45
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Also, don't forget to flush the output stream.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Radha MahaLakshmi
Ranch Hand

Joined: Mar 28, 2003
Posts: 45
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
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

Joined: Mar 28, 2003
Posts: 45
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generating PDF from jsp using iText