目前遇到个根据html转Java的功能,使用itext可以快速的实现pdf打印下载的功能,在itext中我们一般有以下三中方式实现

  1. 配置pdf模板,通过Adobe Acrobat 来设置域最后通过代码将数据填充进去
  2. 通过FreeMarker或thymeleaf配置html模板填充数据
  3. Jsoup+XMLWorkerHelper对于上述的三种方式.

我简述下我的体验:第一种方式对于入门简单,如果我们需求中的pdf文件是表格或者报表的样式还是很好实现的,但如果遇到要求和html样式一致的话就基本歇菜了。第二张方式比较理想,在项目基本完工的情况下再去改成模板不太现实,不选。第三种方式我也尝试了下,对于一些简单网页比如说博客,我们通过Jsoup可以获取到文章的内容和html样式,网上的demo也是通过博客来举例的,效果不错,但我看了下博客内容的基本样式就是一些基本的div、p、li这些标签,但在我实际的项目中样式比较复杂,生成的pdf无法打开。而且上面的三种方式都有一个致命的缺点:那就是需要去针对模板。现实的项目中我们可能多个地方需要实现pdf打印的功能,样式模板的配置将占用很大的开发工作量。第三种方式虽说不需要模板,但通过网页的标签去获取数据也会变得多样化,完全不能实现方法的复用。当然,itext也不是一无是处,在这个项目里面我们还是需要用到itext去生成水印、加密、不可编辑等一系列细致的活。

这里我想要的理想的pdf打印效果是:最少的改动,实现pdf的打印的效果与我请求的url的html样式一致。那就要使用到我们今天的主角wkhtmltopdf,完美的解决了我的难题,windows下解压即用,linux下需要安装三个依赖和一个宋体文字,下面让我们一起coding吧~

详细步骤:

  1. windows下wkhtmltopdf的下载安装测试     
  2. linux下的wkhtmltopdf的下载安装测试
  3. 配置pdf生成的路径解读
  4. 自动调用java中Runtime.getRuntime()运行wkhtmltopdff插件的具体实现

 

通过https://wkhtmltopdf.org/downloads.html下载并解压缩wkhtmltox文件,通过cmd 命令找到 bin下面的wkhtmltopdf.exe 文件,执行wkhtmltopdf.exe +http url + pdf路径及文件名

 

wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4.tar.bz2 用wget下载wkhtmltopdf安装,这里我们采用的是0.12.4的版本,没有和上面windows的0.12.5保持一致原因是没有0.12.5的tar包,rpm的不习惯。安装完之后我们参考上面windows的方式运行,运行过程中在centos 7中会遇到缺少依赖的问题。执行yum -y install libXrender* libfontconfig* libXext* 安装后重新运行测试,还会遇到中文乱码问题,这这里我们还需将windows下的simsun字体放置到 /usr/share/fonts/ ,windows字体的存放位置为C:\Windows\Fonts,win10部分用户的simsun字体是没效果的,linux下一般就这些问题。这里就不做演示了,我们开始真正的coding吧。

 

  1. # linux path
  2. #wkhtmltopdfPath 指向wkhtmltopdfPath安装的bin目录下wkhtmltopdf
  3. wkhtmltopdfPath=/home/wkhtmltox/bin/wkhtmltopdf
  4. #pdf
  5. pdfLocationPathForLinux=/home/prd-gehcpp-01/otoResources/pdf/
  6. waterPdfLocationPathForLinux=/home/prd-gehcpp-01/otoResources/waterPdf/
  7. # windows path
  8. #photoLocationPath=D://otoResources//photo//
  9. #pdf文件路径
  10. pdfLocationPath= D://otoResources//pdf//
  11. #添加水印后文件路径
  12. waterPdfLocationPath= D://otoResources//waterPdf//

上面配置中我们分别配置了linux和windows两套路径环境,在我们代码中我们会自动判别当前服务器的操作系统,然后选择相关的路径。下面我们分别解读下各个配置参数

  1. wkhtmltopdfPath 指向wkhtmltopdfPath安装的bin目录下wkhtmltopdf,这段地址其实就是为了执行wkhtmltopdf插件而添加的,这里我们用全路径,这样在linux的环境下我们就不需要去配置环境了,另外细心的朋友会发现windows的没有配置,这是因为我已经将插件放到了资源文件夹下,
    然后通过Java程序去拼接了一个完整的路径,具体我们往下看。
  1. pdfLocationPathForLinux/photoLocationPath 这两个路径分别指向了linuxWindows下的pdf生成的初始文件,如果我们不需要对pdf进行二次操作的话,在这里我们就可以把结果返回给用户。
  1. waterPdfLocationPathForLinux/waterPdfLocationPath :这个路径是指经过加工后的路径,比如水印、加密、不可编辑等,上面的路径相当于是模子,这个才是最终返回客户的结果。

部分代码参考 https://www.cnblogs.com/xionggeclub/p/6144241.html

