使⽤flying-saucer-pdf实现html转换pdf
ps:之前研究了使⽤itext html转PDF 对中⽂和css的⽀持不很好,果然Google了⼀把,发现flying-saucer-pdf这个效果好,研究了⼀下果然⾏,运⽤到项⽬中基本上能满⾜需求。
1、l ⽂件
1  1   <dependency>
2  2            <groupId>com.itextpdf</groupId>
3  3            <artifactId>itextpdf</artifactId>
4  4            <version>5.5.13</version>
5  5        </dependency>
6  6        <dependency>
7  7            <groupId>l</groupId>
8  8            <artifactId>xmlworker</artifactId>
9  9            <version>5.5.13</version>
10 10        </dependency>
11 11        <dependency>
12 12            <groupId>com.itextpdf</groupId>
13 13            <artifactId>itext-asian</artifactId>
14 14            <version>5.2.0</version>
15 15        </dependency>
16 16        <dependency>
17 17            <groupId>org.xhtmlrenderer</groupId>
18 18            <artifactId>flying-saucer-pdf</artifactId>
19 19            <version>9.0.3</version>
20 20        </dependency>
2、代码
package pdf.kit;
import *;
import pdf.BaseFont;
import pdf.PdfWriter;
import l.xml.Pipeline;
import l.xml.XMLWorker;
import l.xml.XMLWorkerFontProvider;
import l.xml.XMLWorkerHelper;
import l.xml.html.CssAppliers;
import l.xml.html.CssAppliersImpl;
import l.xml.html.Tags;
import l.xml.FileRetrieve;
import l.xml.ReadingProcessor;
import l.xml.parser.XMLParser;
import l.xml.pipeline.css.CSSResolver;
import l.xml.pipeline.css.CssResolverPipeline;
import l.d.PdfWriterPipeline;
import l.xml.pipeline.html.*;
import org.apachemons.io.FileUtils;
import org.apachemons.lang.StringUtils;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
import java.HttpURLConnection;
import java.URL;
import java.nio.charset.Charset;
/**
* html 转换成 pdf
*/
public class ParseHtmlTable {
public static final String pdfDestPath = "C:/Users/xxx-b/Desktop/pdf/";
public static final String htmlPath = "D:\\3-workspace\\pdf-test\\src\\test\\resources\\templates\\test.html";
public static void main(String[] args) throws IOException, DocumentException {
String pdfName = "test.pdf";
ParseHtmlTable parseHtmlTable = new ParseHtmlTable();
String htmlStr = adFileToString(new File(htmlPath));
parseHtmlTable.html2pdf(htmlStr,pdfName ,"C:\\Windows\\Fonts");
}
public void html2pdf(String html, String pdfName, String fontDir) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = (ITextFontResolver) SharedContext().getFontResolver();
//添加字体库 begin
File f = new File(fontDir);
if (f.isDirectory()) {
File[] files = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
String lower = LowerCase();
dsWith(".otf") || dsWith(".ttf") || dsWith(".ttc");
}
});
for (int i = 0; i < files.length; i++) {
fontResolver.addFont(files[i].getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);                }
}
//添加字体库end
renderer.setDocumentFromString(html);
renderer.layout();
renderer.finishPDF();
byte[] buff = os.toByteArray();
//保存到磁盘上
FileUtil.byte2File(buff,pdfDestPath,pdfName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
FileUtil.java
package pdf.kit;
import java.io.*;
public class FileUtil {
/**
* 获得指定⽂件的byte数组
*
* @param filePath ⽂件绝对路径
* @return
*/
public static byte[] file2Byte(String filePath) {
ByteArrayOutputStream bos = null;
BufferedInputStream in = null;
pdf转htmltry {
File file = new File(filePath);
if (!ists()) {
throw new FileNotFoundException("file not exists");
}
bos = new ByteArrayOutputStream((int) file.length());
in = new BufferedInputStream(new FileInputStream(file));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}
ByteArray();
} catch (Exception e) {
System.out.Message());
e.printStackTrace();
return null;
} finally {
try {
if (in != null) {
in.close();
}
if (bos != null) {
bos.close();
}
} catch (Exception e) {
System.out.Message());
e.printStackTrace();
}
}
}
/**
* 根据byte数组,⽣成⽂件
*
* @param bfile    ⽂件数组
* @param filePath ⽂件存放路径
* @param fileName ⽂件名称
*/
public static void byte2File(byte[] bfile, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!ists() && !dir.isDirectory()) {//判断⽂件⽬录是否存在
dir.mkdirs();
}
file = new File(filePath + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
System.out.Message());
e.printStackTrace();
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
} catch (Exception e) {
System.out.Message());
e.printStackTrace();
}
}
}
}
3.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
*{
padding: 0;
margin: 0;
color: #000;
font-family:Microsoft YaHei
}
</style>
</head>
<body screen_capture_injected="true" ryt11773="1">
<p>
<span >⾧空</span><span
>(Broken
Sword),</span><span >秦王殘劍</span>
<span >(Flying
Snow),</span><span >⾶雪</span>
<span >(Moon),
</span><span >如⽉</span><span
>(the
King), and</span><span >秦王</span>
<span >(Sky).</span>
</p>
<p>选中<input type="checkbox" value="1" disabled="disabled" checked="checked"/></p>
<br/>
<textarea rows="11" cols="10"  disabled="disabled">
adfadfadfadfa
</textarea>
</body>
</html>
4、pdf结果
5、⼀些总结
1、模板的要求: html的格式要求符合xml格式,必须要有闭合标签。
2、字体的⽀持: font-family:Microsoft YaHei ,html中定义字体,程序中⼀定要导⼊想匹配的格式,才能有效。