简体   繁体   中英

How to convert dynamic html to pdf?

I want to convert dynamic html to pdf. Following code show the conversion of static html to pdf:

Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("d:/sample/pdfaskkea.pdf"));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("webcontent/jsp/index.jsp"), null); 
        
//        XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("C:\\pdf_table1.html"), null);
        //step 5
        
        
         document.close();
 
        System.out.println( "PDF Created!" );

From your question it is not clear, what you mean under "dynamic HTML".

If it is an HTML dynamically created with JSP, for example, PD4ML offers a JSP custom tag library - you only need to surround your code with and to output PDF instead of HTML.

If under dynamic HTML you mean JavaScript-rich HTML pages, I would recommend to take a look at PhantomJS , which can convert HTMLs also built on-a-fly with JavaScript. PhantomJS is a native standalone application, based on WebKit.

You can use itext pdf library to convert html into rich PDF files. To generate dynamic HTML content you can use a template library like thymeleaf.

I have a detailed article about generating PDF files with thymeleaf in a spring boot application if you are interested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM