文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

java导出pdf(纯代码实现)

2023-08-17 13:21

关注

java导出pdf
在项目开发中,产品的需求越来越奇葩啦,开始文件下载都是下载为excel的,做着做着需求竟然变了,要求能导出pdf。导出pdf倒也不是特别大的问题关键就是麻烦。
导出pdf我知道的一共有3中方法:
方法一:利用模板导出,但是首先编辑模板的工具不好找,现有的国外的工具要收费,所以放弃了这个。
方法二:利用HTML页面导出,奈何自己不会写HTML,前端忙没时间帮忙写。本着求人不如靠己的想法就选择了第三种比较麻烦的方法,自己用table画。
方法三:自己用纯代码画格式(可调字体大小,颜色,对复杂没有规则的数据都可以)
首先必须导入的依赖有

                    com.itextpdf            itextpdf            5.5.10                            com.itextpdf            itext-asian            5.2.0        

然后就是一顿代码输出
先把效果贴上
在这里插入图片描述
然后是代码部分

@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。

来源地址:https://blog.csdn.net/weixin_51114236/article/details/131729816

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