文章目录 一、制作pdf模板1.1、使用excel制作一个表格1.2、转成pdf1.3、设置表单域1.4、最终模版效果 二、引入POM依赖三、代码实现3.1、工具类3.2、实体对象3.3、Controller 一、制作
我采用的是pdfelement 官网地址需要付费或者自行破解,也可以使用其他pdf编辑器。
com.itextpdf itextpdf 5.5.13 com.itextpdf itext-asian 5.2.0
将制作好的pdf模板放入项目resources/pdf目录下,如图
import cn.hutool.core.bean.BeanUtil;import cn.hutool.core.util.ObjectUtil;import cn.hutool.core.util.StrUtil;import com.itextpdf.text.DocumentException;import com.itextpdf.text.pdf.AcroFields;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.PdfReader;import com.itextpdf.text.pdf.PdfStamper;import lombok.extern.slf4j.Slf4j;import org.apache.commons.io.IOUtils;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.util.ResourceUtils;import javax.servlet.Http.httpservletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.net.URLEncoder;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;@Slf4jpublic class PdfUtil { private static boolean isPrimitiveOrWrapper(Class> clazz) { return clazz.isPrimitive() || clazz.getName().startsWith("java.lang"); } private static Map turnMap(Object object) { Map strinGobjectMap = BeanUtil.beanToMap(object); Map map = new HashMap<>(stringObjectMap.size() * 2); // 打印输出属性名称和属性值 for (Map.Entry entry : stringObjectMap.entrySet()) { String key = entry.geTKEy(); Object value = entry.getValue(); if (ObjectUtil.isNotEmpty(value)) { //基本类型和封装类型 if (isPrimitiveOrWrapper(value.getClass())) { map.put(key, String.valueOf(value)); } else { //其他类型 if (value instanceof List) { List
import lombok.Data;import java.util.List;@Datapublic class TestDto { private String name; private String birthday; private Other other; private List otherList; @Data public static class Other { private String career; private String company; }}
import io.swagger.annotations.api;import org.springframework.WEB.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.ArrayList;import java.util.List;@RestController@RequestMapping("test")@Api(tags = "测试")public class TestController { @GetMapping("/pdf") public void pdf(HttpServletRequest request, HttpServletResponse response) { TestDto testDto = new TestDto(); testDto.setName("张三"); testDto.setBirthday("2020-12-12"); TestDto.Other other = new TestDto.Other(); other.setCareer("码农"); other.setCompany("字节不跳动"); testDto.setOther(other); List list = new ArrayList<>(); for (int i = 0; i <= 2; i++) { TestDto.Other other1 = new TestDto.Other(); other1.setCareer("码农" + i); other1.setCompany("字节不跳动" + i); list.add(other1); } testDto.setOtherList(list); PdfUtil.generatePdf(request, response, "test.pdf", testDto, false); } }
浏览器访问ip:port/test/pdf,其中ip为你的ip地址,port为你的端口,访问结果如下:
来源地址:https://blog.csdn.net/zero_wsh/article/details/130845151
--结束END--
本文标题: java集成itextpdf实现通过pdf模板填充数据生成pdf
本文链接: https://www.lsjlt.com/news/373801.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0