Second Menu
Sample table creation in PDF using Servlet
Required Jar:
Download Itext 1.3 jar
Servlet Code:
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class PDFServlet extends HttpServlet
{
public PDFServlet()
{
super();
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws javax.servlet.ServletException, java.io.IOException
{
DocumentException ex = null;
ByteArrayOutputStream baosPDF = null;
try
{
baosPDF = generatePDFDocumentBytes(req, this.getServletContext());
StringBuffer sbFilename = new StringBuffer();
sbFilename.append("filename_");
sbFilename.append(System.currentTimeMillis());
sbFilename.append(".pdf");
resp.setHeader("Cache-Control", "max-age=30");
resp.setContentType("application/pdf");
StringBuffer sbContentDispValue = new StringBuffer();
sbContentDispValue.append("inline");
sbContentDispValue.append("; filename=");
sbContentDispValue.append(sbFilename);
resp.setHeader(
"Content-disposition",
sbContentDispValue.toString());
resp.setContentLength(baosPDF.size());
ServletOutputStream sos;
sos = resp.getOutputStream();
baosPDF.writeTo(sos);
sos.flush();
}
catch (DocumentException dex)
{
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println(
this.getClass().getName()
+ " caught an exception: "
+ dex.getClass().getName()
+ "<br>");
writer.println("<pre>");
dex.printStackTrace(writer);
writer.println("</pre>");
}
finally
{
if (baosPDF != null)
{
baosPDF.reset();
}
}
}
protected ByteArrayOutputStream generatePDFDocumentBytes(
final HttpServletRequest req,
final ServletContext ctx)
throws DocumentException
{
Document doc = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter docWriter = null;
try
{
docWriter = PdfWriter.getInstance(doc, baosPDF);
doc.addAuthor(this.getClass().getName());
doc.addCreationDate();
doc.addProducer();
doc.addCreator(this.getClass().getName());
doc.addTitle("This is a title.");
doc.addKeywords("pdf, itext, Java, open source, http");
doc.setPageSize(PageSize.LETTER);
HeaderFooter footer = new HeaderFooter(
new Phrase("Created by Ganesh Rengarajan."),
false);
doc.setFooter(footer);
doc.open();
doc.add(new Paragraph(
"Sample table in PDF using Web Application(Servlet)"));
doc.add( makeGeneralRequestDetailsElement(req) );
}
catch (DocumentException dex)
{
baosPDF.reset();
throw dex;
}
finally
{
if (doc != null)
{
doc.close();
}
if (docWriter != null)
{
docWriter.close();
}
}
if (baosPDF.size() < 1)
{
throw new DocumentException(
"document has "
+ baosPDF.size()
+ " bytes");
}
return baosPDF;
}
protected Element makeGeneralRequestDetailsElement(
final HttpServletRequest req)
{
ArrayList l=new ArrayList();
l.add("Name");
l.add("ganesh");
l.add("Age");
l.add("23");
l.add("Gender");
l.add("Male");
l.add("Date");
l.add("23/02/12");
l.add("Seat No");
l.add("07");
l.add("From");
l.add("Mayiladuthurai");
l.add("To");
l.add("Chennai");
l.add("Ticket Fare");
l.add("240");
Table tab = null;
tab = makeTableFromMap(l);
return (Element) tab;
}
private static Table makeTableFromMap(
final java.util.ArrayList m)
{
Table tab = null;
try
{
tab = new Table(2 /* columns */);
}
catch (BadElementException ex)
{
throw new RuntimeException(ex);
}
tab.setBorderWidth(1.0f);
tab.setPadding(5);
tab.setSpacing(5);
tab.endHeaders();
if (m.size() == 0)
{
Cell c = new Cell("none");
c.setColspan(tab.columns());
tab.addCell(c);
}
else
{
Iterator i = m.iterator();
String strName=null;
while (i.hasNext())
{
strName=i.next().toString();
tab.addCell(new Cell(strName));
}
}
return tab;
}
}
Subscribe to:
Posts (Atom)
About Me
Live Traffic
Labels
- Android (2)
- Autocomplete using jquery and Mysql Database (1)
- Create Table in Java Swing (1)
- email using java code (1)
- Excel Creation using java (2)
- How to create hibernate in netbeans (1)
- How to create stored procedure in java (1)
- JASPER REPORT USING JAVA (1)
- jquery (2)
- JSTL Tags (1)
- Listview with AlertBox in Android (1)
- multiple jquery version in jsp page (1)
- MYSQL QUERY (2)
- Oracle Database Connection for Java (1)
- Sample table creation in PDF (1)
- Simple WebApplication Using Spring (1)
- Swing Look and Feel (1)
- Way2SMS Java API 2012 (1)
Popular Posts
-
STEP 1: Download ireport plugin for Netbeans http://plugins.netbeans.org/plugin/4425/ireport STEP 2: Install plugin in Netbe...
-
Download Link for Look and Feel Jar: Download Double Click and open nimrodlf-1.2.jar and... Save NimRODThemeFile in your ...
-
DML SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSE...
-
By using below template you can get initial capital letter for multiple word. <xsl:call-template name="ginitcap"> ...
-
Requirement: Required javascript and css file Download Here Required jsp files see below code update.jsp <html> <head...
-
Download the database and jar file here download 1 .Create Java Project in Netbeans 2.Create JFrame form 3.Drag...
-
import java.io.IOException; import java.io.PrintWriter; import java.util.Properties; import javax.activation.DataHandler; ...
-
This Tutorial showz that how you can use the www.way2sms.com for sending message us...
-
Download js and css Sample.jsp <html> <head> <meta http-equiv="Content-Language" content="en-us"...
-
Export data from MySQL into a CSV file,Its simple by using this query. SELECT * INTO OUTFILE '/tmp/name.csv' FIELDS TERMINATED...
Powered by Blogger.
Translate
Widgets
Followers
About Me
GANESH RENGARAJAN,
JAVA DEVELOPER,
CHENNAI,TAMILNADU,
INDIA