|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package cn.iocoder.yudao.module.system.service.jobinfo; |
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil; |
|
|
|
|
import cn.hutool.core.io.FileUtil; |
|
|
|
|
import cn.hutool.core.lang.Dict; |
|
|
|
|
import cn.hutool.extra.template.Template; |
|
|
|
@ -7,6 +8,9 @@ import cn.hutool.extra.template.TemplateConfig;
|
|
|
|
|
import cn.hutool.extra.template.TemplateEngine; |
|
|
|
|
import cn.hutool.extra.template.TemplateUtil; |
|
|
|
|
import cn.iocoder.yudao.framework.common.util.pdf.WkHtmlToPdfUtil; |
|
|
|
|
import cn.iocoder.yudao.module.infra.api.file.FileApi; |
|
|
|
|
import cn.iocoder.yudao.module.infra.api.file.dto.InfraFileInfoDTO; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.validation.annotation.Validated; |
|
|
|
@ -33,6 +37,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
|
|
*/ |
|
|
|
|
@Service |
|
|
|
|
@Validated |
|
|
|
|
@Slf4j |
|
|
|
|
public class JobInfoServiceImpl implements JobInfoService { |
|
|
|
|
|
|
|
|
|
@Value("${yudao.htmlToPdf.tools}") |
|
|
|
@ -41,6 +46,10 @@ public class JobInfoServiceImpl implements JobInfoService {
|
|
|
|
|
@Value("${yudao.htmlToPdf.jobTemp}") |
|
|
|
|
private String htmlToPdfJobTemp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private FileApi fileApi; |
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
|
private JobInfoMapper jobInfoMapper; |
|
|
|
|
|
|
|
|
@ -94,18 +103,34 @@ public class JobInfoServiceImpl implements JobInfoService {
|
|
|
|
|
TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig(htmlToPdfJobTemp, TemplateConfig.ResourceMode.FILE)); |
|
|
|
|
|
|
|
|
|
Template template = engine.getTemplate("jobTemp.html"); |
|
|
|
|
Dict content1 = Dict.create().set("content", jobInfo.getContent()); |
|
|
|
|
content1.set("jobName",jobInfo.getJobName()); |
|
|
|
|
content1.set("jobDate", DateUtil.format(jobInfo.getJobDate().atStartOfDay(),"YYYY.MM.DD")); |
|
|
|
|
|
|
|
|
|
String content = template.render(Dict.create().set("content", jobInfo.getContent())); |
|
|
|
|
String content = template.render(content1); |
|
|
|
|
|
|
|
|
|
// 写入文件并返回File对象
|
|
|
|
|
File file = FileUtil.writeUtf8String(content, htmlToPdfJobTemp + "test.html"); |
|
|
|
|
File file = FileUtil.writeUtf8String(content, htmlToPdfJobTemp + jobInfo.getTitle()+".html"); |
|
|
|
|
// 调用wkhtmltopdf工具转换为pdf
|
|
|
|
|
// 这里的htmlToPdfTools是wkhtmltopdf的工具路径,htmlToPdfJobTemp是html文件路径,test.pdf是输出pdf文件路径
|
|
|
|
|
File file1 = new File(htmlToPdfJobTemp + "test.pdf"); |
|
|
|
|
File file1 = new File(htmlToPdfJobTemp + jobInfo.getTitle()+".pdf"); |
|
|
|
|
|
|
|
|
|
String params = WkHtmlToPdfUtil.DPI + WkHtmlToPdfUtil.PAGE_SIZE_A4 + WkHtmlToPdfUtil.DISABLE_SMART_SHRINKING+WkHtmlToPdfUtil.LOAD_ERROR_HANDLING_IGNORE; |
|
|
|
|
|
|
|
|
|
WkHtmlToPdfUtil.convert(htmlToPdfJobTemp + "test.html", file1, "",params); |
|
|
|
|
WkHtmlToPdfUtil.convert(htmlToPdfJobTemp + jobInfo.getTitle()+".html", file1, "",params); |
|
|
|
|
|
|
|
|
|
try{ |
|
|
|
|
InfraFileInfoDTO file2 = fileApi.createFile(FileUtil.readBytes(file1)); |
|
|
|
|
jobInfo.setPdfUrl(file2.getUrl()); |
|
|
|
|
jobInfoMapper.updateById(jobInfo); |
|
|
|
|
//等待3秒后异步删除
|
|
|
|
|
FileUtil.del(file); |
|
|
|
|
FileUtil.del(file1); |
|
|
|
|
|
|
|
|
|
}catch (Exception e) { |
|
|
|
|
|
|
|
|
|
log.info(e.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return content; |
|
|
|
|
} |
|
|
|
|