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

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 结果

    165512_lyke_253813.jpg


如果大家有不明白的可以留言问我,一同进步

转载于:https://my.oschina.net/sanji/blog/277704

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值