views:

3516

answers:

7

Hi, i want to convert HTML (having javascript ) to PDF..

how i can do that ??

i just want to show what is being shown in web page ... like my scenerio is . i am displaying a gantt chart that is generated by javascript library .. now i want to save that HTML web page in PDF ... how to do that ????

+2  A: 

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.

Trick
A: 

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
gimel
A: 

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.

Aaron Digulla
A: 

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();
}
Serhii
A: 

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.

jowierun
+1  A: 

Javascript Libs:

Przemyslaw