JAVA导出PDF并压缩成zip

这篇博客介绍了如何在JAVA中利用iText库生成PDF文件,并将多个PDF文件压缩成一个ZIP文件。首先,引入了iText及其亚洲字体支持的依赖。接着,展示了导出逻辑,遍历数据生成每个客户的PDF文件,每个文件包含交易详情和风险提示。最后,将所有PDF文件添加到ZIP文件中,返回给客户端下载。
摘要由CSDN通过智能技术生成

JAVA导出PDF借助 iText

pom先引入两个jar包

 <dependency>
                <groupId>com.itextpdf</groupId>
                 <artifactId>itextpdf</artifactId>
                <version>${itextpdf.version}</version>
            </dependency>
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itext-asian</artifactId>
                <version>${asian.version}</version>
            </dependency>

 导出逻辑(这里是生成很多pdf文件,然后压缩到一个文件里面)

@SneakyThrows
    @Override
    public  ResponseEntity<InputStreamResource>   exportTradeAckPdfFile(QueryTradeAckDetailReqDto queryTradeAckDetailDto){
        FileOutputStream fileOutputStream = null;
        PdfWriter writer=null;
        File pdfFile=null;
        Document tradeAckPdfDocument=null;
        List<File> files = new ArrayList<>();
        InputStreamResource inputStreamResource=null;
        String zipFilePath=null;
        //定义压缩文件名
        String zipFileName="交易确认单"+ DateUtils.getDateyyyyMMdd()+".zip";
        try{
            QueryTradeAckDetailReqBo reqBo= AcctUserInfoReq.Req.getReqBo(queryTradeAckDetailDto);
            //获取筛选条件内所有客户交易账号
            List<QueryCustNoRespBo> queryCustNoRespBoList= sAcktradeblotterMapper.queryCustNoListByChannelCode(reqBo);
            //每一条记录生成一个PDF文件,然后保存到本地
            for (QueryCustNoRespBo custNoRespBo:queryCustNoRespBoList) {
                QueryAccountUserTradeAckDetailReqBo queryAccountUserTradeAckDetailReqBo=new QueryAccountUserTradeAckDetailReqBo();
                queryAccountUserTradeAckDetailReqBo.setAckStartDate(reqBo.getAckStartDate());
                queryAccountUserTradeAckDetailReqBo.setAckEndDate(reqBo.getAckEndDate());
                queryAccountUserTradeAckDetailReqBo.setChannelCodes(reqBo.getChannelCodes());
                queryAccountUserTradeAckDetailReqBo.setCustNo(custNoRespBo.getCustNo());
                //单个客户信息
                QueryAccountUserInfoRespBo queryAccountUserInfoRespBo=sAcktradeblotterMapper.queryAccountUserInfo(custNoRespBo.getCustNo());
                tradeAckPdfDocument=new Document();
                //pdfFile=new File(pathProperties.getFilePath());
                zipFilePath=pathProperties.getFilePath()+"/"+zipFileName;
                pdfFile=new File(pathProperties.getFilePath()+"/"+queryAccountUserInfoRespBo.getInvName()+custNoRespBo.getCustNo()+".pdf");
                //输出文件到服务器本地
                fileOutputStream = new FileOutputStream(pdfFile);
                writer = PdfWriter.getInstance(tradeAckPdfDocument, fileOutputStream);
                writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
                tradeAckPdfDocument.setPageSize(PageSize.A4);
                tradeAckPdfDocument.open();
                tradeAckPdfDocument.add(getPdfTitleParagraph("交易对账单"));

                 //客户交易详情
                List<QueryAccountUserTradeDetailRespBo> queryAccountUserTradeDetailRespBoList=sAcktradeblotterMapper.queryAccountUserTradeDetail(queryAccountUserTradeAckDetailReqBo);
                tradeAckPdfDocument=addAccountUserTradeDetailToDocument(queryAccountUserTradeDetailRespBoList,tradeAckPdfDocument,reqBo);

                Paragraph titleParagraph = new Paragraph("    风险提示", getPdfTableTitleFont());
                tradeAckPdfDocument.add(titleParagraph);
                Paragraph firstParagraph = new Paragraph("             在线自助交易单据服务仅供客户查询及打印参考使用,不得伪造、篡改。投资者在本机构持有的基金份额及交易确", getPdfRowCellFont());
                tradeAckPdfDocument.add(firstParagraph);
                Paragraph secendParagraph = new Paragraph("    认结果以注册登记机构最终确认的结果为准。", getPdfRowCellFont());
                tradeAckPdfDocument.add(secendParagraph);              
                Paragraph fourthParagraph = new Paragraph("    快协助您解决。", getPdfRowCellFont());
                tradeAckPdfDocument.add(fourthParagraph);
                files.add(pdfFile);
                if(tradeAckPdfDocument.isOpen()){
                    tradeAckPdfDocument.close();
                }
                writer.close();
                fileOutputStream.close();
            }
            //文件夹压缩
            inputStreamResource=addFileToZipPackage(files,zipFilePath);
        }catch (Exception e) {
           log.error("导出交易确认PDF表出错",e);
        }finally {
            return ResponseEntity.ok()
                    .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS,"FileName",HttpHeaders.CONTENT_DISPOSITION)
                    .header(HttpHeaders.CONTENT_DISPOSITION,
                            "attachment;filename=" + zipFileName)
                    .header("FileName", zipFileName)
                    .contentType(MediaType.APPLICATION_OCTET_STREAM)
                    .body(inputStreamResource);
        }
    }
