当在界面点击导出pdf时,向后端发起请求

前端代码:

  <el-link  id="outLink"  style="float: right;margin-left: 20px;margin-top: 5px;display: none"   ></el-link>
          <el-button v-if="active.status==2" size="small"  style="float: right;margin-left: 20px;margin-top: 5px;" @click="exportPdf()">
            导出PDF</el-button>

接口方法:

    exportPdf(){
      let _this = this;
      let link = document.getElementById("outLink");
      let id = _this.active.id
      link.href =this.$IPConfig.IpConfig + '/active/exportPdf?id='+id
      link.click();
      //释放url对象
      URL.revokeObjectURL(link.href);
      //释放a标签
      document.body.removeChild(link);
    },

如果是前后端分离项目,需要放行此地址

后端主要代码:

需要引入需要的依赖

 <!--PDF导出-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.4.3</version>
        </dependency>

接口实现:

  @Override
    public void exportPdf(HttpServletRequest request,HttpServletResponse response) throws IOException, DocumentException {
        log.info("进入导出pdf方法");
        Dto inDto = Dtos.newDto(request);
        OutActive outActive = outActiveMapper.loadById(inDto.getString("id"));
        //设置响应格式等
        response.setContentType("application/pdf");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        //设置纸张规格为A4纸
        Rectangle rect = new Rectangle(PageSize.A4);
        //创建文档实例
        Document doc=new Document(rect);
        //添加中文字体
        BaseFont bfChinese=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        //设置字体样式
        Font textFont = new Font(bfChinese,11, Font.NORMAL); //正常
        //Font redTextFont = new Font(bfChinese,11,Font.NORMAL,Color.RED); //正常,红色
        Font boldFont = new Font(bfChinese,11,Font.NORMAL); //加粗
        //Font redBoldFont = new Font(bfChinese,11,Font.BOLD,Color.RED); //加粗,红色
        Font firsetTitleFont = new Font(bfChinese,22,Font.BOLD); //一级标题
        Font secondTitleFont = new Font(bfChinese,18,Font.NORMAL, CMYKColor.BLUE); //二级标题
        Font underlineFont = new Font(bfChinese,11,Font.UNDERLINE); //下划线斜体
        //设置字体
        com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese15= new com.itextpdf.text.Font(bfChinese, 15, com.itextpdf.text.Font.NORMAL);
        com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
        com.itextpdf.text.Font FontChinese14= new com.itextpdf.text.Font(bfChinese, 14, com.itextpdf.text.Font.NORMAL);
        com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);
        com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);
        com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);

        //设置要导出的pdf的标题
        String title = outActive.getGroup_name()+outActive.getActive_time().toString().replace("T"," ")+"外联活动报备单";
        response.setHeader("Content-disposition","attachment; filename=".concat(String.valueOf(URLEncoder.encode(title + ".pdf", "UTF-8"))));
        OutputStream out = response.getOutputStream();
        PdfWriter.getInstance(doc,out);
        doc.open();
        doc.newPage();
        //新建段落
        //使用二级标题 颜色为蓝色
        Paragraph p1 = new Paragraph(title, secondTitleFont);
        //设置行高
        p1.setLeading(18);
        //设置标题居中
        p1.setAlignment(Element.ALIGN_CENTER);
        //将段落添加到文档上
        doc.add(p1);

        //设置一个空的段落,行高为18  什么内容都不显示
        Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese14);
        doc.add(blankRow1);
        //给表格设置宽度
        int width1[] = {80,60};
        int width2[] = {100};
        //新建段落
        //使用二级标题 颜色为蓝色
        Paragraph p2 = new Paragraph("报备单详情",FontChinese15);
        //设置行高
        //设置标题居中
        p2.setAlignment(Element.ALIGN_CENTER);
        //将段落添加到文档上
        doc.add(p2);
        //设置一个空的段落,行高为18  什么内容都不显示
        Paragraph blankRow2= new Paragraph(16f, " ", FontChinese14);
        doc.add(blankRow2);


        //新建表格 列数为2
        PdfPTable table1 = new PdfPTable(2);
        table1.setWidths(width1);
        //第1行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell11 = new PdfPCell(new Paragraph("报备编号:"+outActive.getId(),boldFont));
        PdfPCell cell12 = new PdfPCell(new Paragraph("申请模块:"+outActive.getGroup_name(),boldFont));
        //设置单元格边框为0
        cell11.setBorder(1);
        cell11.setPadding(5);
        cell11.setMinimumHeight(30);
        cell12.setBorder(1);
        cell12.setPadding(5);
        cell12.setMinimumHeight(30);
        table1.addCell(cell11);
        table1.addCell(cell12);
        doc.add(table1);


        PdfPTable table2 = new PdfPTable(2);
        //给表格设置宽度
        table2.setWidths(width1);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell21 = new PdfPCell(new Paragraph("申请人:  "+outActive.getCreate_person(),boldFont));
        PdfPCell cell22 = new PdfPCell(new Paragraph("活动时间:"+outActive.getActive_time().toString().replace("T"," "),boldFont));
        //设置单元格边框为0
        cell21.setBorder(1);
        cell21.setPadding(5);
        cell21.setMinimumHeight(30);
        cell22.setBorder(1);
        cell22.setPadding(5);
        cell22.setMinimumHeight(30);
        table2.addCell(cell21);
        table2.addCell(cell22);
        doc.add(table2);



        PdfPTable table3 = new PdfPTable(1);
        //给表格设置宽度
        table3.setWidths(width2);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell31 = new PdfPCell(new Paragraph("活动地点:"+outActive.getActive_address(),boldFont));
        //设置单元格边框为0
        cell31.setBorder(1);
        cell31.setPadding(5);
        cell31.setMinimumHeight(30);
        table3.addCell(cell31);
        doc.add(table3);

        PdfPTable table4 = new PdfPTable(1);
        //给表格设置宽度
        table4.setWidths(width2);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell41 = new PdfPCell(new Paragraph("接待人:  "+outActive.getReception_person(),boldFont));
        //设置单元格边框为0
        cell41.setBorder(1);
        cell41.setPadding(5);
        cell41.setMinimumHeight(30);
        table4.addCell(cell41);
        doc.add(table4);

        PdfPTable table5 = new PdfPTable(2);
        //给表格设置宽度
        table5.setWidths(width1);
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell51 = new PdfPCell(new Paragraph("接待形式:  "+outActive.getReceptionType().replace("其他:",""),boldFont));
        PdfPCell cell52= new PdfPCell(new Paragraph("来访单位性质:  "+outActive.getActiveType().replace("其他:",""),boldFont));
        //设置单元格边框为0
        cell51.setBorder(1);
        cell51.setPadding(5);
        cell51.setMinimumHeight(30);
        cell52.setBorder(1);
        cell52.setPadding(5);
        cell52.setMinimumHeight(30);
        table5.addCell(cell51);
        table5.addCell(cell52);
        doc.add(table5);

        PdfPTable table6 = new PdfPTable(1);
        //给表格设置宽度
        table6.setWidths(width2);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell6 = new PdfPCell(new Paragraph("来访单位名称:  "+outActive.getActive_organization(),boldFont));
        //设置单元格边框为0
        cell6.setBorder(1);
        cell6.setPadding(5);
        cell6.setMinimumHeight(30);
        table6.addCell(cell6);
        doc.add(table6);


        PdfPTable table7 = new PdfPTable(2);
        //给表格设置宽度
        table7.setWidths(width1);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell72= new PdfPCell(new Paragraph("车辆车牌号:  "+outActive.getCar_codes(),boldFont));
        PdfPCell cell71 = new PdfPCell(new Paragraph("来访人数:  "+outActive.getP_number(),boldFont));
        //设置单元格边框为0
        cell72.setBorder(1);
        cell72.setPadding(5);
        cell72.setMinimumHeight(30);
        table7.addCell(cell72);
        cell71.setBorder(1);
        cell71.setPadding(5);
        cell71.setMinimumHeight(30);
        table7.addCell(cell71);
        doc.add(table7);



        PdfPTable table8= new PdfPTable(1);
        //给表格设置宽度
        table8.setWidths(width2);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell8 = new PdfPCell(new Paragraph("活动对接人:  "+outActive.getDockUser()+"/"+outActive.getDockTel(),boldFont));
        //设置单元格边框为0
        cell8.setBorder(1);
        cell8.setPadding(5);
        cell8.setMinimumHeight(30);
        table8.addCell(cell8);
        doc.add(table8);

        PdfPTable table9= new PdfPTable(1);
        //给表格设置宽度
        table9.setWidths(width2);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell9 = new PdfPCell(new Paragraph("活动说明:  "+outActive.getActive_subject(),boldFont));
        //设置单元格边框为0
        cell9.setBorder(1);
        cell9.setPadding(5);
        cell9.setMinimumHeight(30);
        table9.addCell(cell9);
        doc.add(table9);


        PdfPTable table10= new PdfPTable(1);
        //给表格设置宽度
        table10.setWidths(width2);
        //第2行
        //给单元格赋值 每个单元格为一个段落,每个段落的字体为加粗
        PdfPCell cell10 = new PdfPCell(new Paragraph("备注:  "+outActive.getRemark(),boldFont));
        //设置单元格边框为0
        cell10.setBorder(1);
        cell10.setPadding(5);
        cell10.setMinimumHeight(30);
        table10.addCell(cell10);
        doc.add(table10);

        //新建段落
        //使用二级标题 颜色为蓝色
        Paragraph p3 = new Paragraph("审批流程",FontChinese15);
        //设置行高
        //设置标题居中
        p3.setAlignment(Element.ALIGN_CENTER);
        //将段落添加到文档上
        doc.add(p3);
        //设置一个空的段落,行高为18  什么内容都不显示
        Paragraph blankRow3= new Paragraph(16f, " ", FontChinese14);
        doc.add(blankRow2);

        int width3[] = {40,40,15,15,40};
        //审批流程
        PdfPTable table = new PdfPTable(5);
        table.setWidths(width3);
        PdfPCell c1 = new PdfPCell(new Paragraph("审批步骤",boldFont));
        PdfPCell c2 = new PdfPCell(new Paragraph("审批意见",boldFont));
        PdfPCell c3 = new PdfPCell(new Paragraph("审批人",boldFont));
        PdfPCell c4 = new PdfPCell(new Paragraph("状态",boldFont));
        PdfPCell c5 = new PdfPCell(new Paragraph("处理时间",boldFont));
        c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c2.setHorizontalAlignment(Element.ALIGN_CENTER);
        c3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c3.setHorizontalAlignment(Element.ALIGN_CENTER);
        c4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c4.setHorizontalAlignment(Element.ALIGN_CENTER);
        c5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c5.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        table.addCell(c2);
        table.addCell(c3);
        table.addCell(c4);
        table.addCell(c5);
        doc.add(table);

        List<GlobalApproveMain> list = globalApproveMainMapper.listByForeignId(inDto.getString("id"), approveTypeMapper.loadTypeIdByCode(ApproveTypeCode.WLHD_APPROVE));
        for (int aw = 0; aw < list.size(); aw++) {
            // 构建每一格
            PdfPTable tablex = new PdfPTable(5);
            tablex.setWidths(width3);
            GlobalApproveMain approveMain = list.get(aw);
            PdfPCell cell1 = new PdfPCell(new Paragraph(approveMain.getStep_name(),boldFont));
            PdfPCell cell2 = new PdfPCell(new Paragraph(approveMain.getContent(),boldFont));
            PdfPCell cell3 = new PdfPCell(new Paragraph(approveMain.getPerson(),boldFont));
            PdfPCell cell4 = new PdfPCell(new Paragraph(getstatus(approveMain.getStatus()),boldFont));
            PdfPCell cell5 = new PdfPCell(new Paragraph(approveMain.getCreate_time().toString().replace("T"," "),boldFont));
            cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            tablex.addCell(cell1);
            tablex.addCell(cell2);
            tablex.addCell(cell3);
            tablex.addCell(cell4);
            tablex.addCell(cell5);
            doc.add(tablex);
        }
            doc.close();
    }

最后导出效果图:

 

GitHub 加速计划 / vu / vue
100
18
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:20 天前 )
9e887079 [skip ci] 11 个月前
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