poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)

选中图片右击  选择设置图片格式

poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)_第1张图片

poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)_第2张图片

 poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)_第3张图片

例如word模板

poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)_第4张图片

 

maven依赖

        
        
             e-iceblue 
            spire.doc.free
            3.9.0
        
 
  
        
            org.apache.poi
            poi
            4.1.2
        
        
            org.apache.poi
            poi-ooxml
            4.1.2
        
        
            org.apache.poi
            poi-ooxml-schemas
            4.1.2
        
        
            org.apache.poi
            poi-scratchpad
            4.1.2
        
 
        
        
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.pdf
            1.0.6
        
 
        
        
            fr.opensagres.xdocreport
            xdocreport
            2.0.2
        
 
        
        
            com.itextpdf
            itextpdf
            5.5.13.1
        
        
        
            com.itextpdf
            itext-asian
            5.2.0
        
 
        
        
            com.deepoove
            poi-tl
            1.9.1
        
 
 
  
 
 
         
        
            com.e-iceblue
               http://repo.e-iceblue.cn/repository/maven-public/
           
      
 
 
        
 
 
 
 
 
 
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                
                    true
                
            
        
    
 

 

  
        
            com.deepoove
            poi-tl
            1.9.1
        


        
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.pdf
            1.0.6
        


        
            fr.opensagres.xdocreport
            xdocreport
            2.0.2
        

读取

poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)_第5张图片

 代码

 @GetMapping("exportPDF")
    public AjaxResult exportPDF(HttpServletResponse response) throws Exception {
        LoginUser loginUser = getLoginUser();
        OpenCompanyInfo companyInfo = openCompanyInfoService.selectEnterpriseInfo(loginUser.getId());
        if (companyInfo != null) {
            Map data = new HashMap<>();
            data.put("legalDeputy", companyInfo.getLegalDeputy());
            data.put("companyName", companyInfo.getCompanyName());
            data.put("companyCode", companyInfo.getCompanyCode());
            if (StringUtils.isNoneBlank(companyInfo.getBusinessLicenseUrl())) {
                InputStream businessLicenseUrl = minioClientUtils.returnInputStream(null, companyInfo.getBusinessLicenseUrl());
                data.put("businessLicenseUrl", Pictures.ofStream(businessLicenseUrl, PictureType.JPEG).size(120, 120).create());
            }
            if (StringUtils.isNoneBlank(companyInfo.getCompanyLogoUr())) {
                InputStream companyLogoUrl = minioClientUtils.returnInputStream(null, companyInfo.getCompanyLogoUr());
                data.put("companyLogoUrl", Pictures.ofStream(companyLogoUrl, PictureType.JPEG).size(120, 120).create());
            }
            InputStream inputStreamFile = ResourceReader.class.getResourceAsStream("/templates/模版.docx");
            XWPFTemplate template = XWPFTemplate.compile(inputStreamFile).render(data);
//            XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\11949\\Desktop\\模版.docx").render(data);
            byte[] array = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            template.writeAndClose(baos);
            array = baos.toByteArray();
            baos.close();
            template.close();
            InputStream inputStream = new ByteArrayInputStream(array);
            ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
            fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(xwpfDocument, pdfBaos, PdfOptions.create());
            inputStream.close();
            xwpfDocument.close();
            response.setContentType("application/octet-stream");
            String format = DateUtil.format(new Date(), "yyyy-MM-dd");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("认证授权函" + format + ".pdf", "UTF-8"));
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            pdfBaos.writeTo(bos);
            bos.flush();
            out.flush();
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
        }
        return null;
    }

你可能感兴趣的:(poi)