java导出pdf

引入包

XML 复制代码
    <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>

直接参考代码

java 复制代码
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

相关推荐
码上小翔哥23 分钟前
Spring Boot Redis 缓存序列化踩坑记:GenericJackson2JsonRedisSerializer 的数组反序列化陷阱
java·redis
pq21723 分钟前
LambdaMetafactory(fastjson2使用的黑科技)
java
SamDeepThinking26 分钟前
你认为从0-1开发一个项目最难的地方是什么?
java·后端·架构
Devin~Y30 分钟前
大厂Java面试实战:Spring Boot/Cloud、Redis/Kafka、JVM调优与Spring AI RAG(内容社区UGC+AIGC客服场景)
java·jvm·spring boot·redis·spring cloud·kafka·mybatis
青山师40 分钟前
CompletableFuture深度解析:异步编程范式与源码实现
java·单例模式·面试·性能优化·并发编程
AI人工智能+电脑小能手40 分钟前
【大白话说Java面试题 第42题】【JVM篇】第2题:JVM内存模型有哪些组成部分?
java·开发语言·jvm·面试
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第43题】【JVM篇】第3题:GC分为哪两种?Young GC 和 Full GC有什么区别?
java·开发语言·jvm·后端·面试
Carino_U1 小时前
并发编程之CPU缓存架构&Disruptor
java·缓存·架构
小雅痞1 小时前
[Java][Leetcode middle] 54. 螺旋矩阵
java·leetcode·矩阵
ooseabiscuit1 小时前
Laravel6.x新特性全解析
java·开发语言·后端·mysql·spring