首页>代码>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;
    }

}
最近下载更多
wedfgr  LV1 2023年5月10日
周厉王  LV1 2023年2月24日
蓝卡布布  LV1 2022年11月28日
xiaoyuer2  LV8 2022年11月20日
litchi955  LV11 2022年10月11日
hbsoft2008  LV16 2022年8月22日
唯易人生  LV3 2022年8月15日
lironggang  LV38 2022年8月12日
cxdxfx12  LV14 2022年6月7日
wanggc  LV2 2022年5月7日
最近浏览更多
lyg422 2023年11月28日
暂无贡献等级
edpwyg  LV14 2023年10月22日
zhy1989wz  LV6 2023年10月21日
漫步的海星  LV4 2023年9月26日
jinshibo 2023年9月12日
暂无贡献等级
FuckYourMother 2023年8月12日
暂无贡献等级
zuidaimayou 2023年8月8日
暂无贡献等级
Yuhaha123  LV1 2023年6月26日
getset  LV8 2023年5月17日
wedfgr  LV1 2023年5月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友