From 23bd719cebe5feeff4e48fde925b0b39755eea93 Mon Sep 17 00:00:00 2001 From: feiyu02 <risaku@163.com> Date: 星期二, 15 十一月 2022 10:59:50 +0800 Subject: [PATCH] 2022.11.15 --- src/main/kotlin/cn/flightfeather/supervision/common/pdf/GeneratePdfUtil.kt | 112 +++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/main/kotlin/cn/flightfeather/supervision/common/pdf/GeneratePdfUtil.kt b/src/main/kotlin/cn/flightfeather/supervision/common/pdf/GeneratePdfUtil.kt index 4f3e65e..4970b29 100644 --- a/src/main/kotlin/cn/flightfeather/supervision/common/pdf/GeneratePdfUtil.kt +++ b/src/main/kotlin/cn/flightfeather/supervision/common/pdf/GeneratePdfUtil.kt @@ -2,64 +2,86 @@ import com.itextpdf.html2pdf.ConverterProperties import com.itextpdf.html2pdf.HtmlConverter +import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider +import com.itextpdf.io.font.PdfEncodings +import com.itextpdf.kernel.font.PdfFontFactory import com.itextpdf.layout.font.FontProvider -import com.itextpdf.text.pdf.BaseFont import freemarker.template.Configuration -import org.xhtmlrenderer.pdf.ITextRenderer +import org.apache.pdfbox.pdmodel.PDDocument +import org.apache.pdfbox.rendering.PDFRenderer import java.io.* +import javax.imageio.ImageIO import javax.servlet.http.HttpServletResponse + /** - * @Description: 鐢熸垚PDF鍚堝悓宸ュ叿绫� + * @Description: 鐢熸垚PDF宸ュ叿绫� */ object GeneratePdfUtil { - private const val TEMPORARY_CONTRACT_HTML = "src/main/resources/templates/temporary.html" + private val TEMPORARY_CONTRACT_HTML = (Thread.currentThread().contextClassLoader?.getResource("/")?.path ?: "src/main/resources/") + "templates/temporary.html" - private const val SIMSUM_FILE = "src/main/resources/font/simsun.ttc" + private val SIMSUN_FILE = (Thread.currentThread().contextClassLoader?.getResource("/")?.path ?: "src/main/resources/") + "font/" + + private var cfg: Configuration? = null + @Throws(Exception::class) - fun generateContract(param: DynamicParam): String { - // 鐢熸垚html鍚堝悓 + fun generateContract(param: DynamicParam): List<String> { + // 鐢熸垚html generateHTML(param.templatePath, param.templateName, param.param) - // 鏍规嵁html鍚堝悓鐢熸垚pdf鍚堝悓 + // 鏍规嵁html鐢熸垚pdf generatePDF(param.outPath + param.outName) - // 鍒犻櫎涓存椂html鏍煎紡鍚堝悓 -// removeFile(TEMPORARY_CONTRACT_HTML) - return param.outPath + param.outName + // 鏍规嵁pdf鐢熸垚鍥剧墖 + val picPaths = generatePic(param.outPath + param.outName) + // 鍒犻櫎涓存椂html + removeFile(TEMPORARY_CONTRACT_HTML) + return picPaths } /** - * @Description 鐢熸垚html鏍煎紡鍚堝悓 + * @Description 鐢熸垚html鏍煎紡 */ @Throws(Exception::class) private fun generateHTML(templatePath: String, templateName: String, paramMap: Map<String, Any>) { - val cfg = Configuration() - cfg.defaultEncoding = "UTF-8" - /** - * 1.setClassForTemplateLoading(this.getClass(), "/HttpWeb"); - * 鍩轰簬绫昏矾寰勶紝HttpWeb鍖呬笅鐨刦ramemaker.ftl鏂囦欢 - * 2.setDirectoryForTemplateLoading(new File("/template")); - * 鍩轰簬鏂囦欢绯荤粺,template鐩綍涓嬬殑鏂囦欢 - * 3.setServletContextForTemplateLoading(request.getSession().getServletContext(), "/template"); - * 鍩轰簬Servlet Context锛屾寚鐨勬槸鍩轰簬WebRoot涓嬬殑template涓嬬殑framemaker.ftl鏂囦欢 - */ - cfg.setDirectoryForTemplateLoading(File(templatePath)) + if (cfg == null) { + cfg = Configuration(Configuration.VERSION_2_3_31) + cfg?.defaultEncoding = "UTF-8" + /** + * 1.setClassForTemplateLoading(this.getClass(), "/HttpWeb"); + * 鍩轰簬绫昏矾寰勶紝HttpWeb鍖呬笅鐨刦ramemaker.ftl鏂囦欢 + * 2.setDirectoryForTemplateLoading(new File("/template")); + * 鍩轰簬鏂囦欢绯荤粺,template鐩綍涓嬬殑鏂囦欢 + * 3.setServletContextForTemplateLoading(request.getSession().getServletContext(), "/template"); + * 鍩轰簬Servlet Context锛屾寚鐨勬槸鍩轰簬WebRoot涓嬬殑template涓嬬殑framemaker.ftl鏂囦欢 + */ + val file = File(templatePath) + if (!file.exists()) { + file.mkdirs() + } + cfg?.setDirectoryForTemplateLoading(file) + } // templateName.ftl涓鸿瑁呰浇鐨勬ā鏉� - val template = cfg.getTemplate(templateName) + val template = cfg?.getTemplate(templateName) val outHtmFile = File(TEMPORARY_CONTRACT_HTML) - val out: Writer = BufferedWriter(OutputStreamWriter(FileOutputStream(outHtmFile))) + val out: Writer = BufferedWriter(OutputStreamWriter(FileOutputStream(outHtmFile), "utf-8")) // 灏嗗弬鏁拌緭鍑哄埌妯$増锛屽苟鎿嶄綔鍒癏TML涓� - template.process(paramMap, out) +// val env = template?.createProcessingEnvironment(paramMap, out) +// env?.outputEncoding = "utf-8" +// env?.process() + template?.process(paramMap, out) out.close() } /** - * @Description 鏍规嵁html鐢熸垚pdf鏍煎紡鍚堝悓 + * @Description 鏍规嵁html鐢熸垚pdf */ @Throws(Exception::class) private fun generatePDF(pdfUrl: String) { val htmFile = File(TEMPORARY_CONTRACT_HTML) val pdfFile = File(pdfUrl) + if (!pdfFile.parentFile.exists()) { + pdfFile.parentFile.mkdirs() + } println(pdfUrl) // val url = htmFile.toURI() // val os: OutputStream = FileOutputStream(pdfFile) @@ -72,14 +94,46 @@ // renderer.createPDF(os) // os.close() val p = ConverterProperties() + println("------------------------------>>>>>>>${SIMSUN_FILE}") +// p.charset = "UTF-8" p.fontProvider = FontProvider().apply { -// this.addFont(SIMSUM_FILE) - this.addDirectory("src/main/resources/font/") +// addStandardPdfFonts() +// addDirectory(SIMSUN_FILE) + addSystemFonts() } HtmlConverter.convertToPdf(htmFile, pdfFile, p) } /** + * @Description 鏍规嵁pdf鐢熸垚鍥剧墖 + */ + @Throws(Exception::class) + private fun generatePic(pdfUrl: String): List<String> { + val picPaths = mutableListOf<String>() + + val pdfFile = File(pdfUrl) + if (!pdfFile.exists()) { + throw FileNotFoundException("$pdfUrl 鏂囦欢涓嶅瓨鍦�") + } + val doc = PDDocument.load(pdfFile) + val renderer = PDFRenderer(doc) + val pageCount = doc.numberOfPages + for (i in 0 until pageCount) { + val outputPath = pdfUrl.replace(".pdf", "(${i}).jpg"); + val outputFile = File(outputPath) + picPaths.add(outputPath) + // dpi锛屽浘鐗囧儚绱犵偣锛宒pi瓒婇珮鍥剧墖浣撶Н瓒婂ぇ锛�216寰堟竻鏅帮紝105浣撶Н绋冲畾 + val image = renderer.renderImageWithDPI(i, 216f) + // 鏍煎紡涓篔PG + ImageIO.write(image, "jpg", outputFile) + } + + doc.close() + + return picPaths + } + + /** * @Description 绉婚櫎鏂囦欢 */ private fun removeFile(fileUrl: String) { -- Gitblit v1.9.3