这里写自定义目录标题
@Override
@Transactional(readOnly = true)
public boolean processAndCreateHtml(String freeTempName, Context context, String outFilePath) {
FileWriter fileWriter = null;
try {
File file = new File(outFilePath);
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
fileWriter = new FileWriter(outFilePath);
templateEngine.process(freeTempName, context,fileWriter);
} catch (Exception e) {
log.error(e.getMessage(),e);
return false;
} finally {
try {
if (fileWriter != null){
fileWriter.close();
}
} catch (IOException e) {
log.error(e.getMessage(),e);
return false;
}
}
return true;
}
@Override
@Transactional(readOnly = true)
public String buildHtmlOutFilePath(){
Date date = new Date();
return PathEnum.TEMPLATE_HTML.getPath()
+ DateUtil.year(date)
+ "/"
+ DateUtil.month(date)
+ "/"
+ DateUtil.dayOfMonth(date)
+ "/"
+ UUID.randomUUID().toString()
+ ".html";
}
@Override
@Transactional(readOnly = true)
public String buildPdfOutFilePath(){
Date date = new Date();
return PathEnum.TEMPLATE_PDF.getPath()
+ DateUtil.year(date)
+ "/"
+ DateUtil.month(date)
+ "/"
+ DateUtil.dayOfMonth(date)
+ "/"
+ UUID.randomUUID().toString()
+ ".pdf";
}
@Override
@Transactional(readOnly = true)
public String htmlToPdf(String freeTempName, Context context) {
String htmlOutFilePath = buildHtmlOutFilePath();
boolean createHtml = processAndCreateHtml(freeTempName, context, htmlOutFilePath);
if (!createHtml){
throw new BaseException(PayrollExceptionCode.DATA_NOT_FOUND);
}
String pdfOutFilePath = buildPdfOutFilePath();
try {
Html2pdf.html2pdf(htmlOutFilePath,pdfOutFilePath);
} catch (Exception e) {
e.printStackTrace();
}
return pdfOutFilePath;
}
}
@Override
@Transactional(readOnly = true)
public boolean processAndCreateHtml(String freeTempName, Context context, String outFilePath) {
FileWriter fileWriter = null;
try {
File file = new File(outFilePath);
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
fileWriter = new FileWriter(outFilePath);
templateEngine.process(freeTempName, context,fileWriter);
} catch (Exception e) {
log.error(e.getMessage(),e);
return false;
} finally {
try {
if (fileWriter != null){
fileWriter.close();
}
} catch (IOException e) {
log.error(e.getMessage(),e);
return false;
}
}
return true;
}
package io.ersoft.payroll.pdf.uitls;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont;
public class Html2pdf {
public static void html2pdf(String htmlFile, String pdfFile) throws Exception {
File file = new File(pdfFile);
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
String url = new File(htmlFile).toURI().toURL().toString();
System.out.println(url);
OutputStream os = new FileOutputStream(pdfFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("/Users/user/Desktop/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.layout();
renderer.createPDF(os);
os.close();
}
public static String getCurrentOperatingSystem(){
String os = System.getProperty("os.name").toLowerCase();
System.out.println("---------当前操作系统是-----------" + os);
return os;
}
public static void main(String[] args) {
String htmlFile = "D:\\WorkSpace\\IdeaProjects\\pdf\\src\\main\\resources\\templates\\u.html";
String pdfFile = "c:/test.pdf";
try {
Html2pdf.html2pdf(htmlFile, pdfFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Context context = new Context();
context.setVariable("payslipInfos", payslipEmployeeInfoBos);
String filePath = thymeleafService.htmlToPdf("newPayslipPdfTemplate", context);