wood burning stoves 2.0*
The moose likes Struts and the fly likes Generate PDF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Generate PDF" Watch "Generate PDF" New topic

Generate PDF

Njk Naresh

Joined: Nov 16, 2006
Posts: 11
Hi folks,

I want to generate a pdf document from any html or jsp please give me a sample code.

and it will be more helpful if any one gives me a sample code how to draw a table of labels(html) and that i want to generate in pdf.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41155
There is no Java code available to create a PDF file from an HTML file, and it would be quite a task to write one (essentially you'd be duplicating an HTML renderer, only that it doesn't draw on the screen but creates PDF).

If your HTML originates from XML, you might have a chance to use FOP for this. If the HTML is generated directly by Java code, you could use iText to create both the HTML and the PDF. Both methods have drawbacks, though, and can't be used if the HTML is generated by JSP. (Links to FOP and iText are here.)
[ December 06, 2006: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Njk Naresh

Joined: Nov 16, 2006
Posts: 11
|first |second|third |
|fourth |fifth |sixth |
|seventh| eight| nine |

i want to generate a pdf as like this i,e it mean i want to draw borders as the above table shows and any header and footers
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41155
iText has extensive support for tables , and headers and footers are quite easy to achieve as well.
[ December 06, 2006: Message edited by: Ulf Dittmer ]
Ashish Tahasildar

Joined: Dec 10, 2006
Posts: 12
Hi ,
You can download this jar file (search in google) or you may get it on ibm site.


This is the sample code which will help to create PDF file. Apply your logic to make a design inside code document.add();Implement this in your action.

///Sample Code
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, baos);
BaseFont bf;
Font font;
//bf =
// BaseFont.createFont("c:/windows/fonts/msgothic.ttc,0",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
bf = BaseFont.createFont(path, BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
font = new Font(bf, 12);
document.add(new Paragraph("Your text may be html also ..", font));

ServletOutputStream out = response.getOutputStream();

Hope this will help you.
Ashish Tahasildar
I agree. Here's the link: http://aspose.com/file-tools
subject: Generate PDF
Similar Threads
How to generate thumbnail of word/excel document
PDF generation using Java.
html to pdf
Creating PDF through servlet
how to combine two different PDF's and return only byte[]