在项目中,遇到导出pdf的需求;首先创建一个word文档模板,字符串数据使用{{name}}占位符,图片使用{{@image}}占位符

一、word文档模板:

 二、导入依赖

<!-- Apache POI -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-full</artifactId>
    <version>5.2.2</version>
</dependency>

 三、代码示例

//组装数据
Map<String, Object> params = new HashMap<>();
params.put("crossProvince", "否");

//二维码内容
String url = "https://www.zhihu.com/";
//生成二维码
PictureRenderData image = QrCodeUtils.convertToPictureRenderData(url, "png");
params.put("image", image);

//word文档模板路径
String absoluteStaticPath = "D:xxx\\xxxx.docx";
//导出文档存放路径
String realPath = "D:/pdf";
File dir = new File(realPath);
if (!dir.exists() && !dir.isDirectory()) {
    dir.mkdirs();
}

// 数据填充
XWPFTemplate template = XWPFTemplate.compile(absoluteStaticPath).render(params);
String fileName = realPath + "/" + UUIDUtils.getStringUUID() + ".docx";
template.writeToFile(fileName);
String newoldName = fileName.substring(0, fileName.lastIndexOf("."));
newoldName = newoldName + ".pdf";
Word2PdfUtil.wordToPdf(fileName, newoldName);
template.close();
//导出
PDFUtil.responseFileStream(res, newoldName);
PDFUtil.delFolder(realPath);

四、二维码生成工具类

import com.deepoove.poi.data.PictureRenderData;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;


/**
 * 二维码工具类
 *
 * @author 
 */
@Slf4j
public class QrCodeUtils {

    private final static Integer width = 100;
    private final static Integer height = 100;
    /**
     * 将 BufferedImage 转换为 Word 支持的 PictureRenderData
     */
    public static PictureRenderData convertToPictureRenderData(String imageByte1, String format) throws IOException {
        //二维码
        BufferedImage image = null;
        try {
            image = generateSampleImage(imageByte1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, format, baos);
        return new PictureRenderData(
                width, height, // 宽度和高度(单位:毫米)
                "." + format, // 图片格式扩展名(如 ".png")
                baos.toByteArray()
        );
    }

    /**
     * 示例:生成一个测试图片(二维码或任意BufferedImage)
     */
    private static BufferedImage generateSampleImage(String content) throws Exception {
        // 示例:用ZXing生成二维码
        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix matrix = writer.encode(
                content,
                BarcodeFormat.QR_CODE,
                width, height
        );
        return MatrixToImageWriter.toBufferedImage(matrix);
    }
}

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