最简单 iText 的 PDF 生成方案(含中文解决方案)HTML 转为 PDF

时间:2024-03-06 11:01:54

转自:http://my.oschina.net/sanji/blog/277704

 

最近正好项目有用到 ITEXT ,在网络上搜索了一番,发现了很多方案,但是感觉对于一般开发来说都太复杂了,本文提供一种最便捷的方式来实现,不需要任何额外 JAR 包。

 

ITEXT 的功能和简介这里就不多说了,直接带大家做一遍:

一段可运行的代码比说很多废话强得多

 

S1 :首先下载 JAR 包

    CORE 包:http://sourceforge.net/projects/itext/files/

    XML  包:http://sourceforge.net/projects/xmlworker/files/

 

S2:创建 HTML 文档(将要使用的 HTML 文档素材)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body screen_capture_injected="true" ryt11773="1">
    <p>
        <span style="font-size:12.0pt; font-family:MS Mincho">長空</span> <span
            style="font-size:12.0pt; font-family:Times New Roman,serif">(Broken
            Sword),</span> <span style="font-size:12.0pt; font-family:MS Mincho">秦王殘劍</span>
        <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Flying
            Snow),</span> <span style="font-size:12.0pt; font-family:MS Mincho">飛雪</span>
        <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Moon),
        </span> <span style="font-size:12.0pt; font-family:MS Mincho">如月</span> <span
            style="font-size:12.0pt; font-family:Times New Roman,serif">(the
            King), and</span> <span style="font-size:12.0pt; font-family:MS Mincho">秦王</span>
        <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Sky).</span>
    </p>
</body>
</html>

 

 

S3: 引入 JAR 包 ,编写JAVA 转换代码

 

package com.zhouqian.itext;
  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
  
  
public class D07_ParseHtmlAsian {
  
     
     
    public static final String HTML = "E:/Dropbox/document/java/designPattern/data/hero.html";
    public static final String DEST = "E:/Dropbox/document/java/designPattern/data/hero.pdf";
  
    /**
     * Creates a PDF with the words "Hello World"
     * @param file
     * @throws IOException
     * @throws DocumentException
     */
    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML), Charset.forName("UTF-8"));
        // step 5
        document.close();
    }
  
    /**
     * Main method
     */
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new D07_ParseHtmlAsian().createPdf(DEST);
    }
}

 

S4:在指定路径下就可以得到 PDF 结果