springMVC中itext生成PDF,根本上解决中文乱码以及不显示问题

本文介绍了如何在SpringMVC项目中使用iText库生成PDF,重点解决中文乱码和无法显示的问题。通过在pom.xml引入相关jar包,创建生成PDF的Controller和工具类,可以实现PDF的正确显示。
摘要由CSDN通过智能技术生成

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.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值