//为PDF文件绘制内容明细
private Document addAccountUserTradeDetailToDocument(List<QueryAccountUserTradeDetailRespBo> queryAccountUserTradeDetailRespBoList, Document document,QueryTradeAckDetailReqBo reqBo){
        try{
            document.add(getPdfTableTitleParagraph("    交易明细("+reqBo.getAckStartDate()+"-"+reqBo.getAckEndDate()+"申请数据明细)"));
            final List<String> acctUserInfoTradeDetailColumns =
                    Collections.unmodifiableList( Arrays.asList( new String[]
                            {"确认日期","基金名称","基金代码","业务类型","申请金额(元)","申请份额(份)","确认金额(元)","确认份额(份)","确认净值","手续费  (元)"} ) ) ;
            PdfPTable pdfTable =getPdfTable(10,496);
            for (String cellValue:acctUserInfoTradeDetailColumns) {
                pdfTable.addCell(getPdfHeadCell(cellValue));
            }
            for (QueryAccountUserTradeDetailRespBo rowValue:queryAccountUserTradeDetailRespBoList) {
                pdfTable.addCell(getPdfCell(rowValue.getAckDate()));
                pdfTable.addCell(getPdfCell(rowValue.getFundName()));
                pdfTable.addCell(getPdfCell(rowValue.getFundCode()));
                pdfTable.addCell(getPdfCell(rowValue.getApkType()));
                pdfTable.addCell(getPdfCenterCell(rowValue.getAppAmt()));
                pdfTable.addCell(getPdfCenterCell(rowValue.getAppBalance()));
                pdfTable.addCell(getPdfCenterCell(rowValue.getAckAmt()));
                pdfTable.addCell(getPdfCenterCell(rowValue.getAckBalance()));
                pdfTable.addCell(getPdfCenterCell(rowValue.getAckNav()));
                pdfTable.addCell(getPdfCenterCell(rowValue.getFee()));
            }
            document.add(pdfTable);
        }
        catch (Exception e){
            log.error("导出交易确认PDF表附加交易详情出错",e);
        }finally {
            return document;
        }
    }
    

    public static PdfPTable getPdfTable(int rowSize, int tableRowLength) throws Exception {
        float[] rowWidths=new float[rowSize];
        for(int i=0;i<rowSize;i++){
            rowWidths[i]=1f;
        }
        PdfPTable pdfTable = new PdfPTable(rowWidths);
        pdfTable.setLockedWidth(true);
        pdfTable.setTotalWidth(tableRowLength);
        pdfTable.setSpacingBefore(10); // 前间距
        pdfTable.setSpacingAfter(10); // 后间距

        return pdfTable;
    }

 声明字体和创建一个列

 public static Font getPdfRowCellFont() throws Exception {
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(bfChinese, 10, Font.NORMAL);
        return fontChinese;
    }
    public static Paragraph getPdfTableTitleParagraph(String tableTitle) throws Exception {
        Paragraph titleParagraph=new Paragraph(tableTitle,getPdfTableTitleFont());
        titleParagraph.setAlignment(Element.ALIGN_LEFT);
        titleParagraph.setSpacingAfter(10f);
        titleParagraph.setSpacingBefore(10f);
        return titleParagraph;
    }
    public static Font getPdfTableTitleFont() throws Exception {
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(bfChinese, 14, Font.BOLD);
        return fontChinese;
    }
    public static Paragraph getPdfTitleParagraph(String pdfTitle) throws Exception {
        Paragraph titleParagraph=new Paragraph(pdfTitle,getPdfTitleFont());
        titleParagraph.setAlignment(Element.ALIGN_CENTER);
        titleParagraph.setSpacingAfter(10f);
        titleParagraph.setSpacingBefore(10f);
        return titleParagraph;
    }
    public static Font getPdfTitleFont() throws Exception {
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(bfChinese, 18, Font.BOLD);
        return fontChinese;
    }
    public static PdfPCell getPdfCell(String cellValue) throws Exception {
        PdfPCell pdfCell = new PdfPCell();
        Paragraph paragraph = new Paragraph(cellValue, getPdfRowCellFont());
        pdfCell.setMinimumHeight(30);//设置表格行高
        pdfCell.setUseAscender(true);
        pdfCell.setVerticalAlignment(pdfCell.ALIGN_MIDDLE);
        pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfCell.setPhrase(paragraph);
        return pdfCell;
    }
    public static PdfPCell getPdfCenterCell(String cellValue) throws Exception {
        PdfPCell pdfCell = new PdfPCell();
        pdfCell.setMinimumHeight(30);//设置表格行高
        Paragraph paragraph = new Paragraph(cellValue, getPdfRowCellFont());
        pdfCell.setUseAscender(true);
        pdfCell.setVerticalAlignment(pdfCell.ALIGN_MIDDLE);
        pdfCell.setVerticalAlignment(Element.ALIGN_CENTER);
        pdfCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        pdfCell.setPhrase(paragraph);
        return pdfCell;
    }
    public static PdfPCell getPdfHeadCell(String cellValue) throws Exception {
        PdfPCell pdfCell = new PdfPCell();
        pdfCell.setMinimumHeight(30);//设置表格行高
        pdfCell.setUseAscender(true);
        pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfCell.setVerticalAlignment(pdfCell.ALIGN_MIDDLE);
        Paragraph paragraph = new Paragraph(cellValue, getPdfRowCellFont());
        pdfCell.setPhrase(paragraph);
        return pdfCell;
    }

