1.首先通过maven的pom.xml文件引入jar包
javax.servlet
javax.servlet-api
3.0.1
provided
org.freemarker
freemarker
2.3.22
org.xhtmlrenderer
flying-saucer-pdf
9.0.3
package yang.zheng.util.pdf;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Locale;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
/**
* PDF生成辅助类
* @author Goofy http://www.xdemo.org
*
*/
@SuppressWarnings("deprecation")
public class PdfHelper {
public static ITextRenderer getRender() throws DocumentException, IOException {
ITextRenderer render = new ITextRenderer();
String path = getPath();
//添加字体,以支持中文
render.getFontResolver().addFont(path + "pdf/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
render.getFontResolver().addFont(path + "pdf/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
return render;
}
//获取要写入PDF的内容
public static String getPdfContent(String ftlPath, String ftlName, Object o) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
return useTemplate(ftlPath, ftlName, o);
}
//使用freemarker得到html内容
public static String useTemplate(String ftlPath, String ftlName, Object o) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
String html = null;
Template tpl = getFreemarkerConfig(ftlPath).getTemplate(ftlName);
tpl.setEncoding("UTF-8");
StringWriter writer = new StringWriter();
tpl.process(o, writer);
writer.flush();
html = writer.toString();
return html;
}
/**
* 获取Freemarker配置
* @param templatePath
* @return
* @throws IOException
*/
private static Configuration getFreemarkerConfig(String templatePath) throws IOException {
freemarker.template.Version version = new freemarker.template.Version("2.3.22");
Configuration config = new Configuration(version);
config.setDirectoryForTemplateLoading(new File(templatePath));
config.setEncoding(Locale.CHINA, "utf-8");
return config;
}
/**
* 获取类路径
* @return
*/
public static String getPath(){
return PdfHelper.class.getResource("/").getPath().substring(1);
}
}
3.编写一个pdf工具类
package yang.zheng.util.pdf;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
/**
* PDF生成工具类
* @author Goofy http://www.xdemo.org
*
*/
public class PdfUtils {
public static void main(String[] args) {
try {
Map
4.pdf生成模板(tpl.ftl)
作者:http://www.xdemo.org/
你好:${name}
图片支持
Hello PDF: 中文支持
样式支持,红边框,红字
样式支持,蓝色10像素的边框,蓝字
A
B
C
D
100
29
32
43
100
29
32
43
100
29
32
43
100
29
32
43
100
29
32
43
<#list nameList as list>
${list}
#list>
5.字体
生成pdf工具类中使用了两种字体分别为arialuni.ttf和simsun.ttc,请大家自行到百度中搜索下载。