Conversion from HTML to PDF

Source: Internet
Author: User

Conversion from HTML to PDF
Maven dependency

<dependency>    <groupId>com.itextpdf</groupId>    <artifactId>itextpdf</artifactId>    <version>5.5.7</version></dependency><dependency>    <groupId>com.itextpdf.tool</groupId>    <artifactId>xmlworker</artifactId>    <version>5.5.7</version></dependency>
Class Diagram and API

Write (htmlContent: java. lang. String, OS: java. io. OutputStream): void

After converting the specified html into pdf content, write it to the specified output stream.

Html2PdfServiceImpl

HTML to PDF service implementation class

      
write (htmlContent: java.lang.String, os: java.io.OutputStream): void

Implementation: Convert the specified html into pdf content and write it to the specified output stream.

getIntaceHtml (htmlContent: String): String

The private method is used to obtain the complete html document. If only html fragments are input, you need to use the template to compile them.

doWrite (htmlContent: String, os: OutputStream): void

Private method: converts a complete html document to a pdf file and writes it to the specified output stream.

Code ihtml2workflow Service
Package cn. ljl. javaweb. demo. ckeditor. service; public interface ihtml2#service {/** prefix ID of the complete html content */public static final String INTACT_FLAG = "Html2PdfServiceImpl
Package cn. ljl. javaweb. demo. ckeditor. service; import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. nio. charset. charset; import com. itextpdf. text. extends entexception; public class html2?serviceimpl implements ihtml2=service {@ Override public void write (String htmlContent, OutputStream OS) {If (htmlContent = null | htmlContent. length () = 0) return; if (OS = null) return; htmlContent = getIntaceHtml (htmlContent); doWrite (htmlContent, OS );} /*** obtain the complete html content based on the provided html content. <br> * @ param htmlContent * @ return */private String getIntaceHtml (String htmlContent) {boolean intact = htmlContent. trim (). toLowerCase (). startsWith (INTACT_FLAG); if (! Intact) {htmlContent = TEMPLATE_HTML.replaceFirst ("\\\ \\{ content \\}", htmlContent) ;}return htmlContent ;}/ *** to implement write operations. <br> * @ param htmlContent * @ param OS */private void doWrite (String htmlContent, OutputStream OS) {InputStream is = new ByteArrayInputStream (htmlContent. getBytes (); com. itextpdf. text. document document = new com. itextpdf. text. document (); com.itext;.text=. specified writer = null; try {writer = com.itext;.text=. invalid writer. getInstance (document, OS); document. open (); com. itextpdf. tool. xml. XMLWorkerHelper. getInstance (). parseXHtml (writer, document, is, Charset. forName ("gbk");} catch (incluentexception e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} writer. flush (); // SINOBEST HTML2PDF if we call writer. close (), may not generate pdf normally, or even encounter // Exception: The page 1 was requested but the document has only 0 pages. // writer. close (); document. close ();}}
Possible problems

1. Chinese character display problems

  

For a complete html document, you need to use css to set a default font for the entire document, such as the code snippet in the ihtml2workflow service template:

<style type='text/css'>body {font-family: SimSun;}</style>

2. Some content is blank

  

It may be that html documents use fonts that are not on the server. For example, if a div in an html document is set with the imitation font and is not installed on the machine, the corresponding content is blank when converted to pdf.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.