用freemarker生成静态pdf文件,并下载到本地

yaml文件

<!-- freemarker的坐标依赖 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>
        <!-- servlet-api的坐标依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>

        <!-- PDF工具类 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
        <!-- PDF中文支持 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.19</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.4.1</version>
        </dependency>
public class JSPStringToPDFExample  {
    public static void main(String[] args) {
        // 设置 Freemarker 配置
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        configuration.setClassForTemplateLoading(JSPStringToPDFExample.class, "/");

        // JSP 模板字符串
        String jspTemplateString = "<html><body><h1>Hello, ${name}!</h1></body></html>";

        try {
            // 创建一个字符串输出流
            StringWriter stringWriter = new StringWriter();

            // 将 JSP 模板字符串加载到模板对象
            Template template = new Template("jspTemplate", new StringReader(jspTemplateString), configuration);

            Map<String,Object> map = new HashMap<>();
            map.put("name","a");
            // 合并模板和数据到字符串输出流
            template.process(map, stringWriter);

            // 获取生成的 HTML 内容
            String htmlContent = stringWriter.toString();
            // 将 HTML 内容转换为 PDF     字符串,下载的路径名
            convertHtmlToPdf(htmlContent, "F:/aaa/output.pdf");

            System.out.println("PDF generation completed successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void convertHtmlToPdf(String htmlContent, String outputPdfFile) throws DocumentException, IOException {
        // 创建 PDF 文档对象
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPdfFile));
        document.open();

        // 将 HTML 内容写入 PDF 文档
        InputStream inputStream = new ByteArrayInputStream(htmlContent.getBytes());
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, inputStream);

        // 关闭文档
        document.close();
        writer.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于使用iText和FreeMarker生成HTML并将其转换为Word文档的内容。首先,您需要了解几个概念: 1. iText是一个用于生成PDF文档Java库,但它也可以用于生成其他类型的文档,例如Word文档。 2. FreeMarker是一个用于生成动态内容的Java模板引擎,它可以将数据模型与模板文件结合使用,生成静态文本文件。 接下来,您可以按照以下步骤来实现将HTML转换为Word文档: 1. 创建一个FreeMarker模板文件,其中包含您要生成的HTML内容,并将其保存为.ftl文件。 2. 在Java代码中,使用FreeMarker来读取.ftl文件并将数据模型填充到模板中,生成HTML字符串。 3. 使用iText将HTML字符串转换为Word文档。您可以使用iText的XMLWorkerHelper类来实现此功能。 以下是一个简单的示例代码,用于将HTML字符串转换为Word文档: ``` // Step 1: Load the FreeMarker template Configuration cfg = new Configuration(Configuration.VERSION_2_3_28); cfg.setDirectoryForTemplateLoading(new File("templates")); Template template = cfg.getTemplate("my_template.ftl"); // Step 2: Generate HTML string from the template and data model Map<String, Object> data = new HashMap<>(); data.put("name", "John Doe"); StringWriter out = new StringWriter(); template.process(data, out); String html = out.getBuffer().toString(); // Step 3: Convert HTML to Word document using iText ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(html.getBytes())); document.close(); // Save the Word document to file FileOutputStream fos = new FileOutputStream("output.docx"); fos.write(baos.toByteArray()); fos.close(); ``` 请注意,此示例代码仅用于说明概念,并且可能需要进行修改以适合您的特定需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值