springboot + itext + freemarker 生成pdf

目录

freemarker官方地址:

项目

 maven

freemarker模版

代码


freemarker官方地址:

Putting all together - Apache FreeMarker Manualhttps://freemarker.apache.org/docs/pgui_quickstart_all.html

项目

 maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.freemarker</groupId>
    <artifactId>freemarkerdemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.11</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.13.2</version>
        </dependency>

    </dependencies>





</project>

freemarker模版

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>

    <title>test</title>
    <style type="text/css">
         table {
             font-family: "simsun";
             width: 100%;
             border-collapse: collapse;
         }

         td, th {
             font-size: 1em;
             border: 1px solid #5B4A42;
             padding: 3px 7px 2px 7px;
         }

        th {
             font-size: 1.1em;
             text-align: center;
             padding-top: 5px;
             padding-bottom: 4px;
             background-color: #24A9E1;
           color: #ffffff;
        }
        </style>
    </head>
<body>
<div>
    <table id="customers">
        <tr>
            <th>MessageCode</th>
            <th>MessageStatus</th>
            <th>Cause</th>
        </tr>
        <tr>
            <td>${messageCode}</td>
            <td>${messageStatus}</td>
            <td>${cause}</td>
        </tr>
    </table>
</div>
</body>
</html>

代码

 public void freeMarkerDemo() throws Exception {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("messageCode","消息");
        hashMap.put("messageStatus","状态");
        hashMap.put("cause","cause");
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate("freemarker.ftl");
        String freeStr = FreeMarkerTemplateUtils.processTemplateIntoString(template, hashMap);
        
        Document documentText = new Document();
        String s = "D:\\t.pdf";
        File file = new File(s);
        if(!file.exists()){
            file.createNewFile();
        }
        PdfWriter pdfWriter = PdfWriter.getInstance(documentText,new FileOutputStream(file));
        documentText.open();
//设置中文
        XMLWorkerFontProvider fontProvider=new XMLWorkerFontProvider();
        fontProvider.register(new ClassPathResource("templates/simsun.ttc").getPath());
        XMLWorkerHelper.getInstance()
                .parseXHtml(pdfWriter,documentText,new ByteArrayInputStream(freeStr.getBytes(StandardCharsets.UTF_8)),null,StandardCharsets.UTF_8, fontProvider);
        documentText.close();


    }

                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于使用iTextFreeMarker生成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、付费专栏及课程。

余额充值