wkhtmltopdf+itext实现html生成pdf文件的打印下载(适用于linux及windows)

简介: 目中遇到个根据html转Java的功能,在java中我们itext可以快速的实现pdf打印下载的功能,在itext中我们一般有以下三中方式实现配置pdf模板,通过Adobe Acrobat 来设置域最后通过代码将数据填充进去通过FreeMarker或thymeleaf配置html模板填充数据Jsoup+XMLWorkerHelper对于上述的三种方式,我简述下我的体验:第一种方式对于入门简单,如果我们需求中的pdf文件是表格或者报表的样式还是很好实现的,但如果遇到要求和html样式一致的话就基本歇菜了。

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

  1. 配置pdf模板,通过Adobe Acrobat 来设置域最后通过代码将数据填充进去
  2. 通过FreeMarker或thymeleaf配置html模板填充数据
  3. 对于上述的三种方式,我简述下我的体验:第一种方式对于入门简单,如果我们需求中的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插件的具体实现

     

    windows下安装测试

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

    http://image.tianluzhi.com:81/windows.mp4  windows演示地址

    Linux下安装测试

    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字体是没效果的,这里给大家提供一个下载  http://image.tianluzhi.com:81/SIMSUN.TTC ,linux下一般就这些问题。这里就不做演示了,我们开始真正的coding吧。

     

    配置pdf生成的路径解读

    
    # linux path
    #wkhtmltopdfPath 指向wkhtmltopdfPath安装的bin目录下wkhtmltopdf
    wkhtmltopdfPath=/home/wkhtmltox/bin/wkhtmltopdf
    #pdf
    pdfLocationPathForLinux=/home/prd-gehcpp-01/otoResources/pdf/
    waterPdfLocationPathForLinux=/home/prd-gehcpp-01/otoResources/waterPdf/
    
    # windows path
    #photoLocationPath=D://otoResources//photo//
    #pdf文件路径
    pdfLocationPath= D://otoResources//pdf//
    #添加水印后文件路径
    waterPdfLocationPath= D://otoResources//waterPdf//
    

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

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

    代码实现

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

    controller

    /**
    *
    * @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 

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

    impl

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

    /**
    * 响应下载
    * @param resp
    * @param downloadName 下载的pdf文件名
    * @param fileName 系统中真正的文件名
    */
        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();
    }
    }

    }
    }
     

     

    HtmlToPdfUtil类

    package pdf;
    
    
    import com.itextpdf.text.BaseColor;
    import com.itextpdf.text.pdf.BaseFont;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfStamper;
    import org.apache.commons.lang3.StringUtils;
    import utils.PropertiesUtil;
    
    import java.io.FileOutputStream;
    
    /**
     * @Author: gaofeng_peng
     * @Date: 2018/7/5 13:13
     */
    public class HtmlToPdfUtil {
        //wkhtmltopdf在系统中的路径
        // private static final String toPdfTool = "D:\\wkhtmltox\\bin\\wkhtmltopdf.exe";
    
    
        /**
         * html转pdf
         *
         * @param srcPath    html路径
         * @param pdfLocationPath pdf保存路径
         * @return 转换成功返回true
         */
        public static boolean convert(String srcPath, String pdfLocationPath, String systemOs) throws Exception {
            String toPdfTool="";
            if (systemOs.toLowerCase().startsWith("win")) {
                 toPdfTool = HtmlToPdfUtil.class.getClassLoader().getResource("wkhtmltox/bin/wkhtmltopdf.exe").getPath();
            }else {
                toPdfTool= PropertiesUtil.getProperty("wkhtmltopdfPath");
            }
            StringBuilder cmd = new StringBuilder();
            cmd.append(toPdfTool);
            cmd.append(" ");
            cmd.append("  --background");
            cmd.append(" --debug-javascript");
            cmd.append("  --header-line");//页眉下面的线
            cmd.append(" --header-spacing 10 ");//    (设置页眉和内容的距离,默认0)
    
            cmd.append(srcPath);
            cmd.append(" ");
            cmd.append(pdfLocationPath);
    
            boolean result = true;
            try {
                Process proc = Runtime.getRuntime().exec(cmd.toString());
                HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
                HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
                error.start();
                output.start();
                proc.waitFor();
            } catch (Exception e) {
                result = false;
                e.printStackTrace();
            }
            return result;
        }
    
        /**
    * 生成水印,加密在这里实现 * @param sourceFilePath 源文件路径 * @param fileWaterMarkPath 水印生成文件路径 * @throws Exception */ public static void setWaterMarkForPDF(String sourceFilePath, String fileWaterMarkPath, String waterMarkName) throws Exception { // String waterPath = Class.class.getClass().getResource("/1.png").getPath(); PdfReader reader = new PdfReader(sourceFilePath); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(fileWaterMarkPath)); int total = reader.getNumberOfPages() + 1; PdfContentByte under = null; //Image img = Image.getInstance(waterPath); //img.setAbsolutePosition(30, 100);//坐标 // img.setRotation(-20);//旋转 弧度 //img.setRotationDegrees(-35);//旋转 角度 //img.scaleAbsolute(200,100);//自定义大小 //img.scalePercent(100);//依照比例缩放 BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); if (StringUtils.isBlank(waterMarkName)) { waterMarkName = "Tech Mahindra"; } int j = waterMarkName.length(); char c = 0; int rise = 0; for (int i = 1; i < total; i++) // 每一页都加水印 { rise = 500; under = stamp.getUnderContent(i); // 添加图片 // under.addImage(img); under.beginText(); under.setColorFill(BaseColor.BLACK); under.setFontAndSize(bf, 30); // 设置水印文字字体倾斜 开始 if (j >= 15) { under.setTextMatrix(200, 120); for (int k = 0; k < j; k++) { under.setTextRise(rise); c = waterMarkName.charAt(k); under.showText(c + ""); rise -= 20; } } else { under.setTextMatrix(180, 100); for (int k = 0; k < j; k++) { under.setTextRise(rise); c = waterMarkName.charAt(k); under.showText(c + ""); rise -= 18; } } // 字体设置结束 under.endText(); } stamp.close(); reader.close(); } }
    HtmlToPdfInterceptor类
    package pdf;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    /**
     * @Author: gaofeng_peng
     * @Date: 2018/7/5 13:17
     */
    public class HtmlToPdfInterceptor extends Thread {
        private InputStream is;
    
        public HtmlToPdfInterceptor(InputStream is){
            this.is = is;
        }
    
        public void run(){
            try{
                InputStreamReader isr = new InputStreamReader(is, "utf-8");
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ((line = br.readLine()) != null) {
                    System.out.println(line.toString()); //输出内容
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    水印部分的功能被我注释了,实际效果是可行的,只是本人设计的太丑,没脸见人所以注释。后期改良再说,有兴趣的小伙伴可以自己改良。至此pdf打印下载的功能就完成了。需要注意的是html最好用原生的,使用less预处理可能会导致样式的丢失。

     

相关文章
|
2月前
|
Ubuntu Linux
Ubuntu Linux 20.04 LTS “Focal Fossa”测试版开放下载
u要知道有关新系统的更多信息,大家可以前往Ubuntu Wiki页面,其中包含Ubuntu 20.04的发行说明。
56 0
|
2月前
|
Ubuntu 安全 Linux
Linux系统-Ubuntu的下载和安装 软件大全
在庄子看来,生老病死就像四时交替,都只是自然现象而已,人不必有什么忧虑,也不必有什么痛苦。人源于自然,再回归自然,说到底,不过是正常的生死轮转,他依然在天地之间,只要怀着这样的想法,人又有什么好痛苦的呢。
|
2月前
|
Ubuntu Linux Windows
Ubuntu Linux 24.04 LTS 发行版现已开放下载
Ubuntu 24.04 采用了 Linux 6.8 内核,可利用 Netplan 在桌面上配置网络连接,还配备了现代化的桌面操作系统安装程序,还带来了新版 Ubuntu 字体以及各种性能优化以及大量新功能。
|
4月前
|
存储 前端开发 JavaScript
仿真银行app下载安装, 银行卡虚拟余额制作app,用html+css+js实现逼真娱乐工具
这是一个简单的银行账户模拟器项目,用于学习前端开发基础。用户可进行存款、取款操作,所有数据存储于浏览器内存中
|
7月前
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
1383 77
|
8月前
|
前端开发
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
203 1
【2025优雅草开源计划进行中01】-针对web前端开发初学者使用-优雅草科技官网-纯静态页面html+css+JavaScript可直接下载使用-开源-首页为优雅草吴银满工程师原创-优雅草卓伊凡发布
|
7月前
|
人工智能 Linux iOS开发
Linux下搭建本地deepseek(附文档下载)
本文介绍了在Linux上搭建本地DeepSeek的步骤。主要涉及安装ollama、下载deepseek模型(1.5b参数版本)和配置ChatboxAI客户端。通过命令行安装ollama后,使用`ollama run deepseek-r1:1.5b`下载模型,并配置环境变量以确保服务正常运行。最后,通过ChatboxAI与模型进行交互。整个过程适合初学者,服务器配置为4核CPU和8GB内存,无GPU,响应速度稍有延迟但可接受。相关资源链接已提供。
890 2
|
8月前
|
Linux API
Linux下载工具wget与curl
`wget` 是一个用于从网络下载文件的命令行工具,支持HTTP、HTTPS和FTP协议。它能自动处理下载中断,并支持递归下载网站内容。基本用法:`wget URL`,可指定文件名(`-O`)、保存目录(`-P`),还支持断点续传(`-c`)、限速(`--limit-rate`)和递归下载(`-r`)。相比之下,`curl` 更侧重于发送各种HTTP请求(如GET、POST),并支持文件上传、自定义请求头和cookie等功能。
199 10
|
11月前
|
人工智能 Java 关系型数据库
Red Hat Enterprise Linux 9.5 发布下载,新增功能亮点概述
Red Hat Enterprise Linux 9.5 发布下载,新增功能亮点概述
578 4
Red Hat Enterprise Linux 9.5 发布下载,新增功能亮点概述

热门文章

最新文章