junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.freemarker
freemarker
2.3.22
org.xhtmlrenderer
flying-saucer-pdf
9.0.3
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
package com.lee.load.pdf;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Map
package com.lee.load.pdf;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
public class PdfExport {
/**
* @描述 生成PDF并弹出下载框
* @author lim
* @时间 2017年11月9日
* @param ftlPath ftl模板文件的路径
* @param ftlName ftl模板的名称
* @param data map类型的数据
* @param response
* @param request
* @throws Exception
*/
public void ExportPDF(String ftlPath,String ftlName,Map data,HttpServletResponse response,HttpServletRequest request) {
// create PDF document
Document document = new Document();
try {
response.setContentType("application/x-msdownload;charset=UTF-8");
response.setHeader("Content-Disposition","attachment;filename=report.pdf");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
String html=PdfExport.getPdfContent(ftlPath, ftlName, data,request);
OutputStream out = null;
ITextRenderer render = null;
out = response.getOutputStream();
render = PdfExport.getRender(request);
render.setDocumentFromString(html);
render.layout();
render.createPDF(out);
render.finishPDF();
document.add(new Paragraph(render.toString()));
render = null;
out.close();
}
catch(Exception e) {
e.printStackTrace();
System.err.println("document: " + e.getMessage());
}finally{
document.close();
}
}
//获取要写入PDF的内容
public static String getPdfContent(String ftlPath, String ftlName, Map data, HttpServletRequest request) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
return PdfExport.useTemplate(ftlPath, ftlName, data,request);
}
//使用freemarker得到html内容
public static String useTemplate(String ftlPath, String ftlName, Map data, HttpServletRequest request) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
String html = null;
String webPath = getWEBPath(request);
Template tpl = getFreemarkerConfig(webPath+ftlPath).getTemplate(ftlName);
StringWriter writer = new StringWriter();
tpl.process(data, 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;
}
/**
*
* @描述 加载字体,以便支持中文
* @author lim
* @时间 2017年11月9日
* @param request
* @return
* @throws DocumentException
* @throws IOException
*/
public static ITextRenderer getRender(HttpServletRequest request) throws DocumentException, IOException {
//获取路径
String webPath = getWEBPath(request);
ITextRenderer render = new ITextRenderer();
//添加字体,以支持中文
render.getFontResolver().addFont(webPath+"/template/pdf/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
render.getFontResolver().addFont(webPath+"/template/pdf/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
return render;
}
/**
* @描述 获取路径,以便加载webapp目录下的资源文件
* @author lim
* @时间 2017年11月9日
* @param request
* @return
*/
public static String getWEBPath(HttpServletRequest request) {
return request.getSession().getServletContext().getRealPath("/");
}
}
上面那个类可以稍微修改一下,另一种写法:
package com.lee.load.pdf;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
public class PdfExport {
/**
* @描述 生成PDF并弹出下载框
* @author lim
* @时间 2017年11月9日
* @param ftlPath ftl模板文件的路径
* @param ftlName ftl模板的名称
* @param data map类型的数据
* @param response
* @param request
* @throws Exception
*/
public void ExportPDF(String ftlPath,String ftlName,Map data,HttpServletResponse response,HttpServletRequest request) {
// create PDF document
Document document = new Document();
try {
response.setContentType("application/x-msdownload;charset=UTF-8");
response.setHeader("Content-Disposition","attachment;filename=report.pdf");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
String html=PdfExport.getPdfContent(ftlPath, ftlName, data,request);
OutputStream out = null;
ITextRenderer render = null;
out = response.getOutputStream();
render = PdfExport.getRender(request);
render.setDocumentFromString(html);
render.layout();
render.createPDF(out);
render.finishPDF();
document.add(new Paragraph(render.toString()));
render = null;
out.close();
}
catch(Exception e) {
e.printStackTrace();
System.err.println("document: " + e.getMessage());
}finally{
document.close();
}
}
//获取要写入PDF的内容
public static String getPdfContent(String ftlPath, String ftlName, Map data, HttpServletRequest request) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
return PdfExport.useTemplate(ftlPath, ftlName, data,request);
}
//使用freemarker得到html内容
public static String useTemplate(String ftlPath, String ftlName, Map data, HttpServletRequest request) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
String html = null;
String webPath = getWEBPath(request);
Configuration configuration = new Configuration();
TemplateLoader templateLoader = new FileTemplateLoader(new File(webPath));
configuration.setTemplateLoader(templateLoader);
String path = ftlPath+ftlName;
Template tpl =configuration.getTemplate(path,"UTF-8");
//Template tpl = getFreemarkerConfig(webPath+ftlPath).getTemplate(ftlName);
StringWriter writer = new StringWriter();
tpl.process(data, writer);
writer.flush();
html = writer.toString();
return html;
}
/**
*
* @描述 加载字体,以便支持中文
* @author lim
* @时间 2017年11月9日
* @param request
* @return
* @throws DocumentException
* @throws IOException
*/
public static ITextRenderer getRender(HttpServletRequest request) throws DocumentException, IOException {
//获取路径
String webPath = getWEBPath(request);
ITextRenderer render = new ITextRenderer();
//添加字体,以支持中文
render.getFontResolver().addFont(webPath+"/template/pdf/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
render.getFontResolver().addFont(webPath+"/template/pdf/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
return render;
}
/**
* @描述 获取路径,以便加载webapp目录下的资源文件
* @author lim
* @时间 2017年11月9日
* @param request
* @return
*/
public static String getWEBPath(HttpServletRequest request) {
return request.getSession().getServletContext().getRealPath("/");
}
}
最后记得servlet需要在web.xml中配置一下
代码已经上传至:http://download.csdn.net/download/lm5463640/10111723