当前位置:   article > 正文

java 依赖第三方实现word完美转pdf

java 依赖第三方实现word完美转pdf

ChatGPT狂飙160天,世界已经不是之前的样子。

新建了人工智能中文站https://ai.weoknow.com
每天给大家更新可用的国内可用chatGPT资源


2023.12.5 更新
项目运行中发现,引入了org.apache.poi太多的包,导致内部有依赖冲突且很不好排查,修改后直接使用aspose-words的包就能实现word转换pdf
更新后的pom依赖

  1. <dependency>
  2. <groupId>com.luhuiguo</groupId>
  3. <artifactId>aspose-words</artifactId>
  4. <version>23.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.hutool</groupId>
  8. <artifactId>hutool-all</artifactId>
  9. <version>5.7.5</version>
  10. </dependency>

核心代码:

  1. import com.aspose.words.Document;
  2. import com.aspose.words.License;
  3. import com.aspose.words.SaveFormat;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. public class WordToPdf {
  9. public static File docToPdf(String docPath, String pdfPath) {
  10. System.out.println("WORD转化PDF开始>>>>"); //转化用时
  11. File pdfFile = new File(pdfPath);
  12. try {
  13. long old = System.currentTimeMillis();
  14. String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
  15. ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
  16. License license = new License();
  17. license.setLicense(is);
  18. Document document = new Document(docPath);
  19. FileOutputStream outputStream = new FileOutputStream(pdfFile);
  20. document.save(outputStream, SaveFormat.PDF);
  21. long now = System.currentTimeMillis();
  22. outputStream.close();
  23. is.close();
  24. System.out.println("WORD转化PDF共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
  25. } catch (Exception e) {
  26. System.out.println("转化失败");
  27. e.printStackTrace();
  28. }
  29. return pdfFile;
  30. }
  31. public static File docToPdf(InputStream docPathInputStream, String pdfPath) {
  32. File pdfFile = new File(pdfPath);
  33. try {
  34. long old = System.currentTimeMillis();
  35. String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
  36. ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
  37. License license = new License();
  38. license.setLicense(is);
  39. Document document = new Document(docPathInputStream);
  40. FileOutputStream outputStream = new FileOutputStream(pdfFile);
  41. document.save(outputStream, SaveFormat.PDF);
  42. long now = System.currentTimeMillis();
  43. outputStream.close();
  44. is.close();
  45. System.out.println("WORD转化PDF共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
  46. } catch (Exception e) {
  47. System.out.println("转化失败");
  48. e.printStackTrace();
  49. }
  50. return pdfFile;
  51. }
  52. public static void main(String[] args) {
  53. String docPath = "F:\\Users\\test\\Desktop\\test.docx";
  54. String pdfPath = "F:\\Users\\test\\Desktop\\test-convert.pdf";
  55. docToPdf(docPath,pdfPath);
  56. }
  57. }

之前的版本

最近做的一个项目中需要对接上上签实现自动签章和骑缝章的功能,但是用户这边的合同有docx也有pdf,而上上签只能对接pdf的格式,于是找了很多方法,不是表格转换有问题,就是图片转换有问题,要么就是格式上有点失真,最后找到一个比较完美的方案,记录一下,后台是springboot,文件涉及到公司机密就没有效果展示了。

  1. <dependency>
  2. <groupId>com.luhuiguo</groupId>
  3. <artifactId>aspose-words</artifactId>
  4. <version>23.1</version>
  5. </dependency>
  6. <!-- poi -->
  7. <dependency>
  8. <groupId>org.apache.poi</groupId>
  9. <artifactId>poi</artifactId>
  10. <version>5.2.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.poi</groupId>
  14. <artifactId>poi-ooxml</artifactId>
  15. <version>5.2.0</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.poi</groupId>
  19. <artifactId>poi-scratchpad</artifactId>
  20. <version>5.2.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.poi</groupId>
  24. <artifactId>poi-excelant</artifactId>
  25. <version>5.2.0</version>
  26. </dependency>
  27. <!-- itextpdf -->
  28. <dependency>
  29. <groupId>com.itextpdf</groupId>
  30. <artifactId>itextpdf</artifactId>
  31. <version>5.5.13.2</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>com.itextpdf</groupId>
  35. <artifactId>itext-asian</artifactId>
  36. <version>5.2.0</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>cn.hutool</groupId>
  40. <artifactId>hutool-all</artifactId>
  41. <version>5.7.5</version>
  42. </dependency>

核心代码

  1. import cn.hutool.core.util.StrUtil;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. /**
  8. * @author fhey
  9. * @date 2023-04-20 11:15:58
  10. * @description: 文件工具类
  11. */
  12. public class FileUtil {
  13. private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
  14. //获取新文件的全路径
  15. public static String getNewFileFullPath(String sourceFilePath, String destFilePath, String ext) {
  16. File destFile = new File(destFilePath);
  17. if (destFile.isFile()) {
  18. return destFilePath;
  19. }
  20. File sourceFile = new File(sourceFilePath);
  21. String sourceFileName = sourceFile.getName();
  22. if (sourceFile.isFile()) {
  23. return destFilePath + File.separator + sourceFileName.substring(0, sourceFileName.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + ext;
  24. }
  25. return destFilePath + File.separator + sourceFileName + StrUtil.DOT + ext;
  26. }
  27. //判断文件是否是图片
  28. public static boolean isImage(File file) throws IOException {
  29. FileInputStream is = new FileInputStream(file);
  30. byte[] bytes = new byte[8];
  31. is.read(bytes);
  32. is.close();
  33. String type = bytesToHexString(bytes).toUpperCase();
  34. if (type.contains("FFD8FF") //JPEG(jpg)
  35. || type.contains("89504E47") //PNG
  36. || type.contains("47494638") //GIF
  37. || type.contains("49492A00") //TIFF(tif)
  38. || type.contains("424D") //Bitmap(bmp)
  39. ) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. //将文件头转换成16进制字符串
  45. public static String bytesToHexString(byte[] src) {
  46. StringBuilder builder = new StringBuilder();
  47. if (src == null || src.length <= 0) {
  48. return null;
  49. }
  50. for (int i = 0; i < src.length; i++) {
  51. int v = src[i] & 0xFF;
  52. String hv = Integer.toHexString(v);
  53. if (hv.length() < 2) {
  54. builder.append(0);
  55. }
  56. builder.append(hv);
  57. }
  58. return builder.toString();
  59. }
  60. }

使用示例

  1. public static void wordToPdf(String wordPath, String pdfPath) throws Exception {
  2. pdfPath = FileUtil.getNewFileFullPath(wordPath, pdfPath, "pdf");
  3. File file = new File(pdfPath);
  4. FileOutputStream os = new FileOutputStream(file);
  5. Document doc = new Document(wordPath);
  6. doc.save(os, com.aspose.words.SaveFormat.PDF);
  7. }
  8. public static void main(String[] args) throws Exception {
  9. String filepath = "test.docx";
  10. String outpath = "/tmp";
  11. wordToPdf(filepath, outpath);
  12. }

ChatGPT狂飙160天,世界已经不是之前的样子。

新建了人工智能中文站https://ai.weoknow.com
每天给大家更新可用的国内可用chatGPT资源

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/213368
推荐阅读
相关标签
  

闽ICP备14008679号