Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  16] [ 7]  / answers: 1 / hits: 131708  / 15 Years ago, fri, november 6, 2009, 12:00:00

I want to convert HTML (containing JavaScript ) to a 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 as a PDF, how to do that?


More From » html

 Answers
88

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

[#98374] Monday, November 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keyonnal

Total Points: 746
Total Questions: 103
Total Answers: 116

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
;