SpringBoot 导出 PDF 图表(折现图、饼状图等)

主要是基于 jfreechart + itext
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.21</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.13</version>
        </dependency>
        <!-- 模板引擎 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>core-renderer</artifactId>
            <version>R8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ognl/ognl -->
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.29</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jfree/jfreechart -->
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.5.0</version>
        </dependency>

    </dependencies>

import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.meadel.export.demo01.PdfUtil;
import lombok.extern.slf4j.Slf4j;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;


/**
 * @author
 * @version 1.0.0
 * @Description TODO
 * @createTime 2022年01月18日 09:34:00
 */
@Slf4j
@RestController
@RequestMapping("/demo4")
public class Demo4Controller {

    @RequestMapping(value = "/test", method = {RequestMethod.GET})
    public void test(HttpServletResponse response) throws Exception{
        String filename = "测试pdf";
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(filename, "UTF-8") + ".pdf");
        OutputStream os = new BufferedOutputStream(response.getOutputStream());
        // 1. Document document = new Document();
        Document document = PdfUtil.createDocument();
        // 2. 获取writer
        PdfWriter.getInstance(document, os);
        // 3. open()
        document.open();

        //设置中文样式(不设置,中文将不会显示)
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese_content = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);

        /**
         * 生成统计图
         */
        PieDataset dataset = pieDataSet();
        JFreeChart chart = ChartFactory.createPieChart3D(" 项目进度分布", dataset, true, true, false);
        PiePlot3D plot=(PiePlot3D)chart.getPlot();
        //设置Label字体
        plot.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
        //设置legend字体
        chart.getLegend().setItemFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
        // 图片中显示百分比:默认方式
        //plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
        // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
        plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
        // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
        // 设置背景色为白色
        chart.setBackgroundPaint(Color.white);
        // 指定图片的透明度(0.0-1.0)
        plot.setForegroundAlpha(1.0f);
        // 指定显示的饼图上圆形(false)还椭圆形(true)
        plot.setCircular(true);
        // 设置图标题的字体
        java.awt.Font font = new java.awt.Font(" 黑体",java.awt.Font.CENTER_BASELINE,20);
        TextTitle title = new TextTitle("项目状态分布");
        title.setFont(font);
        chart.setTitle(title);
        //图片存放位置
        String templatePath = this.getClass().getResource("/").getPath() + "/templates/statistics.jpg";
        try {
            FileOutputStream fos_jpg=new FileOutputStream(templatePath);
            ChartUtils.writeChartAsJPEG(fos_jpg,0.7f,chart,800,1000,null);
            fos_jpg.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        document.newPage();
        Paragraph pieParagraph = new Paragraph("02、饼状图测试", fontChinese_content);
        pieParagraph.setAlignment(Paragraph.ALIGN_LEFT);
        document.add(pieParagraph);
        Image pieImage = Image.getInstance(templatePath);
        pieImage.setAlignment(Image.ALIGN_CENTER);
        pieImage.scaleAbsolute(328, 370);
        document.add(pieImage);

        /**
         * 折线图
         */
        CategoryDataset lineDataset = lineDataset();
        JFreeChart lineChart = ChartFactory.createLineChart("java图书销量", "java图书", "销量", lineDataset, PlotOrientation.VERTICAL, true, true, true);
        lineChart.getLegend().setItemFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
        //获取title
        lineChart.getTitle().setFont(new java.awt.Font("宋体", java.awt.Font.BOLD, 16));

        //获取绘图区对象
        CategoryPlot linePlot = lineChart.getCategoryPlot();
        //设置绘图区域背景的 alpha 透明度 ,在 0.0f 到 1.0f 的范围内
        linePlot.setBackgroundAlpha(0.0f);

        //区域背景色
        //linePlot.setBackgroundPaint(Color.white);

        //背景底部横虚线
        linePlot.setRangeGridlinePaint(Color.gray);
        //linePlot.setOutlinePaint(Color.RED);//边界线

        // 设置水平方向背景线颜色
        // 设置是否显示水平方向背景线,默认值为true
        linePlot.setRangeGridlinesVisible(true);
        // 设置垂直方向背景线颜色
        linePlot.setDomainGridlinePaint(Color.gray);
        // 设置是否显示垂直方向背景线,默认值为false
        linePlot.setDomainGridlinesVisible(true);


        //获取坐标轴对象
        CategoryAxis lineAxis = linePlot.getDomainAxis();
        //设置坐标轴字体
        lineAxis.setLabelFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
        //设置坐标轴标尺值字体(x轴)
        lineAxis.setTickLabelFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
        //获取数据轴对象(y轴)
        ValueAxis rangeAxis = linePlot.getRangeAxis();
        rangeAxis.setLabelFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));


        /*
         * 生成图片
         */
        try {
            FileOutputStream fos = new FileOutputStream(templatePath);
            ChartUtils.writeChartAsJPEG(fos, 0.7f, lineChart, 600, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Paragraph lineParagraph = new Paragraph("02、折线图测试", fontChinese_content);
        lineParagraph.setAlignment(Paragraph.ALIGN_LEFT);
        document.add(lineParagraph);
        Image image = Image.getInstance(templatePath);
        image.setAlignment(Image.ALIGN_CENTER);
        image.scaleAbsolute(600, 300);
        document.add(image);

        // 5. close()
        document.close();
        os.close();
    }




    private static CategoryDataset lineDataset() {
        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        //Spring架构
        dataSet.addValue(6000, "Spring架构", "第一季度");
        dataSet.addValue(3000, "Spring架构", "第二季度");
        dataSet.addValue(12000, "Spring架构", "第三季度");
        dataSet.addValue(9000, "Spring架构", "第四季度");

        //Mysql解析
        dataSet.addValue(5000, "Mysql解析", "第一季度");
        dataSet.addValue(6000, "Mysql解析", "第二季度");
        dataSet.addValue(18000, "Mysql解析", "第三季度");
        dataSet.addValue(2000, "Mysql解析", "第四季度");

        //redis原理
        dataSet.addValue(9000, "redis原理", "第一季度");
        dataSet.addValue(1000, "redis原理", "第二季度");
        dataSet.addValue(15000, "redis原理", "第三季度");
        dataSet.addValue(8000, "redis原理", "第四季度");

        //Cloud深入
        dataSet.addValue(16000, "Cloud深入", "第一季度");
        dataSet.addValue(6000, "Cloud深入", "第二季度");
        dataSet.addValue(10000, "Cloud深入", "第三季度");
        dataSet.addValue(8000, "Cloud深入", "第四季度");

        //SpringBoot
        dataSet.addValue(9000, "SpringBoot", "第一季度");
        dataSet.addValue(5000, "SpringBoot", "第二季度");
        dataSet.addValue(3000, "SpringBoot", "第三季度");
        dataSet.addValue(10000, "SpringBoot", "第四季度");

        return dataSet;
    }


    private static PieDataset pieDataSet() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue(" 市场前期", new Double(10));
        dataset.setValue(" 立项", new Double(15));
        dataset.setValue(" 计划", new Double(10));
        dataset.setValue(" 需求与设计", new Double(10));
        dataset.setValue(" 执行控制", new Double(35));
        dataset.setValue(" 收尾", new Double(10));
        dataset.setValue(" 运维",new Double(10));
        return dataset;
    }

}
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class PdfUtil {
 // 标准字体
 public static Font NORMALFONT;
 // 加粗字体
 public static Font BOLDFONT;
 //固定高
 public static float fixedHeight = 27f;
 //间距
 public static int spacing = 5;

 static {
  try {
   BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
   NORMALFONT = new Font(bfChinese, 10, Font.NORMAL);
   BOLDFONT = new Font(bfChinese, 14, Font.BOLD);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static Document createDocument() {
  //生成pdf
  Document document = new Document();
  // 页面大小
  Rectangle rectangle = new Rectangle(PageSize.A4);
  // 页面背景颜色
  rectangle.setBackgroundColor(BaseColor.WHITE);
  document.setPageSize(rectangle);
  // 页边距 左,右,上,下
  document.setMargins(20, 20, 20, 20);
  return document;
 }


 /**
  * @param text 段落内容
  * @return
  */
 public static Paragraph createParagraph(String text, Font font) {
  Paragraph elements = new Paragraph(text, font);
  elements.setSpacingBefore(5);
  elements.setSpacingAfter(5);
  elements.setSpacingAfter(spacing);
  return elements;
 }


 public static Font createFont(int fontNumber, int fontSize, BaseColor fontColor) {
  //中文字体 ----不然中文会乱码
  BaseFont bf = null;
  try {
   bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
   return new Font(bf, fontNumber, fontSize, fontColor);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK);
 }

 /**
  * 隐藏表格边框线
  *
  * @param cell 单元格
  */
 public static void disableBorderSide(PdfPCell cell) {
  if (cell != null) {
   cell.disableBorderSide(1);
   cell.disableBorderSide(2);
   cell.disableBorderSide(4);
   cell.disableBorderSide(8);
  }
 }


 /**
  * 创建居中得单元格
  *
  * @return
  */
 public static PdfPCell createCenterPdfPCell() {
  PdfPCell cell = new PdfPCell();
  cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
  cell.setFixedHeight(fixedHeight);
  return cell;
 }

 /**
  * 创建指定文字得单元格
  *
  * @param text
  * @return
  */
 public static PdfPCell createCenterPdfPCell(String text, int rowSpan, int colSpan, Font font) {
  PdfPCell cell = new PdfPCell(new Paragraph(text, font));
  cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  cell.setHorizontalAlignment(Element.ALIGN_LEFT);
  cell.setFixedHeight(fixedHeight);
  cell.setRowspan(rowSpan);
  cell.setColspan(colSpan);
  return cell;
 }

 /**
  * @param len 表格列数
  * @return
  */
 public static PdfPTable createPdfPTable(int len) {
  PdfPTable pdfPTable = new PdfPTable(len);
  pdfPTable.setSpacingBefore(5);
  pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER);
  return pdfPTable;
 }
}

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
poi 版本 3.17 文件中包含 模板 测试main 数据类 需要调整下 路径 /** * @Description: * @Author: xsr * @date : 2018/7/22 9:41 */ public static void makePiePpt(List dataList) throws Exception { //打开模板ppt String mtemplateName = "E:/PIE/mtemplate/PIE" + dataList.size() + ".pptx"; String path ="E:/PIE/NewPPT/NewPIE" + dataList.size() + ".pptx"; XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(mtemplateName)); pptx.setPageSize(new Dimension(960, 580)); for (int i = 0; i < dataList.size(); i++) { makePiePpt(pptx, i, dataList); } //保存文件 OutputStream out = new FileOutputStream(path); pptx.write(out); out.close(); System.out.println("导出成功"); } /** * @Description: * @Author: xsr * @date : 2018/7/27 5:41 */ public static void makePiePpt(XMLSlideShow pptx, Integer pieNum, List dataList) throws Exception { //获取第一个ppt页面 XSLFSlide slide = pptx.getSlides().get(0); //遍历第一页元素找到图表 XSLFChart chart; List poixmlDocumentParts = new ArrayList(); for (POIXMLDocumentPart part : slide.getRelations()) { if (part instanceof XSLFChart) { chart = (XSLFChart) part; poixmlDocumentParts.add(chart); } } chart = (XSLFChart) poixmlDocumentParts.get(pieNum); POIXMLDocumentPart xlsPart = chart.getRelations().get(0); //把图表绑定到Excel workbook中 XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); CTChart ctChart = chart.getCTChart(); CTPlotArea plotArea = ctChart.getPlotArea(); CTPieChart pieChart = plotArea.getPieChartArray(0); // 获取图表的系列 CTPieSer ser = pieChart.getSerArray(0); XSSFRow row0 = sheet.createRow(0); // Series Text CTSerTx tx = ser.getTx(); tx.getStrRef().

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丢了尾巴的猴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值