1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
package com.fine.demo.util;
import com.google.common.base.Throwables; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.BaseFont; import lombok.extern.slf4j.Slf4j; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer;
import javax.annotation.PostConstruct; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;
@Slf4j public class FileTypeConvertUtil {
public static String pdfPath; public static String fontPath;
public static void html2pdf(String htmlFilePath, String pdfFilePath) { try { log.info("FileTypeConvertUtil[]html2pdf[]start! htmlFilePath:{}, pdfFilePath:{}", htmlFilePath, pdfFilePath); String url = new File(htmlFilePath).toURI().toURL().toString();
OutputStream os = new FileOutputStream(pdfFilePath); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(url);
String fontPath = ""; ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.layout(); renderer.createPDF(os); os.close(); log.info("FileTypeConvertUtil[]html2pdf[]success!"); } catch (Exception e) { log.error("FileTypeConvertUtil[]html2pdf[]error! cause:{}", Throwables.getStackTraceAsString(e)); } }
@PostConstruct public void init() { try { log.info("FileTypeConvertUtil[]init[]make[]contractFile[]start!"); pathInit();
log.info("FileTypeConvertUtil[]init[]createPdfFile[]pdfPath:{}", pdfPath); createPdfFile();
log.info("FileTypeConvertUtil[]init[]createFontFile[]fontPath:{}", fontPath); createFontFile();
log.info("FileTypeConvertUtil[]init[]success!"); } catch (IOException e) { log.error("FileTypeConvertUtil[]init[]error! cause:{}", Throwables.getStackTraceAsString(e));
} }
private void createFontFile() throws IOException { InputStream fontIs = this.getClass().getResourceAsStream("/pdf/simsun.ttf"); OutputStream outputStream = new FileOutputStream(new File(fontPath));
int byteCount = 0; byte[] bytes = new byte[1024]; while ((byteCount = fontIs.read(bytes)) != -1) { outputStream.write(bytes, 0, byteCount); } fontIs.close(); outputStream.close(); }
private void createPdfFile() { File contractFile = new File(pdfPath); if (!contractFile.exists()) { contractFile.mkdir(); } }
private void pathInit() { String jarPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); String itemName = "***.jar"; if (jarPath.contains(itemName)) { jarPath = jarPath.substring(6, jarPath.indexOf(itemName)); } pdfPath = jarPath + "pdf"; fontPath = pdfPath + "/simsun.ttf"; } }
|