java实现html转pdf 带样式

java实现html带样式转pdf

做项目偶遇html版征信报告转pdf存储
查阅各博主方案有的是不支持样式,有的是过于繁琐,今天分享一个比较简单有效的方法 windows liunx 亲测有效
首先需要下载插件 分为windows版本和 liunx版本
在这里插入图片描述
百度网盘:https://pan.baidu.com/s/1yFvNULcmbmAYII7Vn0ZBpw
提取码:3660
以windows为例
插件存放目录 html存放目录
打开cmd执行命令
C:\Users\Administrator/tmp/src/wkhtmltox/bin/wkhtmltopdf --enable-local-file-access C:\Users\Administrator\tmp\src\html\html\2162537843264761856618949.html C:\Users\Administrator\tmp\src\html\html\2162537843264761856618949.pdf
在这里插入图片描述
如图,界面打印Done时执行完成,查看生成pdf
在这里插入图片描述
打开pdf 支持任意页数,包含样式
在这里插入图片描述
下面讲述下java实现过程

	   public Boolean htmlToPdf(String htmlFile,String pdfFile){
    try {
        String cmd = Constants.LOCAL_PATH +  PLUGINSPATH + htmlFile +" "+ pdfFile;
        log.info("=================>脚本:{}",cmd);
        Process exeEcho = Runtime.getRuntime().exec(cmd);
        BufferedReader reader = new BufferedReader(new InputStreamReader(exeEcho.getErrorStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            if ("Done".equals(line)){
                break;
            }
        }
        log.info("文件上传或下载成功");
        return true;
    }catch (Exception e){
        log.info("文件解析异常");
        throw new BizException(ErrCode.E100132);
    }
}

代码中
if ("Done".equals(line)){
            break;
        }

监听pdf生成是否完成,如果在不监听的情况下,存在异步,可以读取文件查看,可能会报空指针异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想要将HTML换为PDF时保留HTML中的样式,可以使用CSS来定义样式,然后将CSS文件链接到HTML文件中。iText库会自动将CSS样式应用于PDF生成文件。 以下是一个示例代码: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; public class HtmlToPdfWithStyle { public static void main(String[] args) throws Exception { String inputFile = "input.html"; String cssFile = "style.css"; String outputFile = "output.pdf"; // Create a Document object Document document = new Document(); // Create a PdfWriter object PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile)); // Open the Document document.open(); // Read the HTML file into an InputStream InputStream htmlStream = new FileInputStream(inputFile); // Read the CSS file into an InputStream InputStream cssStream = new FileInputStream(cssFile); // Use the XMLWorkerHelper to parse the HTML and generate PDF XMLWorkerHelper.getInstance().parseXHtml(writer, document, htmlStream, cssStream); // Close the Document document.close(); } } ``` 在上面的示例代码中,您需要将“input.html”替换为您要换的HTML文件的路径,并将“style.css”替换为您的CSS文件的路径。在HTML文件中,您需要使用<link>标签将CSS文件链接到HTML文件中: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HTML to PDF</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>Hello, World!</h1> <p>This is a paragraph.</p> </body> </html> ``` 在CSS文件中,您可以定义样式: ```css h1 { color: red; } p { font-size: 14pt; } ``` 这将应用于换后的PDF文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值