通常我們在做項目的時候,會有導出的功能,譬如: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官網