controller

  1. /**
    *
    * @param response
    * @param httpUrl 需要生成pdf的请求地址
    * @param downName 生成pdf后的文件名
    * @return
    */
    @RequestMapping("pdfConvert")
    @ResponseBody
    public Response htmlToPdf(HttpServletResponse response ,@RequestParam String httpUrl,String downName) {
    return geFileService.htmlToPdf(response,httpUrl,downName);
    }

service 

  1. Response htmlToPdf(HttpServletResponse response, String httpUrl,String downName);

impl

  1.  
  1. package com.ge.service.impl;

    import com.common.exception.MyException;
    import com.ge.service.GeFileService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Service;
    import pdf.HtmlToPdfUtil;
    import utils.Response;
    import utils.ResultHttpStatus;

    import javax.servlet.http.HttpServletResponse;
    import java.io.*;
    import java.util.UUID;

    import static utils.PropertiesUtil.getProperty;

    /**
    * @Author: gaofeng_peng
    * @Date: 2018/6/13 13:47
    */
    @Service("GeFileService")
    public class FileServiceImpl implements GeFileService {
    private Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    public static String pdfLocationPath;

    public static String waterPdfLocationPath;

    public static String systemOs;
    /**
    * 根据服务器系统设置pdf文件路径
    */
    static {
    systemOs = System.getProperty("os.name");
    if (systemOs.toLowerCase().startsWith("win")) {
    pdfLocationPath = getProperty("pdfLocationPath");
    waterPdfLocationPath = getProperty("waterPdfLocationPath");
    } else {
    pdfLocationPath = getProperty("pdfLocationPathForLinux");
    waterPdfLocationPath = getProperty("waterPdfLocationPathForLinux");
    }
    }



    @Override
    public Response htmlToPdf(HttpServletResponse res, String httpUrl, String downName) {
    Response response = new Response(ResultHttpStatus.OK.getValue(), ResultHttpStatus.OK.getName());
    //判断路径是否存在,不存在则创建
    File fileDir = new File(pdfLocationPath);
    File waterFileDir = new File(waterPdfLocationPath);
    if (!fileDir.exists()) {
    fileDir.setWritable(true);
    fileDir.mkdirs();
    }
    if (!waterFileDir.exists()) {
    waterFileDir.setWritable(true);
    waterFileDir.mkdirs();
    }
    String fileName = UUID.randomUUID().toString() + ".pdf";
    String source = pdfLocationPath + fileName;
    String outPath = waterPdfLocationPath + fileName;
    try {
    //调用HtmlToPdfUtilz中的convent的方法使html生成pdf
    boolean isSuccess = HtmlToPdfUtil.convert(httpUrl, source,systemOs);
    if (isSuccess) {
    //水印加密
    // HtmlToPdfUtil.setWaterMarkForPDF(source,outPath,"");
    download(res, downName, fileName);
    } else {
    throw new MyException("pdf下载异常");
    }

    } catch (Exception e) {
    response.setMsg(e.getMessage());
    response.setStatus(ResultHttpStatus.INTERNAL_ERROR.getValue());
    }
    return response;
    }

  1. /**
    * 响应下载
    * @param resp
    * @param downloadName 下载的pdf文件名
    * @param fileName 系统中真正的文件名
    */
  1. public void download(HttpServletResponse resp, String downloadName, String fileName) {
    try {
    downloadName = new String(downloadName.getBytes("GBK"), "ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    String realPath = pdfLocationPath;
    String path = realPath + fileName;
    File file = new File(path);
    resp.reset();
    resp.setContentType("application/octet-stream");
    resp.setCharacterEncoding("utf-8");
    resp.setContentLength((int) file.length());
    resp.setHeader("Content-Disposition", "attachment;filename=" + downloadName + ".pdf");
    byte[] buff = new byte[1024];
    BufferedInputStream bis = null;
    OutputStream os = null;
    try {
    os = resp.getOutputStream();
    bis = new BufferedInputStream(new FileInputStream(file));
    int i = 0;
    while ((i = bis.read(buff)) != -1) {
    os.write(buff, 0, i);
    os.flush();
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    bis.close();
    //完成后删除本地pdf文件
    file.delete();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    }
  1.  

 

HtmlToPdfUtil类

  1. package pdf;
  2. import com.itextpdf.text.BaseColor;
  3. import com.itextpdf.text.pdf.BaseFont;
  4. import com.itextpdf.text.pdf.PdfContentByte;
  5. import com.itextpdf.text.pdf.PdfReader;
  6. import com.itextpdf.text.pdf.PdfStamper;
  7. import org.apache.commons.lang3.StringUtils;
  8. import utils.PropertiesUtil;
  9. import java.io.FileOutputStream;
  10. /**
  11. * @Author: gaofeng_peng
  12. * @Date: 2018/7/5 13:13
  13. */
  14. public class HtmlToPdfUtil {
  15. //wkhtmltopdf在系统中的路径
  16. // private static final String toPdfTool = "D:\\wkhtmltox\\bin\\wkhtmltopdf.exe";
  17.  
  18.  
  19. /**
  20. * html转pdf
  21. *
  22. * @param srcPath html路径
  23. * @param pdfLocationPath pdf保存路径
  24. * @return 转换成功返回true
  25. */
  26. public static boolean convert(String srcPath, String pdfLocationPath, String systemOs) throws Exception {
  27. String toPdfTool="";
  28. if (systemOs.toLowerCase().startsWith("win")) {
  29. toPdfTool = HtmlToPdfUtil.class.getClassLoader().getResource("wkhtmltox/bin/wkhtmltopdf.exe").getPath();
  30. }else {
  31. toPdfTool= PropertiesUtil.getProperty("wkhtmltopdfPath");
  32. }
  33. StringBuilder cmd = new StringBuilder();
  34. cmd.append(toPdfTool);
  35. cmd.append(" ");
  36. cmd.append(" --background");
  37. cmd.append(" --debug-javascript");
  38. cmd.append(" --header-line");//页眉下面的线
  39. cmd.append(" --header-spacing 10 ");// (设置页眉和内容的距离,默认0)
  40. cmd.append(srcPath);
  41. cmd.append(" ");
  42. cmd.append(pdfLocationPath);
  43. boolean result = true;
  44. try {
  45. Process proc = Runtime.getRuntime().exec(cmd.toString());
  46. HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
  47. HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
  48. error.start();
  49. output.start();
  50. proc.waitFor();
  51. } catch (Exception e) {
  52. result = false;
  53. e.printStackTrace();
  54. }
  55. return result;
  56. }
  57. /**
    * 生成水印,加密在这里实现
  58. * @param sourceFilePath 源文件路径
  59. * @param fileWaterMarkPath 水印生成文件路径
  60. * @throws Exception
  61. */
  62. public static void setWaterMarkForPDF(String sourceFilePath, String fileWaterMarkPath, String waterMarkName) throws Exception {
  63. // String waterPath = Class.class.getClass().getResource("/1.png").getPath();
  64. PdfReader reader = new PdfReader(sourceFilePath);
  65. PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(fileWaterMarkPath));
  66. int total = reader.getNumberOfPages() + 1;
  67. PdfContentByte under = null;
  68. //Image img = Image.getInstance(waterPath);
  69. //img.setAbsolutePosition(30, 100);//坐标
  70. // img.setRotation(-20);//旋转 弧度
  71. //img.setRotationDegrees(-35);//旋转 角度
  72. //img.scaleAbsolute(200,100);//自定义大小
  73. //img.scalePercent(100);//依照比例缩放
  74. BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
  75. BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
  76. if (StringUtils.isBlank(waterMarkName)) {
  77. waterMarkName = "Tech Mahindra";
  78. }
  79. int j = waterMarkName.length();
  80. char c = 0;
  81. int rise = 0;
  82. for (int i = 1; i < total; i++) // 每一页都加水印
  83. {
  84. rise = 500;
  85. under = stamp.getUnderContent(i);
  86. // 添加图片
  87. // under.addImage(img);
  88. under.beginText();
  89. under.setColorFill(BaseColor.BLACK);
  90. under.setFontAndSize(bf, 30);
  91. // 设置水印文字字体倾斜 开始
  92. if (j >= 15) {
  93. under.setTextMatrix(200, 120);
  94. for (int k = 0; k < j; k++) {
  95. under.setTextRise(rise);
  96. c = waterMarkName.charAt(k);
  97. under.showText(c + "");
  98. rise -= 20;
  99. }
  100. } else {
  101. under.setTextMatrix(180, 100);
  102. for (int k = 0; k < j; k++) {
  103. under.setTextRise(rise);
  104. c = waterMarkName.charAt(k);
  105. under.showText(c + "");
  106. rise -= 18;
  107. }
  108. }
  109. // 字体设置结束
  110. under.endText();
  111. }
  112. stamp.close();
  113. reader.close();
  114. }
  115. }
  1. HtmlToPdfInterceptor
  1. package pdf;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. /**
  7. * @Author: gaofeng_peng
  8. * @Date: 2018/7/5 13:17
  9. */
  10. public class HtmlToPdfInterceptor extends Thread {
  11. private InputStream is;
  12. public HtmlToPdfInterceptor(InputStream is){
  13. this.is = is;
  14. }
  15. public void run(){
  16. try{
  17. InputStreamReader isr = new InputStreamReader(is, "utf-8");
  18. BufferedReader br = new BufferedReader(isr);
  19. String line = null;
  20. while ((line = br.readLine()) != null) {
  21. System.out.println(line.toString()); //输出内容
  22. }
  23. }catch (IOException e){
  24. e.printStackTrace();
  25. }
  26. }
  27. }
  1. 水印部分的功能被我注释了,实际效果是可行的,只是本人设计的太丑,没脸见人所以注释。有兴趣的小伙伴可以自己改良。至此pdf打印下载的功能就完成了。需要注意的是html最好用原生的,使用less预处理可能会导致样式的丢失。

 

版权声明:本文为nowl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/nowl/p/9283978.html