压缩文件

 private InputStreamResource addFileToZipPackage(List<File> pdfFile,String zipFilePath) throws IOException {
        ZipOutputStream zipOutputStream=null;
        FileInputStream fileInputStream=null;
        InputStreamResource resource=null;

        File zipFile=new File(zipFilePath);
        if(zipFile.exists()){
            zipFile.delete();
        }else {
            zipFile.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
        zipOutputStream = new ZipOutputStream(fileOutputStream);
        for(File file:pdfFile){
            byte[] buf = new byte[BUFFER_SIZE];
            zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
            int len;
            fileInputStream = new FileInputStream(file);
            while ((len = fileInputStream.read(buf)) != -1) {
                zipOutputStream.write(buf, 0, len);
            }
            fileInputStream.close();
            file.delete();
        }
        zipOutputStream.close();
        //从服务器读取文件 流传给前端
        fileInputStream = new FileInputStream(zipFile) ;
        resource = new InputStreamResource(fileInputStream);
        fileOutputStream.close();
        return resource;
    }

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用ZipOutputStream类将多个txt文件打包成一个zip文件。以下是一个简单的示例代码,可以将List中的每个元素导出到单独的txt文件中,并将这些txt文件打包成一个zip文件: ```java import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ExportListToZip { public static void main(String[] args) { List<String> dataList = new ArrayList<>(); // 定义要导出的List集合 dataList.add("This is the first line."); dataList.add("This is the second line."); dataList.add("This is the third line."); String zipFileName = "data.zip"; // 定义要导出zip文件名 try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFileName))) { for (int i = 0; i < dataList.size(); i++) { String fileName = "file" + (i + 1) + ".txt"; // 定义要导出的文件名 String data = dataList.get(i); // 获取要导出的数据 BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); writer.write(data); // 写入文件内容 writer.close(); // 关闭文件流 FileInputStream fileIn = new FileInputStream(fileName); ZipEntry zipEntry = new ZipEntry(fileName); zipOut.putNextEntry(zipEntry); // 将文件添加到zip输出流中 byte[] bytes = new byte[1024]; int length; while ((length = fileIn.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); // 将文件数据写入zip输出流中 } fileIn.close(); // 关闭文件流 System.out.println("File " + fileName + " exported successfully!"); } } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们定义了一个List集合,并向其中添加了三个元素。然后定义了一个要导出zip文件名。接着,使用ZipOutputStream类创建一个新的zip文件,并使用for循环遍历List中的每个元素,生成一个文件名和要导出的数据。然后使用BufferedWriter类和FileWriter类创建一个新的文件,并将数据写入文件中。接着,使用FileInputStream类将文件数据读取出来,并使用ZipEntry类将文件添加到zip输出流中。最后,关闭文件流并输出一条消息表示文件已成功导出zip文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值