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

相关推荐
lybugproducer7 分钟前
浅谈 Redis 数据类型
java·数据库·redis·后端·链表·缓存
purrrew33 分钟前
【Java ee初阶】网络编程 UDP socket
java·网络·网络协议·udp·java-ee
新老农43 分钟前
php数据导出pdf,然后pdf转图片,再推送钉钉群
pdf·php·钉钉
上海合宙LuatOS1 小时前
全栈工程师实战手册:LuatOS日志系统开发指南!
java·开发语言·单片机·嵌入式硬件·物联网·php·硬件工程
多敲代码防脱发1 小时前
导出导入Excel文件(详解-基于EasyExcel)
java·开发语言·jvm·数据库·mysql·excel
一刀到底2111 小时前
做为一个平台,给第三方提供接口的时候,除了要求让他们申请 appId 和 AppSecret 之外,还应当有哪些安全选项,要过等保3级
java·网络·安全
wjcurry1 小时前
我的实习日报
java·redis·mysql
我喜欢山,也喜欢海2 小时前
Jenkins Maven 带权限 搭建方案2025
java·jenkins·maven
明天更新2 小时前
Java处理压缩文件的两种方式!!!!
java·开发语言·7-zip