convert HTML ( having Javascript ) to PDF using JavaScript

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?


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

You can use iText for Java to convert to PDF.

Using itext to convert HTML to PDF

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


使用浏览器小部件(如SWT中的小部件)将图表呈现为图像,然后使用iText从中创建PDF。

链接地址: http://www.djcxy.com/p/66836.html

上一篇: 如何在JavaScript中将浮点数转换为整数?

下一篇: 使用JavaScript将HTML(使用Javascript)转换为PDF