文章目录
1.1、使用excel制作一个表格
1.2、转成pdf
我采用的是pdfelement 官网地址需要付费或者自行破解,也可以使用其他pdf编辑器。
1.3、设置表单域
1.4、最终模版效果
com.itextpdf itextpdf 5.5.13 com.itextpdf itext-asian 5.2.0
将制作好的pdf模板放入项目resources/pdf目录下,如图
3.1、工具类
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
3.2、实体对象
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; }}
3.3、Controller
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