生成表格pdf格式

1. 添加依赖

java 复制代码
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.2.5</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.2.5</version>
        </dependency>

2.demo代码

java 复制代码
    @SneakyThrows
    public void export(List list, OutputStream outputStream) {
        List<JbMaintainPlanEntity> list = planEntityService.list(dto);
        // 创建 PDF 写入器和文档对象
        PdfWriter writer = new PdfWriter(outputStream);
        PdfDocument pdfDocument = new PdfDocument(writer);
        Document document = new Document(pdfDocument);
        //支持中文,指定字符集
        InputStream resourceAsStream = resourceLoader.getClassLoader().getResourceAsStream("front/simsun.ttf");
        byte[] bytes = IoUtil.readBytes(resourceAsStream);
        FontProgram fontProgram = FontProgramFactory.createFont(bytes);
        PdfFont font = PdfFontFactory.createFont(fontProgram);
        document.setFont(font);
        // 创建表格,并设置列数为data中的键的数量
        float[] columnWidths = {2f, 2f, 2f, 3f, 3f, 2f};
        Table table = new Table(columnWidths);
        table.setWidth(UnitValue.createPercentValue(80));
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 添加表头
        String[] headers = {"序号", "设备分类", "设备机型", "设备", "轮保日期", "备注"};
        for (String header : headers) {
            Cell cell = new Cell().add(new Paragraph(header));
            cell.setTextAlignment(TextAlignment.CENTER);
            table.addHeaderCell(cell);
        }
        Integer count = 1;
        for (Objetc data: list) {
            Map<String, Object> map = new HashMap<>();
            BeanUtil.beanToMap(data, map, false, false);
            table.addCell(new Cell().add(new Paragraph(count.toString())).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("name"))))
                    .setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("model")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("age")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(String.valueOf(map.get("date")))).setTextAlignment(TextAlignment.CENTER));
            table.addCell(new Cell().add(new Paragraph(ObjectUtil.isNull(map.get("remark")) ? "" : String.valueOf(map.get("remark"))))
                    .setTextAlignment(TextAlignment.CENTER));
        }
        // 将表格添加到文档中
        document.add(table);

        // 关闭文档
        document.close();
    }

3.字符集下载

https://gitcode.com/open-source-toolkit/c17ea/overview

相关推荐
敲代码的小吉米5 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
东方巴黎~Sunsiny9 小时前
EasyExcel导出excel再转PDF转图片详解
pdf·excel
aklry12 小时前
uniapp实现在线pdf预览以及下载
前端·pdf·uni-app
繁依Fanyi12 小时前
我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
java·pdf·uni-app·生活·harmonyos·codebuddy首席试玩官
Mast Sail16 小时前
windows下authas调试tomcat
java·windows·tomcat·authas
疯狂的挖掘机16 小时前
记一次从windows连接远程Linux系统来控制设备采集数据方法
linux·运维·windows
DevOpenClub18 小时前
PPT 转高精度 PDF API 接口
pdf·powerpoint
前进的程序员18 小时前
C++ 在 Windows 和 Linux 平台上的开发差异及常见问题
linux·c++·windows
大笨象、小笨熊20 小时前
【Win32 API】 lstrcpyA()
windows
双叶83620 小时前
(C语言)超市管理系统 (正式版)(指针)(数据结构)(清屏操作)(文件读写)
c语言·开发语言·数据结构·c++·windows