Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to convert HTML (having javascript ) to PDF. How can I do that?

I just want to show what is being shown in web page. I am displaying a gantt chart that is generated by a javascript library.

Now I want to save that HTML web page in PDF, how to do that?

share|improve this question
4  
Java is completely different from Javascript! – Ikke Nov 6 '09 at 8:50
i know , i want solution using java or javascript. both are acceptable for me. – g.revolution Nov 6 '09 at 8:55
Some questions: Is Java already present in the project? How Javascript does this chart (images, table, ...) and where the data come from? I think your question won't have a direct solution. – sinuhepop Nov 6 '09 at 9:04

6 Answers

up vote 4 down vote accepted

We are also looking for some way to convert html files with complex javascript to pdf. The javasript in our files contains document.write and DOM manipulation.

We have tried using a combination of HtmlUnit to parse the files and Flying Saucer to render to pdf but the results are not satisfactory enough. It works, but in our case the pdf is not close enough to what the user wants.

If you want to try this out, here is a code snippet to convert a local html file to pdf.

URL url = new File("test.html").toURI().toURL();
WebClient webClient = new WebClient(); 
HtmlPage page = webClient.getPage(url);

OutputStream os = null;
try{
   os = new FileOutputStream("test.pdf");

   ITextRenderer renderer = new ITextRenderer();
   renderer.setDocument(page,url.toString());
   renderer.layout();
   renderer.createPDF(os);
} finally{
   if(os != null) os.close();
}
share|improve this answer
your code rocks... but as you have mentioned it is not 100% fine... so, did you find something new to enhance the quality of pdf ? – Prabhat Subedi Mar 4 at 2:39

You can use iText for Java to convert to PDF.

http://stackoverflow.com/questions/235851/using-itext-to-convert-html-to-pdf

I have used iText a lot and it's very easy and you learn it quick.

share|improve this answer
1  
It's not free for commercial – Odelya Oct 26 '11 at 16:23
@Odelya well it is if you can put up with the AGPL :-) – Pointy Jun 23 '12 at 19:14

Copy and paste this in your site to provide a link which will convert the page to a PDF page.

<a href="javascript:void(window.open('http://www.htmltopdfconverter.net/?convert='+window.location))">Convert To PDF</a>
share|improve this answer
1  
doesn't work anymore. – ronnieaka Sep 25 '12 at 6:10

Use a browser widget (like the one from SWT) to render the chart as an image, and then use iText to create PDF from that.

share|improve this answer

Using the browser's Print... menu item, you can utilize a PDF Printer Driver, like PDFCreator. This way any JavaScript included in the page is processed by the browser when the page is rendered.

PDFCreator is a free tool to create PDF files from nearly any Windows application.

  • Create PDFs from any program that is able to print
share|improve this answer

With Docmosis or JODReports you could feed your HTML and Javascript to the document render process which could produce PDF or doc or other formats. The conversion underneath is performed by OpenOffice so results will be dependent on the OpenOffice import filters. You can try manually by saving your web page to a file, then loading with OpenOffice - if that looks good enough, then these tools will be able to give you the same result as a PDF.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.