| | |
| | | import com.itextpdf.html2pdf.ConverterProperties |
| | | import com.itextpdf.html2pdf.HtmlConverter |
| | | import com.itextpdf.layout.font.FontProvider |
| | | import com.itextpdf.text.pdf.BaseFont |
| | | import freemarker.template.Configuration |
| | | import org.xhtmlrenderer.pdf.ITextRenderer |
| | | import java.io.* |
| | | import javax.servlet.http.HttpServletResponse |
| | | |
| | |
| | | object GeneratePdfUtil { |
| | | private const val TEMPORARY_CONTRACT_HTML = "src/main/resources/templates/temporary.html" |
| | | |
| | | private const val SIMSUM_FILE = "src/main/resources/font/simsun.ttc" |
| | | private const val SIMSUN_FILE = "src/main/resources/font/simsunb.ttf" |
| | | @Throws(Exception::class) |
| | | fun generateContract(param: DynamicParam): String { |
| | | // 生成html合同 |
| | |
| | | // 根据html合同生成pdf合同 |
| | | generatePDF(param.outPath + param.outName) |
| | | // 删除临时html格式合同 |
| | | // removeFile(TEMPORARY_CONTRACT_HTML) |
| | | removeFile(TEMPORARY_CONTRACT_HTML) |
| | | return param.outPath + param.outName |
| | | } |
| | | |
| | |
| | | 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) |
| | |
| | | // os.close() |
| | | val p = ConverterProperties() |
| | | p.fontProvider = FontProvider().apply { |
| | | // this.addFont(SIMSUM_FILE) |
| | | this.addDirectory("src/main/resources/font/") |
| | | // this.addFont(SIMSUN_FILE) |
| | | this.addSystemFonts() |
| | | // this.addDirectory("src/main/resources/font/") |
| | | } |
| | | HtmlConverter.convertToPdf(htmFile, pdfFile, p) |
| | | } |