itext生成PDF,根本上解决中文不显示问题
1、建立maven工程,在pom文件中引入下面的jar包
<span style="font-size:12px;"><dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.7</version>
</dependency></span>
2、spring中新建一个生成PDF的controller
<span style="font-size:12px;">/**
* @throws Exception
* @Definition : 导出PDF格式的简历
* @Author: xiachao
* @Time: 2016年9月19日
*/
@RequestMapping("exportPdf")
public ResponseEntity<byte[]> exportPdf(HttpServletRequest request, HttpServletResponse response) throws Exception{
Account a = getCurAccount(request);
Integer accountId = a.getAccId();
Map resultMap = accountService.findAllProfileAndResumeForExport(accountId);
File oFile = ExportPdfUtil.getPdf(a, resultMap);
HttpHeaders headers = new HttpHeaders();
String fileName = null;
fileName = new String((a.getRealName()+"简历-"+"迷你校.pdf").getBytes("utf-8"),"iso-8859-1");//解决中文乱码
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(oFile),headers, HttpStatus.OK);
}</span>
3、在工程的common包下新建一个生成PDF的工具类,即上面controller中的ExportPDFUtility类
<span style="font-size:12px;"> /**
* @Definition 另一种方式导出简历PDF
* @author xiachao
* @Data 2016-9-28
* @throws Exception
*/
public static File getPdf(Account account,Map map) throws Exception{
PdfPCell cell1 =null;
String fileName = "test.pdf";
File outFile = new File(Constant.pdfExportPath + File.separator + fileName);
if (! outFile.exists()) {
new File(Constant.pdfExportPath).mkdirs();
outFile.createNewFile();
}
// 1.新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(outFile));
// 3.打开文档
document.open();
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(baseFontChinese , 11 , Font.NORMAL);
Font font_2 = new Font(baseFontChinese,12,Font.BOLD);
Font font = new Font(baseFontChinese, 14, Font.BOLD);
// 4.向文档中添加内容
Paragraph paragraph = new Paragraph(account.getRealName()+"个人简历",new Font(baseFontChinese,18,Font.BOLD));
paragraph.setAlignment(1); // 设置段落居中
paragraph.setLeading(7.0f);; //设置段落与其上方的距离
document.add(paragraph);
document.add(new Paragraph("\n"));
document.add(new Paragraph("基本信息",font));
/*
* 输出一条直线
*/
Paragraph p1 = new Paragraph();
p1.add(new Chunk(new LineSeparator()));
document.add(p1);
document.add(new Paragraph("\n"));
if( map.get("r_1001") != null){
List list = (List) map.get("r_1001");
JSONArray jsonArray = JSONArray.fromObject(list);
for(int i = 0 ; i<jsonArray.size(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
//输出一个表格
PdfPTable table = new PdfPTable(6);
//第一行
cell1 = new PdfPCell(new Phrase("姓名",fontChinese));
cell1.setBorderWidth(0);
cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase((String) ((jsonObject.get("name") != null) ? (jsonObject.get("name")) : ""),fontChinese));
cell1.setPadding(6.5f);
cell1.setBorderWidth(0);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase(""));
cell1.setBorderWidth(0);
cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase(""));
cell1.setBorderWidth(0);
cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase(""));
cell1.setBorderWidth(0);
cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase(""));
cell1.setBorderWidth(0);
cell1.setPadding(6.5f);
table.addCell(cell1);
//第二行
cell1 = new PdfPCell(new Phrase("性别",fontChinese));
cell1.setBorderWidth(0);cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase((String) ((jsonObject.get("sex") != null) ? (jsonObject.get("sex")) : ""),fontChinese));
cell1.setBorderWidth(0);cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase("出生日期",fontChinese));
cell1.setBorderWidth(0);cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase((String) ((jsonObject.get("birthday") != null) ? (jsonObject.get("birthday")) : ""),fontChinese));
cell1.setBorderWidth(0);cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase("政治面貌",fontChinese));
cell1.setBorderWidth(0);cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase((String) ((jsonObject.get("political") != null) ? (jsonObject.get("political")) : ""),fontChinese));
cell1.setBorderWidth(0);cell1.setPadding(6.5f);
table.addCell(cell1);
//第三行
cell1 = new PdfPCell(new Phrase("现居住地",fontChinese));
cell1.setBorderWidth(0);cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase((String) ((jsonObject.get("city") != null) ? (jsonObject.get("city")) : ""),fontChinese));
cell1.setBorderWidth(0);
cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase("生源地",fontChinese));
cell1.setBorderWidth(0);
cell1.setPadding(6.5f);
table.addCell(cell1);
cell1 = new PdfPCell(new Phrase((String) ((jsonObject.