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

相关推荐
java_python源码2 分钟前
[含文档+PPT+源码等]精品基于springboot实现的原生Andriod手机使用管理软件
java·spring boot·后端
爱喝矿泉水的猛男36 分钟前
Prompt设计技巧和高级PE
java·人工智能·prompt·cot·tot·pe·ape
十夫长40 分钟前
Java poi 模板导出Word 带图片
java·word·poi·图片·模板
ooyyaa656140 分钟前
sslSocketFactory not supported on JDK 9+
java·开发语言
技术猿188702783511 小时前
Spring Boot应用中的文件压缩与解压技术实践
java·spring boot·后端
鸣弦artha1 小时前
蓝桥杯——数组
java·数据结构·算法·蓝桥杯·eclipse·排序算法
夜魔0092 小时前
SQLite3 JDBC Java工具类
java·数据库·sqlite·工具类·sqliteutile
Bio Coder2 小时前
利用python 检测当前目录下的所有PDF 并转化为png 格式
python·pdf·批量·检测·png
蓝冰凌2 小时前
前端pdf预览方案
前端·pdf·状态模式