| | |
| | | |
| | | 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 { |
| | | fun generateContract(param: DynamicParam): List<String> { |
| | | // 生成html合同 |
| | | generateHTML(param.templatePath, param.templateName, param.param) |
| | | // 根据html合同生成pdf合同 |
| | | generatePDF(param.outPath + param.outName) |
| | | // 根据pdf生成图片 |
| | | val picPaths = generatePic(param.outPath + param.outName) |
| | | // 删除临时html格式合同 |
| | | // removeFile(TEMPORARY_CONTRACT_HTML) |
| | | return param.outPath + param.outName |
| | | 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包下的framemaker.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包下的framemaker.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")) |
| | | // 将参数输出到模版,并操作到HTML上 |
| | | 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) |
| | |
| | | // 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,图片像素点,dpi越高图片体积越大,216很清晰,105体积稳定 |
| | | val image = renderer.renderImageWithDPI(i, 216f) |
| | | // 格式为JPG |
| | | ImageIO.write(image, "jpg", outputFile) |
| | | } |
| | | |
| | | doc.close() |
| | | |
| | | return picPaths |
| | | } |
| | | |
| | | /** |
| | | * @Description 移除文件 |
| | | */ |
| | | private fun removeFile(fileUrl: String) { |