iText – 使用Java将HTML转换为PDF

iText “XML Worker”允许开发人员以一种程序员友好的方式将XML文件转换成PDF文件。iText还可以将包含CSS样式的HTML转换为PDF格式的文档。

 

目标:

  • 实现如何利用iText Java库将HTML文件转换成PDF文档?

Environment & Tools

  • Eclipse (or any other IDE)
  • Maven (optional)

Library:

  • iText 5.4.2

( 1 ) HTML File

  • index.html
    01 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    02 <html>
    03     <head>
    04         <title>HTML to PDF</title>
    05         <link href="style.css" rel="stylesheet" type="text/css" />
    06     </head>
    07     <body>
    08         <h1>HTML to PDF</h1>
    09         <p>
    10             <span class="itext">itext</span> 5.4.2 <span class="description"> converting HTML to PDF</span>
    11         </p>
    12         <table>
    13           <tr>
    14                 <th class="label">Title</th>
    15                 <td>iText - Java HTML to PDF</td>
    16             </tr>
    17             <tr>
    18                 <th>URL</th>
    19                 <td>http://hmkcode.com/itext-html-to-pdf-using-java</td>
    20             </tr>
    21         </table>
    22     </body>
    23 </html>
  • style.css
    01 h1 {
    02   color:#ccc;
    03 }
    04 table tr td{
    05     text-align:center;
    06     border:1px solid gray;
    07     padding:4px;
    08 }
    09 table tr th{
    10     background-color:#84C7FD;
    11     color:#fff;
    12     width100px;
    13 }
    14 .itext{
    15     color:#84C7FD;
    16     font-weight:bold;
    17 }
    18 .description{
    19     color:gray;
    20 }

    ( 2 ) Java App

  • App.java
    01 package com.hmkcode;
    02   
    03 import java.io.FileInputStream;
    04 import java.io.FileOutputStream;
    05 import java.io.IOException;
    06 import com.itextpdf.text.Document;
    07 import com.itextpdf.text.DocumentException;
    08 import com.itextpdf.text.pdf.PdfWriter;
    09 import com.itextpdf.tool.xml.XMLWorkerHelper;
    10   
    11 public class App
    12 {
    13     public static void main( String[] args ) throws DocumentException, IOException
    14     {
    15       // step 1
    16         Document document = new Document();
    17         // step 2
    18         PdfWriter writer = PdfWriter.getInstance(document, newFileOutputStream("pdf.pdf"));
    19         // step 3
    20         document.open();
    21         // step 4
    22         XMLWorkerHelper.getInstance().parseXHtml(writer, document,
    23                 new FileInputStream("index.html"));
    24         //step 5
    25          document.close();
    26   
    27         System.out.println( "PDF Created!" );
    28     }
    29 }

    ( 3 ) Output “PDF”

    itext-html-pdf-output

    Source Code @ GitHub

1、解决中文问题 2、附字体 3、动态html拼接转pdf public static void htmlCodeComeString(String linkcss,String htmlCode, String outputFile,String title) throws Exception { OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocumentFromString(getConversionHtmlCode(linkcss,htmlCode,title)); ITextFontResolver fontResolver = renderer.getFontResolver(); URL fontPath = ItextUtil.class.getResource("simsun.ttc"); fontResolver.addFont(fontPath.toString(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 解决图片的相对路径问题 // renderer.getSharedContext().setBaseURL("file:/F:/teste/html/"); renderer.layout(); renderer.createPDF(os); System.out.println("======转换成功!"); os.close(); os.flush(); } public static void main(String[] args) { ItextUtil itextUtil = new ItextUtil(); String html = ""; html += ""; html += "企业信息"; html += " "; html += " "; html += " 登记日期"; html += " 2006-04-28"; html += " "; html += " "; html += " 纳税人编号"; html += " HSJIHKS002"; html += " "; html += " "; html += " 有效标志"; html += " Y"; html += " "; html += " "; html += " 社会信用代码"; html += " 916101317H"; html += " "; html += " "; html += " 评估机关代码"; html += " 盛世"; html += " "; html += " "; html += " 工商注销日期"; html += " 2006-04-28"; html += " "; html += " "; html += ""; String outputFile = "D:\\pdf\\aa.pdf"; try { itextUtil.htmlCodeComeString("",html,outputFile,""); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("生成结束!!!"); }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值