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");
}
}
}