Java纯代码实现导出PDF功能

 更新时间:2023年07月21日 10:47:11   作者:Xiao5xiao122  
在项目开发中,产品的需求越来越奇葩啦,开始文件下载都是下载为excel的,做着做着需求竟然变了,要求能导出pdf,本文就来和大家分享一下Java实现导出PDF的常用方法吧

java导出pdf

在项目开发中,产品的需求越来越奇葩啦,开始文件下载都是下载为excel的,做着做着需求竟然变了,要求能导出pdf。导出pdf倒也不是特别大的问题关键就是麻烦。

导出pdf我知道的一共有3中方法:

方法一:利用模板导出,但是首先编辑模板的工具不好找,现有的国外的工具要收费,所以放弃了这个。

方法二:利用HTML页面导出,奈何自己不会写HTML,前端忙没时间帮忙写。本着求人不如靠己的想法就选择了第三种比较麻烦的方法,自己用table画。

方法三:自己用纯代码画格式(可调字体大小,颜色,对复杂没有规则的数据都可以)

首先必须导入的依赖有

<!--导出pdf所需包-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

然后就是一顿代码输出先把效果贴上

然后是代码部分

@ApiOperation(value = "导出")
    @PostMapping("/download")
    @SneakyThrows(Exception.class)
    public void download(@RequestBody @Valid FumigationDTO fumigationDTO, HttpServletResponse response, HttpServletRequest request) {
        // 防止日志记录获取session异常
        request.getSession();
        // 设置编码格式
        response.setContentType("application/pdf;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("下载的PDF名称", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".pdf");
        fumigationService.download(fumigationDTO, response);
    }

业务层

@Override
    public void download(FumigationDTO fumigationDTO, HttpServletResponse response) throws IOException {
    	//要下载的数据查询数据部分我去掉了有需要自己根据业务取
        FumigationDowloadVO fumigationDowloadVO = new FumigationDowloadVO();
        // 定义全局的字体静态变量
        Font titlefont;
        Font headfont;
        Font keyfont = null;
        Font textfont = null;
        Font content = null;
        // 最大宽度
        try {
            // 不同字体(这里定义为同一种字体:包含不同字号、不同style)
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            keyfont = new Font(bfChinese, 10, Font.BOLD);
            textfont = new Font(bfChinese, 15, Font.NORMAL);
            content = new Font(bfChinese, 10, Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseFont bf;
        Font font = null;
        try {
            //创建字体
            bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            //使用字体并给出颜色
            font = new Font(bf,20,Font.BOLD,BaseColor.BLACK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Document document = new Document(new RectangleReadOnly(842F, 595F));
        try {
            PdfWriter.getInstance(document,response.getOutputStream());
            //打开生成的pdf文件
            document.open();
            //设置内容
            Paragraph paragraph = new Paragraph("熏蒸备案回执",font);
            paragraph.setAlignment(1);
            //引用字体
            document.add(paragraph);
            // 设置表格的列宽和列数
            float[] widths = {25f,25f,25f,25f,25f,25f};
            PdfPTable table = new PdfPTable(widths);
            table.setSpacingBefore(20f);
            // 设置表格宽度为100%
            table.setWidthPercentage(100.0F);
            table.setHeaderRows(1);
            table.getDefaultCell().setHorizontalAlignment(1);
            PdfPCell cell = null;
            //第一行
            cell = new PdfPCell(new Paragraph("熏蒸备案编码",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(30);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getXzbm()));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("熏蒸备案时间",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(fumigationDowloadVO.getSqxzrq())));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("申请备案单位",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getDwmc(),content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            //第二行
            cell = new PdfPCell(new Paragraph("熏蒸作业库点",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(30);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getKdmc(),content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("负责人",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getFzr(),content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("联系电话",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getFzrdh(),content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            //第三行
            cell = new PdfPCell(new Paragraph("单据状态",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(30);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(shzt(fumigationDowloadVO.getShzt()),content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("审核时间",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString5(fumigationDowloadVO.getShsj()),content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(" ",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(" ",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);
            // 设置表格的列宽和列数
            float[] widths2 = {25f,25f,25f,25f,25f,25f};
            PdfPTable table2 = new PdfPTable(widths2);
            table2.setSpacingBefore(20f);
            // 设置表格宽度为100%
            table2.setWidthPercentage(100.0F);
            table2.setHeaderRows(1);
            table2.getDefaultCell().setHorizontalAlignment(1);
            //人员列表-第四行
            cell = new PdfPCell(new Paragraph("姓名",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(20);
            table2.addCell(cell);
            cell = new PdfPCell(new Paragraph("职务",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table2.addCell(cell);
            cell = new PdfPCell(new Paragraph("职业资格",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table2.addCell(cell);
            cell = new PdfPCell(new Paragraph("身体状况",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table2.addCell(cell);
            cell = new PdfPCell(new Paragraph("熏蒸任务分工",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table2.addCell(cell);
            cell = new PdfPCell(new Paragraph("是否外包",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table2.addCell(cell);
            //人员列表数据-第五行
            if(fumigationDowloadVO.getProples().size() > 0){
                for (RecordFumigationPeople prople : fumigationDowloadVO.getProples()) {
                    PdfPCell cell1 = new PdfPCell(new Paragraph(prople.getXm(), content));
                    PdfPCell cell2 = new PdfPCell(new Paragraph(prople.getZw(), content));
                    PdfPCell cell3 = new PdfPCell(new Paragraph(prople.getZyzg(), content));
                    PdfPCell cell4 = new PdfPCell(new Paragraph(prople.getStzk(), content));
                    PdfPCell cell5 = new PdfPCell(new Paragraph(prople.getXzrwfg(), content));
                    PdfPCell cell6 = new PdfPCell(new Paragraph(prople.getSfwb(), content));
                    //单元格对齐方式
                    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell1.setFixedHeight(20);
                    //单元格垂直对齐方式
                    cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    table2.addCell(cell1);
                    table2.addCell(cell2);
                    table2.addCell(cell3);
                    table2.addCell(cell4);
                    table2.addCell(cell5);
                    table2.addCell(cell6);
                }
            }
            // 设置表格的列宽和列数
            float[] widths3 = {25f,25f,25f,25f,25f};
            PdfPTable table3 = new PdfPTable(widths3);
            table3.setSpacingBefore(20f);
            // 设置表格宽度为100%
            table3.setWidthPercentage(100.0F);
            table3.setHeaderRows(1);
            table3.getDefaultCell().setHorizontalAlignment(1);
            //实施储粮信息
            cell = new PdfPCell(new Paragraph("仓房",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setFixedHeight(20);
            table3.addCell(cell);
            cell = new PdfPCell(new Paragraph("货位",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table3.addCell(cell);
            cell = new PdfPCell(new Paragraph("粮食品种",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table3.addCell(cell);
            cell = new PdfPCell(new Paragraph("计划熏蒸开始时间",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table3.addCell(cell);
            cell = new PdfPCell(new Paragraph("计划熏蒸结束时间",content));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table3.addCell(cell);
            if(fumigationDowloadVO.getDtls().size() > 0){
                for (RecordFumigationDtlVO dtl : fumigationDowloadVO.getDtls()) {
                    PdfPCell cell1 = new PdfPCell(new Paragraph(dtl.getCfmc(), content));
                    PdfPCell cell2 = new PdfPCell(new Paragraph(dtl.getHwmc(), content));
                    PdfPCell cell3 = new PdfPCell(new Paragraph(dtl.getLspzmc(), content));
                    PdfPCell cell4 = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(dtl.getJhxzksrq()), content));
                    PdfPCell cell5 = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(dtl.getJhxzjsrq()), content));
                    //设置居中
                    cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell1.setFixedHeight(20);
                    cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    table3.addCell(cell1);
                    table3.addCell(cell2);
                    table3.addCell(cell3);
                    table3.addCell(cell4);
                    table3.addCell(cell5);
                }
            }
            document.add(new Paragraph("\n"));
            document.add(new Paragraph("▋ 基本信息",content));
            document.add(new Paragraph("\n"));
            document.add(table);
            document.add(new Paragraph("\n"));
            document.add(new Paragraph("▋ 基本信息",content));
            document.add(new Paragraph("\n"));
            document.add(table2);
            document.add(new Paragraph("\n"));
            document.add(new Paragraph("▋ 熏蒸作业储粮粮情",content));
            document.add(new Paragraph("\n"));
            document.add(table3);
			//关闭文档
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
            log.error("导出pdf失败:{}",e);
        }
    }

然后就可以了直接导出pdf。

到此这篇关于Java纯代码实现导出PDF功能的文章就介绍到这了,更多相关Java导出PDF内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论