aspose-words 跳过证书验证jar

优先用 aspose-words-19.3.jar ,不需要读取license.xml,导出后直接无水印,jar包最好直接放在项目resource目录下直接引用,要不下载不下来

   public static String doc2pdf(String fileName, String filePath) {
        try {
            String oldFile = filePath + fileName;
            String newFile = oldFile.substring(0, oldFile.lastIndexOf("."))+".pdf";

            File file = new File(newFile);  //新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(oldFile);                    //Address是将要被转化的word文档
            ParagraphFormat pf=doc.getStyles().getDefaultParagraphFormat();
            pf.clearFormatting();
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            return newFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//先导出word然后转pdf


```java
public void exportFiveBsPdf(RiBaoVo.FiveBsParams dto, HttpServletResponse response) {
        OutputStream downLoadOutput = null;
        try {
            String date = dto.getRqb() + "至" + dto.getRq();
            RiBaoVo.FiveBsDataVo vo = getFiveBsData(dto);
            ClassPathResource classPathResource = new ClassPathResource("template/QGDLSCQK.docx");
            InputStream ins = classPathResource.getInputStream();
            //注册xdocreport实例并加载FreeMarker模板引擎
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins, TemplateEngineKind.Freemarker);
            //创建xdocreport上下文对象
            IContext context = report.createContext();
            //创建要替换的文本变量
            context.put("date", date);
            context.put("today", DateUtils.getStringDateShort());
            //context  put的值不能为null,会报错,用“”代替
            Class<? extends RiBaoVo.FiveBsDataVo> aClass = vo.getClass();
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                String value = ObjectUtils.isEmpty(field.get(vo)) ? "" : field.get(vo).toString();
                context.put(name, value);
            }
            downLoadOutput = response.getOutputStream();

            ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
            report.process(context, byteArrayOutputStream);

            byte[] bytes = byteArrayOutputStream.toByteArray();
            InputStream inputStream=new ByteArrayInputStream(bytes);
            Document doc = new Document(inputStream);                
            ParagraphFormat pf=doc.getStyles().getDefaultParagraphFormat();
            pf.clearFormatting();
            doc.save(downLoadOutput, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换

            response.setContentType("application/pdf");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("全国电力生产情况.pdf", "utf-8"));
            response.setHeader("Content-Type", "application/octet-stream;charset=utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (downLoadOutput != null) {
                    downLoadOutput.flush();
                    downLoadOutput.close();
                    downLoadOutput = null;
                }
            } catch (IOException e1) {
                //e1.printStackTrace();
                System.out.println("Close ServletOutputStream error. type 55");
            }
        }
    }

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值