当前位置: 首页 > news >正文

java导出pdf

引入包

    <properties><itext.version>8.0.5</itext.version></properties><dependencies><dependency><groupId>com.itextpdf</groupId><artifactId>itext-core</artifactId><version>${itext.version}</version><type>pom</type></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>bouncy-castle-adapter</artifactId><version>${itext.version}</version></dependency></dependencies>

直接参考代码

import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;@ApiOperation("导出餐别统计")@PostMapping("/exportMealPdf")public void exportMealPdf(@RequestBody ChargeTypeReportPageDTO query, HttpServletResponse response) throws IOException {response.setContentType("application/pdf");response.setCharacterEncoding("utf-8");// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系String fileName = URLEncoder.encode("餐别统计表", "UTF-8").replaceAll("\\+", "%20");response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".pdf");PdfDocument pdfDoc = new PdfDocument(new PdfWriter(response.getOutputStream()));Document doc = new Document(pdfDoc);// 设置中文字体PdfFont pdfFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);;doc.setFont(pdfFont);doc.add(new Paragraph("With 3 columns:"));Table table = new Table(UnitValue.createPercentArray(new float[] {10, 10, 80}));table.setMarginTop(5);table.addCell("Col a");table.addCell("Col b");table.addCell("Col c");table.addCell("Value a");table.addCell("Value b");table.addCell("This is a long description for column c. " +"It needs much more space hence we made sure that the third column is wider.");doc.add(table);doc.add(new Paragraph("With 2 columns:"));table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();table.setMarginTop(5);table.addCell("Col a");table.addCell("Col b");table.addCell("Value a");table.addCell("Value b");table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +"It needs much more space hence we made sure that the third column is wider.")));table.addCell("Col a");table.addCell("Col b");table.addCell("Value a");table.addCell("Value b");table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +"It needs much more space hence we made sure that the third column is wider.")));doc.add(table);doc.close();}

官网地址

itext-publications-examples-java/src/main/java/com/itextpdf/samples/sandbox/tables/SimpleTable9.java at master · itext/itext-publications-examples-java · GitHub

http://www.lryc.cn/news/486413.html

相关文章:

  • muduo之线程同步CountDownLatch
  • 【Python系列】Python中打印详细堆栈信息的技巧
  • SpringBoot中监听器、过滤器、拦截器和AOP详解
  • 如何让手机ip变成动态
  • [Qt platform plugin问题] Could not load the Qt platform plugin “xcb“
  • 嵌入式开发人员如何选择合适的开源前端框架进行Web开发
  • MySQL数据库(七)----查询相关操作(子查询)
  • 01_Spring开胃菜
  • SpringBoot使用AspectJ的@Around注解实现AOP全局记录接口:请求日志、响应日志、异常日志
  • WPF下播放Rtmp的解决方案
  • 7.高可用集群架构Keepalived双主热备原理
  • 为以人工智能为中心的工作负载重新设计的全局控制台
  • go channel中的 close注意事项 range取数据
  • Vue3 -- 项目配置之eslint【企业级项目配置保姆级教程1】
  • 鸿蒙开发应用权限管理
  • 【数据库】如何保证数据库迁移过程中数据的一致性?
  • C++之内存管理
  • ISP是什么?
  • 机房动环境监控用各种列表已经淘汰了,现在都是可视化图表展示了
  • RHCE的练习(12)
  • uniapp自动注册机制:easycom
  • 【论文阅读】(Security) Assertions by Large Language Models
  • C++ 编程基础(5)类与对象 | 5.8、面向对象五大原则
  • node.js中express的基本了解
  • AI大模型(一):Prompt AI编程
  • ArcGIS Pro属性表乱码与字段名3个汉字解决方案大总结
  • 小程序-基于java+SpringBoot+Vue的驾校预约平台设计与实现
  • 计算机网络网关简介
  • 如何用python将pdf转换为json格式
  • STL关联式容器介绍