• java 通过 冰蓝 word 转pdf ,最大程度包装pdf 样式和word接近


     所需依赖

    1. <dependency>
    2. <groupId>e-icebluegroupId>
    3. <artifactId>spire.doc.freeartifactId>
    4. <version>3.9.0version>
    5. dependency>
    6. <repositories>
    7. <repository>
    8. <id>com.e-iceblueid>
    9. <name>e-icebluename>
    10. <url>http://repo.e-iceblue.com/nexus/content/groups/public/url>
    11. repository>
    12. repositories>

     测试代码

    1. public static void main(String[] args) {
    2. Document document =new Document();
    3. document.loadFromFile("C:\\Users\\11949\\Desktop\\工作票3.docx");
    4. document.saveToFile("C:\\Users\\11949\\Desktop\\工作票3.pdf", FileFormat.PDF);
    5. }

    实际使用

    1. @GetMapping(value = "/exportWordTestPDFUrl")
    2. public R exportWordTestUrl(@RequestParam Map mapCon , HttpServletResponse response ) throws Exception {
    3. R resultBody = replaceWordDataService.replaceWord( mapCon );
    4. if(resultBody.getCode() == 0 ){
    5. byte[] array = null;
    6. ExportWordDTO data = (ExportWordDTO) resultBody.getData();
    7. XWPFTemplate template = data.getXwpfTemplate();
    8. ByteArrayOutputStream baos = new ByteArrayOutputStream();
    9. template.writeAndClose( baos );//文档写入流
    10. array = baos.toByteArray();
    11. baos.close();
    12. template.close();
    13. // 替换后的word转流
    14. InputStream inputStream = new ByteArrayInputStream( array ) ;
    15. Document document =new Document();
    16. document.loadFromStream(inputStream,FileFormat.Docx_2013);
    17. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    18. document.saveToStream(outputStream, FileFormat.PDF);
    19. byte[] pdfArray = null;
    20. pdfArray = outputStream.toByteArray();
    21. outputStream.close();
    22. R upload =
    23. remoteFileService.upload(pdfArray, 1, applicationName, data.getOutFileName()+".pdf");
    24. if (null != upload && upload.getCode() == 0){
    25. // 将url 返回前端
    26. ResultFileModel fileModel = upload.getData();
    27. String path = fileModel.getAddress() + fileModel.getPath();
    28. return R.ok().data( path );
    29. }else {
    30. return R.fail().msg( "文件上传失败!" );
    31. }
    32. }
    33. return R.fail().msg(resultBody.getMsg());
    34. }

  • 相关阅读:
    机器学习基础篇(4)滤波器
    超全整理,Pytest自动化测试框架-多进程(pytest-xdist)运行总结...
    Docker搭建nginx
    ASP.NET Core - 缓存之分布式缓存
    Uniapp扫码预览连接地址与手机不在同一网段
    vue预览txt
    Vue3 动态组件 component:is= 失效
    在全链路追踪中加入对方法(Method)追踪
    学习 MySQL 需要知道的 28 个小技巧
    centos7在线安装rabbitmq及其远程连接
  • 原文地址:https://blog.csdn.net/qq_36961226/article/details/126764607