4、制作一个html转pdf的spring boot starter

记录每一个努力的日子!
上一篇:maven多module打包
参考项目:github 1.2.0分支 tool模块

1、依赖软件

wkhtmltopdf

2、关键代码

使用ProcessBuilder来执行可执行脚本

public Html2pdf(String command){
   this.command = command;
}

public final void conver(String htmlPath, String pdfPath) throws Throwable {
    ProcessBuilder pb = new ProcessBuilder(Stream.of(command, htmlPath, pdfPath).collect(Collectors.toList()));
    pb.redirectErrorStream(true);
    Process p = null;
    BufferedReader br = null;
    try {
        p = pb.start();
        br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = br.readLine()) != null) {}
        p.waitFor();
    } finally {
        if (br != null) {
            br.close();
        }
        if (p != null) {
            p.destroy();
        }
    }
}

3、步骤

3.1

新建module,编写html2pdf的代码

3.2

新建一个starter的module,引入上一个module,把上一个module的主执行类申明为一个Bean

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "html2pdf", name = "enable", havingValue = "true")
public Html2pdf html2pdf(Html2pdfProperties html2pdfProperties) {
    return new Html2pdf(html2pdfProperties.getCommand());
}

在strter的resources下新建META-INF/spring.factories,添加starter中定义的Configuration

org.springframework.boot.autoconfigure.EnableAutoConfiguration=net.dog.html2pdfspringbootstarter.Html2pdfAutoConfiguration

3.3

在项目中直接引入starter,即可注入bean使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值