1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Java导出根据模板PDF(Springboot+Adobe Acrobat)

Java导出根据模板PDF(Springboot+Adobe Acrobat)

时间:2020-12-28 22:58:48

相关推荐

Java导出根据模板PDF(Springboot+Adobe Acrobat)

1、使用Adobe Acrobat创建PDF模板(设置文本域),文本域命名对应Java实体类

2、引入依赖

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

3、编写实现类和控制类

@Service@Slf4jpublic class PdfServiceImpl implements PdfService {@Overridepublic void generator(Invoice invoice, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException {// 模板名称String templateName = "Invoice_elle_April模板.pdf";String path = "";// 获取操作系统名称,根据系统名称确定模板存放的路径String systemName = System.getProperty("os.name");if (systemName.toUpperCase().startsWith("WIN")) {path = "C:/Users/Administrator/Desktop/";}// 生成导出PDF的文件名称String fileName = "Invoice.pdf";fileName = URLEncoder.encode(fileName, "UTF-8");// 设置响应头response.setCharacterEncoding("UTF-8");response.setContentType("application/pdf");response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);OutputStream out = null;PdfReader reader = null;ByteArrayOutputStream bos;PdfStamper stamper;try {// 保存到本地// out = new FileOutputStream(fileName);// 输出到浏览器端out = response.getOutputStream();// 读取PDF模板表单reader = new PdfReader(path + templateName);// 字节数组流,用来缓存文件流bos = new ByteArrayOutputStream();// 根据模板表单生成一个新的PDFstamper = new PdfStamper(reader, bos);// 获取新生成的PDF表单AcroFields form = stamper.getAcroFields();// 给表单生成中文字体,这里采用系统字体,不设置的话,中文显示会有问题BaseFont font = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);form.addSubstitutionFont(font);// 装配数据Map<String, Object> data = new HashMap<>(15);data.put("billedTo", invoice.getBilledTo());data.put("dateOfIssue", invoice.getDateOfIssue());data.put("invoiceNumber", invoice.getInvoiceNumber());data.put("amountDue", invoice.getAmountDue());data.put("description", invoice.getDescription());data.put("rate", invoice.getRate());data.put("qty", invoice.getQty());data.put("lineTotal", invoice.getLineTotal());data.put("total", invoice.getTotal());// 遍历data,给pdf表单赋值for (String key : data.keySet()) {form.setField(key, data.get(key).toString());}// 表明该PDF不可修改stamper.setFormFlattening(true);// 关闭资源stamper.close();// 将ByteArray字节数组中的流输出到out中(即输出到浏览器)Document doc = new Document();PdfCopy copy = new PdfCopy(doc, out);doc.open();PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);copy.addPage(importPage);doc.close();log.info("*****************************PDF导出成功*********************************");} catch (Exception e) {e.printStackTrace();} finally {try {if (out != null) {out.flush();out.close();}if (reader != null) {reader.close();}} catch (Exception e) {e.printStackTrace();}}}}

public interface PdfService {void generator(Invoice invoice, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException;}

@RestController@RequestMapping("/pdf")public class PdfController {@Autowiredprivate PdfService pdfService;@PostMapping("/export")public void generatorAdmissionCard(@RequestBody Invoice invoice, HttpServletResponse response) {try {pdfService.generator(invoice, response);} catch (UnsupportedEncodingException | FileNotFoundException e) {e.printStackTrace();}}}

4、导出效果(字体和换行可在Adobe Acrobat中设置)

参考文章:

Java如何实现Pdf的导出?

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。