iText 开发文档:
http://developers.itextpdf.com/developers-home
iText 目前有两套版本iText5和iText7。iText5应该是网上用的比较多的一个版本。iText5因为是很多开发者参与贡献代码,因此在一些规范和 设计上存在不合理的地方。iText7是后来官方针对iText5的重构,两个版本差别还是挺大的。不过在实际使用中,一般用到的都比较简单,所以不用特 别拘泥于使用哪个版本。比如我们在http://mvnrepository.com/中搜索iText,出来的都是iText5的依赖。
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* Created by lujianing on 2017/5/7.
*/
public class JavaToPdf {
private static final String DEST = "target/HelloWorld.pdf";
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
document.add(new Paragraph("hello world"));
document.close();
writer.close();
}
}
http://developers.itextpdf.com/examples/font-examples/using-fonts#1227-tengwarquenya1.java
测试代码:JavaToPdfCN
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* Created by lujianing on 2017/5/7.
*/
public class JavaToPdfCN {
private static final String DEST = "target/HelloWorld_CN.pdf";
private static final String FONT = "simhei.ttf";
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
document.add(new Paragraph("hello world,我是鲁家宁", f1));
document.close();
writer.close();
}
}
http://developers.itextpdf.com/examples/xml-worker-itext5/xml-worker-examples
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>
<meta charset="UTF-8"/>
<title>Title</title>
<style>
body{
font-family:SimHei;
}
.red{
color: red;
}
</style>
</head>
<body>
<div>
你好,鲁家宁
</div>
</body>
</html>
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.lujianing.test.util.PathUtil;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* Created by lujianing on 2017/5/7.
*/
public class JavaToPdfHtml {
private static final String DEST = "target/HelloWorld_CN_HTML.pdf";
private static final String HTML = PathUtil.getCurrentPath()+"/template.html";
private static final String FONT = "simhei.ttf";
public static void main(String[] args) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
// step 3
document.open();
// step 4
XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontImp.register(FONT);
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML), null, Charset.forName("UTF-8"), fontImp);
// step 5
document.close();
}
}
<artifactId>freemarker</artifactId>
<version>2.3.19</version>
</dependency>
<meta charset="UTF-8"/>
<title>Title</title>
<style>
body{
font-family:SimHei;
}
.blue{
color: blue;
}
</style>
</head>
<body>
<div>
你好,${name}
</div>
</body>
</html>
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.lujianing.test.util.PathUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
/**
* Created by lujianing on 2017/5/7.
*/
public class JavaToPdfHtmlFreeMarker {
private static final String DEST = "target/HelloWorld_CN_HTML_FREEMARKER.pdf";
private static final String HTML = "template_freemarker.html";
private static final String FONT = "simhei.ttf";
private static Configuration freemarkerCfg = null;
static {
freemarkerCfg =new Configuration();
//freemarker的模板目录
try {
freemarkerCfg.setDirectoryForTemplateLoading(new File(PathUtil.getCurrentPath()));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException, DocumentException {
Map<String,Object> data = new HashMap();
data.put("name","鲁家宁");
String content = JavaToPdfHtmlFreeMarker.freeMarkerRender(data,HTML);
JavaToPdfHtmlFreeMarker.createPdf(content,DEST);
}
public static void createPdf(String content,String dest) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontImp.register(FONT);
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"), fontImp);
// step 5
document.close();
}
/**
* freemarker渲染html
*/
public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码方式
Template template = freemarkerCfg.getTemplate(htmlTmp);
template.setEncoding("UTF-8");
// 合并数据模型与模板
template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
}
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.5</version>
</dependency>
<meta charset="UTF-8"/>
<title>Title</title>
<style>
body{
font-family:SimHei;
}
.color{
color: green;
}
.pos{
position:absolute;
left:200px;
top:5px;
width: 200px;
font-size: 10px;
}
</style>
</head>
<body>
<img src="logo.png" width="600px"/>
<div>
你好,${name}
</div>
</body>
</html>
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.lujianing.test.util.PathUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
/**
* Created by lujianing on 2017/5/7.
*/
public class JavaToPdfHtmlFreeMarker {
private static final String DEST = "target/HelloWorld_CN_HTML_FREEMARKER_FS.pdf";
private static final String HTML = "template_freemarker_fs.html";
private static final String FONT = "simhei.ttf";
private static final String LOGO_PATH = "file://"+PathUtil.getCurrentPath()+"/logo.png";
private static Configuration freemarkerCfg = null;
static {
freemarkerCfg =new Configuration();
//freemarker的模板目录
try {
freemarkerCfg.setDirectoryForTemplateLoading(new File(PathUtil.getCurrentPath()));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
Map<String,Object> data = new HashMap();
data.put("name","鲁家宁");
String content = JavaToPdfHtmlFreeMarker.freeMarkerRender(data,HTML);
JavaToPdfHtmlFreeMarker.createPdf(content,DEST);
}
/**
* freemarker渲染html
*/
public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码方式
Template template = freemarkerCfg.getTemplate(htmlTmp);
template.setEncoding("UTF-8");
// 合并数据模型与模板
template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
public static void createPdf(String content,String dest) throws IOException, DocumentException, com.lowagie.text.DocumentException {
ITextRenderer render = new ITextRenderer();
ITextFontResolver fontResolver = render.getFontResolver();
fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 解析html生成pdf
render.setDocumentFromString(content);
//解决图片相对路径的问题
render.getSharedContext().setBaseURL(LOGO_PATH);
render.layout();
render.createPDF(new FileOutputStream(dest));
}
}
<artifactId>jpedal-lgpl</artifactId>
<version>4.74b27</version>
</dependency>
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.lujianing.test.util.PathUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.jpedal.PdfDecoder;
import org.jpedal.exception.PdfException;
import org.jpedal.fonts.FontMappings;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
/**
* Created by lujianing on 2017/5/7.
*/
public class JavaToPdfImgHtmlFreeMarker {
private static final String DEST = "target/HelloWorld_CN_HTML_FREEMARKER_FS_IMG.png";
private static final String HTML = "template_freemarker_fs.html";
private static final String FONT = "simhei.ttf";
private static final String LOGO_PATH = "file://"+PathUtil.getCurrentPath()+"/logo.png";
private static final String IMG_EXT = "png";
private static Configuration freemarkerCfg = null;
static {
freemarkerCfg =new Configuration();
//freemarker的模板目录
try {
freemarkerCfg.setDirectoryForTemplateLoading(new File(PathUtil.getCurrentPath()));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
Map<String,Object> data = new HashMap();
data.put("name","鲁家宁");
String content = JavaToPdfImgHtmlFreeMarker.freeMarkerRender(data,HTML);
ByteArrayOutputStream pdfStream = JavaToPdfImgHtmlFreeMarker.createPdf(content);
ByteArrayOutputStream imgSteam = JavaToPdfImgHtmlFreeMarker.pdfToImg(pdfStream.toByteArray(),2,1,IMG_EXT);
FileOutputStream fileStream = new FileOutputStream(new File(DEST));
fileStream.write(imgSteam.toByteArray());
fileStream.close();
}
/**
* freemarker渲染html
*/
public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码方式
Template template = freemarkerCfg.getTemplate(htmlTmp);
template.setEncoding("UTF-8");
// 合并数据模型与模板
template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
/**
* 根据模板生成pdf文件流
*/
public static ByteArrayOutputStream createPdf(String content) {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ITextRenderer render = new ITextRenderer();
ITextFontResolver fontResolver = render.getFontResolver();
try {
fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (com.lowagie.text.DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 解析html生成pdf
render.setDocumentFromString(content);
//解决图片相对路径的问题
render.getSharedContext().setBaseURL(LOGO_PATH);
render.layout();
try {
render.createPDF(outStream);
return outStream;
} catch (com.lowagie.text.DocumentException e) {
e.printStackTrace();
} finally {
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 根据pdf二进制文件 生成图片文件
*
* @param bytes pdf二进制
* @param scaling 清晰度
* @param pageNum 页数
*/
public static ByteArrayOutputStream pdfToImg(byte[] bytes, float scaling, int pageNum,String formatName) {
//推荐的方法打开PdfDecoder
PdfDecoder pdfDecoder = new PdfDecoder(true);
FontMappings.setFontReplacements();
//修改图片的清晰度
pdfDecoder.scaling = scaling;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
//打开pdf文件,生成PdfDecoder对象
pdfDecoder.openPdfArray(bytes); //bytes is byte[] array with PDF
//获取第pageNum页的pdf
BufferedImage img = pdfDecoder.getPageAsImage(pageNum);
ImageIO.write(img, formatName, out);
} catch (PdfException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return out;
}
}
PhantomJS 是一个基于 WebKit 的服务器端 JavaScript API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。 PhantomJS 可以用于 页面自动化 , 网络监测 , 网页截屏 ,以及 无界面测试 等。
具体方法可自行查询。