• JAVA使用wkhtml 将html转成pdf或Image文件


    linux的wkhtml 安装:

    linux安装wkhtmltopdf(清晰明了)_sout-lanys的博客-CSDN博客

    win的wkhtml安装:

    直接下载:wkhtmltopdf

    html 必须加UTF-8编码

               

    java代码:

    1. import lombok.extern.log4j.Log4j2;
    2. import java.io.BufferedReader;
    3. import java.io.File;
    4. import java.io.InputStreamReader;
    5. import java.nio.charset.StandardCharsets;
    6. /**
    7. *

    8. * html转换工具
    9. *

    10. *
    11. * @author Garcia
    12. * @since 2023-09-12
    13. */
    14. @Log4j2
    15. public class HtmlUtils {
    16. private final static String WIN_PDF_TOOL = "D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
    17. private final static String WIN_IMAGE_TOOL = "D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe";
    18. private final static String LINUX_PDF_TOOL = "/usr/local/bin/wkhtmltopdf";
    19. private final static String LINUX_IMAGE_TOOL = "/usr/local/bin/wkhtmltoimage";
    20. /**
    21. * html转PDF
    22. *
    23. * @param htmlPath html路径
    24. * @param pdfPath pdf路径
    25. */
    26. public static void htmlToPdf(String htmlPath, String pdfPath) {
    27. File file = new File(pdfPath);
    28. File parent = file.getParentFile();
    29. //如果pdf保存路径不存在,则创建路径
    30. if (!parent.exists()) {
    31. parent.mkdirs();
    32. }
    33. String tool;
    34. StringBuilder cmd = new StringBuilder();
    35. if (System.getProperty("os.name").contains("Windows")) {
    36. //非windows 系统
    37. tool = WIN_PDF_TOOL;
    38. }else {
    39. tool = LINUX_PDF_TOOL;
    40. }
    41. cmd.append(tool);
    42. cmd.append(" ");
    43. cmd.append("-B 0 -L 0 -R 0 -T 0 ");
    44. //开启本地文件访问
    45. cmd.append("--enable-local-file-access ");
    46. //cmd.append(" --header-line");//页眉下面的线
    47. //cmd.append(" --header-center 这里是页眉这里是页眉这里是页眉这里是页眉 ");//页眉中间内容
    48. //cmd.append(" --margin-top 3cm ");//设置页面上边距 (default 10mm)
    49. //cmd.append(" --header-html file:///" + WebUtil.getServletContext().getRealPath("") + FileUtil.convertSystemFilePath("\\style\\pdf\\head.html"));// (添加一个HTML页眉,后面是网址)
    50. //cmd.append(" --header-spacing 5 ");// (设置页眉和内容的距离,默认0)
    51. //cmd.append(" --footer-center (设置在中心位置的页脚内容)");//设置在中心位置的页脚内容
    52. //cmd.append(" --footer-html file:///" + WebUtil.getServletContext().getRealPath("") + FileUtil.convertSystemFilePath("\\style\\pdf\\foter.html"));// (添加一个HTML页脚,后面是网址)
    53. //cmd.append(" --footer-line");//* 显示一条线在页脚内容上)
    54. //cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
    55. cmd.append(htmlPath);
    56. cmd.append(" ");
    57. cmd.append(pdfPath);
    58. try {
    59. Process proc = Runtime.getRuntime().exec(cmd.toString());
    60. InputStreamReader isr = new InputStreamReader(proc.getInputStream(), StandardCharsets.UTF_8);
    61. BufferedReader br = new BufferedReader(isr);
    62. String line = null;
    63. while ((line = br.readLine()) != null) {
    64. System.out.println("html转pdf进度和信息:"+ line);
    65. }
    66. proc.waitFor();
    67. } catch (Exception e) {
    68. log.error("HTML转PDF失败",e);
    69. }
    70. }
    71. public static void htmlToImg(String srcPath, String destPath) {
    72. try {
    73. File file = new File(destPath);
    74. File parent = file.getParentFile();
    75. //如果pdf保存路径不存在,则创建路径
    76. if (!parent.exists()) {
    77. parent.mkdirs();
    78. }
    79. String tool;
    80. StringBuilder cmd = new StringBuilder();
    81. if (System.getProperty("os.name").contains("Windows")) {
    82. //非windows 系统
    83. tool = WIN_IMAGE_TOOL;
    84. }else {
    85. tool = LINUX_IMAGE_TOOL;
    86. }
    87. cmd.append(tool);
    88. cmd.append(" ");
    89. cmd.append(srcPath);
    90. cmd.append(" ");
    91. cmd.append(destPath);
    92. Process proc = Runtime.getRuntime().exec(cmd.toString());
    93. InputStreamReader isr = new InputStreamReader(proc.getInputStream(), StandardCharsets.UTF_8);
    94. BufferedReader br = new BufferedReader(isr);
    95. String line = null;
    96. while ((line = br.readLine()) != null) {
    97. System.out.println("html转jpg进度和信息:"+ line);
    98. }
    99. proc.waitFor();
    100. } catch (Exception e) {
    101. log.error("HTML转IMAGE失败",e);
    102. }
    103. }
    104. }

  • 相关阅读:
    单目深度估计: LDRN网络及代码讲解
    LeetCode-非递增子序列
    白盒测试的各种方法
    IB心理学社会文化介绍
    【算法练习Day18】二叉搜索树的最小绝对差&&二叉搜索树中的众数&& 二叉树的最近公共祖先
    解读 | 快速精确的体素GICP三维点云配准算法
    DVWA靶场Medium难度部分解析
    m4a转换成mp3,音频格式轻松转换
    Cross-species regulatory sequence activity prediction
    静电模型PIC方法的Matlab仿真设计
  • 原文地址:https://blog.csdn.net/Qensq/article/details/132838481