通常我们在做项目的时候,会有导出的功能,譬如:excel、word、pdf等等
之前有一篇导出excel的博文,大家可以查看:
Java poi Excel 通用导出
这里提供一篇简单导出pdf表格的文章
所需jar包 itextpdf-5.4.3
itextpdf-5.4.3 jar 下载地址字体jar包itext-asian-5.2.0
itext-asian-5.2.0 jar 下载地址
maven地址:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
下面请看代码
public class ExportPdfUtil<T> extends BaseAction{
/**
* sheetName:导出的pdf文件名
* headers:表格列头
* columns:列
* lists:数据源(数据集合)
*/
public void exportPdf(String sheetName,String[] headers, String[] columns, List lists, HttpServletRequest request,
HttpServletResponse response,String realPath) throws Exception{
logger.info("zsl==========开始导出pdf");
Document document = new Document(PageSize.A2,50,50,50,50);
// PdfWriter pw = PdfWriter.getInstance(document,new FileOutputStream("D:\\Test.pdf"));
String fileName = sheetName + ".pdf";
String destPath = UploadFileUtil.getFilePath(request)+ SystemConfig.readValue("pdfPath") + File.separator;
logger.info("zsl==========pdfPath:" + destPath);
mkDirs(destPath);
String filePath = destPath + fileName;
logger.info("zsl==========filePath:" + filePath);
//必须紧跟在document创建的时候创建PdfWriter,否则导出无数据
PdfWriter pw = PdfWriter.getInstance(document,new FileOutputStream(filePath));
document.open();
//中文字体显示,直接用下载的字体jar包也可以
// BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",false);
//绝对路径
String fontPath = realPath + "/WEB-INF/classes/font/SIMYOU.TTF";
BaseFont baseFont = BaseFont.createFont(fontPath,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font font = new Font(baseFont,10,Font.NORMAL);
Paragraph p = new Paragraph(title,font);
document.add(p);
PdfPTable table = new PdfPTable(headers.length);
table.setSpacingBefore(25);
table.setSpacingAfter(25);
//创建表头
for (int i = 0; i < headers.length; i++) {
PdfPCell cell = new PdfPCell(new Paragraph(headers[i],font));
table.addCell(cell);
}
for (int i = 0; i < lists.size(); i++) {
String value = "";
if(null == lists.get(i)){
value = "";
}else{
value = lists.get(i).toString();
}
table.addCell(new Paragraph(value,font));
}
document.add(table);
document.close();
pw.flush();
pw.close();
downloadPdf(filePath, response);
logger.info("zsl==========导出pdf成功");
}
public static void mkDirs(String filepath){
File fileDir = new File(filepath);
if(!fileDir.exists()){
fileDir.mkdirs();
}
}
//下载
public static void downloadPdf(String filepath, HttpServletResponse response)
throws IOException {
File file = new File(filepath);
String fileName = file.getName();
response.setContentType("application/pdf;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "ISO8859-1"));
response.setCharacterEncoding("utf-8");
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] b = new byte[fis.available()];
fis.read(b);
response.getOutputStream().write(b);
response.getOutputStream().flush();
response.getOutputStream().close();
response.flushBuffer();
fis.close();
}
}
上面需要注意一点:有关字体,如果不指定中文字体的话,中文会显示不出来。
我们使用字体可以有两种方式。
1、可以使用我们下载的itext-asian-5.2.0jar 包,只需要在代码里
BaseFont baseFont = BaseFont.createFont(“STSongStd-Light”, “UniGB-UCS2-H”,false);
2、也可以指定我们自己的字体
Windows系统可以在C:\Windows\Fonts下面找到很多字体,我们只要拷贝一种自己喜欢的字体,放到我们项目目录下,然后在代码里制定到这个字体目录就可以了。
为大家提供一个demo官网,里面有很多例子:
点击这里:导出pdf官网