首页>代码>spring boot整合eCharts,itext读取数据库数据显示eCharts柱状图表,支持pdf文件导出和生成图片>/spring-boot-echarts-master/src/main/java/com/demo/controller/PdfExportController.java
package com.demo.controller; import com.itextpdf.text.*; import com.itextpdf.text.Font; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfWriter; import org.springframework.web.bind.annotation.*; import sun.misc.BASE64Decoder; import java.io.*; import java.util.UUID; /** * @Author : GaoMing * @Create_Date : 2019/10/25/17:27 * @Pack_name : com.demo.controller : pdfExport * ***************************************************** * 注释: */ @RestController @RequestMapping("/eCharts/pdf") public class PdfExportController { /** * 获取eChars生成base64编码生成图片 * @param base64Info * @return * @throws IOException */ @RequestMapping(value = "/getImg", method = RequestMethod.POST) public String getImg(@RequestParam String base64Info) throws IOException { System.out.println(base64Info); //解码Base64 BASE64Decoder decoder = new BASE64Decoder(); //切割前台传入信息 String[] arr = base64Info.split("base64,"); //获取切割后的信息 String s = arr[1]; //定义写入目录与文件名称 String picPath = "I://pic/"+ UUID.randomUUID().toString() +".png"; try { byte[] buffer = decoder.decodeBuffer(s); //IO写入硬盘 OutputStream os = new FileOutputStream(picPath); os.write(buffer); os.close(); } catch (IOException e) { throw new RuntimeException(); } System.out.println(picPath); return picPath; } /** * 由本地图片写入PDF导出 * @throws IOException * @throws DocumentException */ @RequestMapping("/export") public void export() throws IOException, DocumentException { //new 个对象 Document document = new Document(); //设置中文字体,不然不显示中文字体 TODO:必须!!! BaseFont bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, 10, Font.NORMAL); //自定义文件存储位置和文件名 String pdfPath = "I://pic/"+ UUID.randomUUID().toString() +".pdf"; //写入pdf文件 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath)); document.open(); //添加一个段落 document.add(new Paragraph("pdf 导出测试",fontChinese)); //引入本地图片路径 Image image = Image.getInstance("I:\\pic/test.png"); //设置图片位置的x轴和y轴 image.setAbsolutePosition(100f, 550f); //设置图片的宽度和高度 image.scaleAbsolute(350, 200); document.add(image); document.close(); } /** * 下载pdf * @param base64Info * @throws IOException * @throws DocumentException */ @RequestMapping(value = "/export/base64", method = RequestMethod.POST) public void down(@RequestParam String base64Info) throws IOException, DocumentException { //new 个对象 Document document = new Document(); //设置中文字体,不然不显示中文字体 TODO:必须!!! BaseFont bfChinese = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, 10, Font.NORMAL); //自定义文件存储位置和文件名 String pdfPath = "I://pic/"+ UUID.randomUUID().toString() +".pdf"; //写入pdf文件 PdfWriter.getInstance(document, new FileOutputStream(pdfPath)); document.open(); //添加一个段落 document.add(new Paragraph("pdf 导出测试",fontChinese)); Image img = this.getImage(base64Info); //设置图片位置的x轴和y轴 img.setAbsolutePosition(100f, 550f); //设置图片的宽度和高度 img.scaleAbsolute(350, 200); document.add(img); document.close(); } /** * 获取Base64编码转成的image对象 * @param base64Info * @return * @throws IOException * @throws BadElementException */ private Image getImage(String base64Info) throws IOException, BadElementException { BASE64Decoder decoder = new BASE64Decoder(); //获取base64图片编码 int i = base64Info.indexOf("base64,") + 7; byte[] buffer = decoder.decodeBuffer(base64Info.substring(i, base64Info.length() - 1)); //创建Itext Image对象 (Base64转Itext图片对象) Image img = Image.getInstance(buffer); return img; } }