记录每一个努力的日子!
上一篇: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使用