PDF导出

PDF导出

controller控制层

    public void export(queryVo vo, PageQuery pageQuery,HttpServletResponse response) {
        TableDataInfo<TaskAudit> auditTableDataInfo = iTaskAuditService.queryPageAuditList(bo, pageQuery);
        try {
            iTaskAuditService.createPDF(auditTableDataInfo.getRows(), pageQuery,response);
        } catch (Exception e) {
            throw new RuntimeException(e);;
        }
    }

service实现类

public void createPDF(List<TaskAudit> taskAudits, PageQuery pageQuery, HttpServletResponse response) throws Exception {
        String fileName = "导出_" + LoginHelper.getUsername() + "_" + DateUtils.parseDateToStr("yyyy-MM-dd", new Date()) + ".pdf";
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
        
        BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font headFont = new Font(bf, 18, Font.BOLD);
        Font textFont = new Font(bf, 12, Font.NORMAL);
        int column = 2;
        Document document = new Document();
        OutputStream out = response.getOutputStream();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, out);
            document.open();
            document.setPageSize(PageSize.A4);
            PdfPTable table = ExcelUtil.createTable(column);
            table.addCell(ExcelUtil.createTitle("导出", headFont, Element.ALIGN_CENTER, column, false));
            //
            for (int i = 0; i < taskAudits.size(); i++) {
                TaskAudit taskAudit = taskAudits.get(i);
                table.addCell(ExcelUtil.createCell(String.valueOf(i + 1), textFont));
                PdfPCell taskCell = ExcelUtil.createCell(
                    (taskAudit.getTaskName() + "—:" + taskAudit.getTaskCode() +
                        "\n 投放周期:" + taskAudit.getServingStartTime().substring(0, 9) + "~" + taskAudit.getServingEndTime().substring(0, 9) +
                        "\n 公司:" + taskAudit.getSupplierName() + "\n 供应商:" + taskAudit.getDeptName()), textFont);
                table.addCell(taskCell);
                //广告点位
                if (taskAudit.getAuditStatus() >= 1) {
                    List<TaskAudit> pointInfo = this.selectAuditInfo(taskAudit.getId(), pageQuery).getRows();
                    for (int k = 0; k < pointInfo.size(); k++) {
                        for (int j = 0; j < column; j++) {
                            TaskAudit pointRecordVo = pointInfo.get(k);
                            if (j == 0) {
                                PdfPCell black1 = ExcelUtil.createCell("", textFont);
                                black1.disableBorderSide(3);
                                table.addCell(black1);
                            } else {
                                String auditStatusName = "";
                                if (pointRecordVo.getAuditStatus() == 1) {
                                    auditStatusName = "待审核";
                                } else if (pointRecordVo.getAuditStatus() == 2) {
                                    auditStatusName = "审核不通过";
                                } else if (pointRecordVo.getAuditStatus() == 3) {
                                    auditStatusName = "审核通过";
                                }
                                String auditTime;
                                if (pointRecordVo.getAuditTime() == null || ("").equals(pointRecordVo.getAuditTime())) {
                                    auditTime = "";
                                } else {
                                    auditTime = DateUtils.parseDateToStr("yyyy-MM-dd", pointRecordVo.getAuditTime());
                                }
                                PdfPCell pointCell = ExcelUtil.createCell((pointRecordVo.getPointName() + "—:" + pointRecordVo.getPointCode()
                                    + "\n 审核结果:" + auditStatusName
                                    + "\n 审核人:" + Optional.ofNullable(pointRecordVo.getAuditName()).orElse("")
                                    + "\n 审核时间:" + auditTime), textFont);
                                pointCell.disableBorderSide(2);
                                table.addCell(pointCell);
                                //设置图片table
                                PdfPCell black2 = ExcelUtil.createCell("", textFont);
                                black2.disableBorderSide(3);
                                table.addCell(black2);
                                PdfPCell cells = new PdfPCell();
                                cells.setFixedHeight(180);
                                String imgUrl = pointRecordVo.getSmallFileUrl();
                                String[] imgList = imgUrl.split(",");
                                PdfPTable imgTable = new PdfPTable(imgList.length);
                                for (int l = 0; l < imgList.length; l++) {
                                    String imgPath = fileService.getShareUrl(imgList[l]);
                                    Image image = Image.getInstance(imgPath);
                                    image.setWidthPercentage(20.0f);
                                    PdfPCell cell = new PdfPCell(image, true);
                                    cell.disableBorderSide(15);
                                    imgTable.addCell(cell);
                                }
                                cells.addElement(imgTable);
                                cells.disableBorderSide(1);
                                table.addCell(cells);
                            }
                        }
                    }
                }
            }
            document.add(table);
            document.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        return file;
    }
}

Excelutil工具类

    public static PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_LEFT);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建PDF文件表格
     * @param colNumber
     * @return
     */
    public static PdfPTable createTable(int colNumber) {
        PdfPTable table = new PdfPTable(colNumber);
        table.setLockedWidth(true);
        table.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setBorder(1);
        int size = 2;
        float width[] = new float[size];
        for (int i = 0; i < size; i++) {
            if (i == 0) {
                width[i] = 60;
            } else {
                width[i] = 520;
            }
        }
        try {
            table.setTotalWidth(width);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        return table;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Node.js中,可以使用一些库来导出PDF文件。一个常用的库是`pdfkit`,它允许你以编程的方式创建PDF文件。你可以使用以下代码示例来导出PDF文件: ```javascript const PDFDocument = require('pdfkit'); const fs = require('fs'); const doc = new PDFDocument(); // 创建一个PDF文档 doc.pipe(fs.createWriteStream('output.pdf')); // 将PDF文档写入文件 // 在文档中添加内容 doc.fontSize(12) .text('Hello World!', 100, 100); doc.end(); // 结束文档的编写 ``` 这段代码创建了一个PDF文档,并在文档中添加了一个文本内容"Hello World!"。最后,使用`doc.end()`结束文档的编写,并将其写入到名为"output.pdf"的文件中。 请注意,这只是一个基本的示例。你可以根据自己的需求,使用`pdfkit`库来添加更多的内容和样式到PDF文件中。希望这对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [node js批量导出PDF 导出压缩为zip 拆分设计 文件压缩 批量导出设计](https://blog.csdn.net/lizhen_software/article/details/82871067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [node js 批量处理pdf,提取关键信息,并导出excel](https://blog.csdn.net/weixin_34072637/article/details/91421708)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值