Itextrenderer (Flying Saucer) HTML to PDF

Source: Internet
Author: User



Itextrenderer on the basis of relying on IText, the HTML rendering PDF is implemented by itself, which can basically realize the integrity of CSS 2.1 and fully conform to the specifications.



Use HTML and CSS to define styles and render content. The following flowchart:











Chinese support


First you need to add a Chinese font, which is all the fonts used in your page:


ITextFontResolver fontResolver = renderer.getFontResolver();  
        fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
        fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        fontResolver.addFont("C:/Windows/Fonts/simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);





Note: fonts in the page cannot be used in Chinese, they need to be in English and are case sensitive! For example, the English name of the song body is SimSun (note not simsun! , the first letter is in uppercase)



Wrong wording: font-family: Song body or Font-family:simsun



correct wording: Font-family:simsun or Font-family:simhei



If the resulting PDF is not displayed or garbled, please confirm the following information:


    • Make sure that all content in the page is assigned a font, it is best to specify body {font-family: ...} to prevent the slip.

    • It is convenient to make sure all of the above fonts are added by AddFont, the font name is wrong or the font does not exist, but there is no prompt for fonts that are not imported.

    • Make sure the font name is correct, do not use Chinese, and is case-sensitive.

    • Make sure the HTML tags are correct, the simple way is all the content is removed, just write a few Chinese to see whether the normal generation, if you can, carefully check the HTML tags, otherwise check the above several.


There is the problem of Chinese line, with Chinese and more text there is a newline, you need to add the table style:



Table-layout:fixed, then the TD using% in the table also specifies the width of the TD.





Encryption and Permissions


The encryption method is relatively simple:


ITextRenderer renderer = new ITextRenderer();  
 renderer.setPDFEncryption(getEncryption());
 
 private PDFEncryption getEncryption()
{
    PDFEncryption encrypt = new PDFEncryption(new String("a").getBytes(), new String("b").getBytes(), PdfWriter.ALLOW_SCREENREADERS);
    return encrypt;
}


need to introduce JAR package! Bcprov-jdk16-145.jar, Baidu a lot of.



Two parameters: Two are passwords, the difference is that the first password is to browse the password, enter the password to open the PDF after the set of permissions to control, the second password belongs to the owner password, using the password to open the PDF permissions are not controlled.


multi-page generation PDF


Actually very simple, the first page does not change from the second one:


 for(int i = 1; i < inputFile.length; i++)
        {
        	renderer.setDocument(new File(root, inputFile[i]));
        	renderer.layout();
        	renderer.writeNextDocument();
        }
        renderer.finishPDF();




Label


Note that if you are generating multiple pages into a PDF, simply add the bookmark to the last page! Otherwise, it repeats.






Pages generate landscape-oriented PDFs




<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="****.css" rel="stylesheet" type="text/css" />
 <bookmarks>  
     <bookmark name="a" href="#a" /> 
      <bookmark name="b" href="#b" /> 
 </bookmarks>  
</head>







Page Other Features




<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Alice‘s Adventures in Wonderland -- Chapter I</title>
        <link rel="stylesheet" type="text/css" href="alice.css" media="print"/>
    </head>
   
    <body>
        <div id="header" style="">Alice‘s Adventures in Wonderland</div>
        <div id="footer" style="">  Page <span id="pagenumber"/> of <span id="pagecount"/> </div>
               
        <h1>CHAPTER I</h1>
       
        <h2>Down the Rabbit-Hole</h2>
       
        <p class="dropcap-holder">
            <div class="dropcap">A</div>
            lice was beginning to get very tired of sitting by her sister
            on the bank, and of having nothing to do: once or twice she had
            peeped into the book her sister was reading, but it had no pictures
            or conversations in it, `and what is the use of a book,‘ thought
            Alice `without pictures or conversation?‘
        </p>
       
        <p>So she was considering in her own mind (as well as she could,
            for the hot day made her feel very sleepy and stupid), whether the
            pleasure of making a daisy-chain would be worth the trouble of
            getting up and picking the daisies, when suddenly a White Rabbit
        with pink eyes ran close by her. </p>
       
        <p class="figure">
            <img src="alice2.gif" width="200px" height="300px"/>
            <br/>
            <b>White Rabbit checking watch</b>
        </p>
        ... the rest of the chapter
@page {
size: 4.18in 6.88in;
margin: 0.25in;
-fs-flow-top: "header";
-fs-flow-bottom: "footer";
-fs-flow-left: "left";
-fs-flow-right: "right";
border: thin solid black;
padding: 1em;
}
#header {
font: bold serif;
position: absolute; top: 0; left: 0;
-fs-move-to-flow: "header";
}
#footer {
font-size: 90%; font-style: italic;
position: absolute; top: 0; left: 0;
-fs-move-to-flow: "footer";
}
#pagenumber:before {
content: counter(page);
}
#pagecount:before {content: counter(pages); 
}







Itextrenderer (Flying Saucer) HTML 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.