java根据模板生成PDF
Posted lyon91
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java根据模板生成PDF相关的知识,希望对你有一定的参考价值。
首先你的制作一个pdf模板:
1.先用word做出模板界面
画单元格的时候需要考虑值的长度,像这里的状态可能会很长
2.文件另存为pdf格式文件
使用福昕PDF 打开,添加文本,以及需要添加值的地方,设置文本域,这个就是属性值
保存以后会生成PDF封面,需要去掉。
在线分封PDF
https://smallpdf.com/cn/result#r=1d5e4fbd858366eb5e4abe33a71ba648&t=split
代码
@GetMapping("/downOrderDashboard/orderNo") @ApiAuthority public ResponseEntity<byte[]> downOrderDashboard(@PathVariable String orderNo, HttpServletResponse response, HttpServletRequest request) throws Exception ByteArrayOutputStream byteArrayOutputStream = serialNumberService.downOrderDashboard(orderNo, request); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentDispositionFormData("attachment", java.net.URLEncoder.encode("orderDB.pdf", "UTF-8")); httpHeaders.setContentType(MediaType.parseMediaType("application/pdf")); byte[] bytes = byteArrayOutputStream.toByteArray(); return new ResponseEntity<byte[]>(bytes, httpHeaders, HttpStatus.CREATED); /*response.setContentType("application/pdf"); response.setContentLength(byteArrayOutputStream.size()); ServletOutputStream out = response.getOutputStream(); byteArrayOutputStream.writeTo(out); out.flush();*/
public ByteArrayOutputStream downOrderDashboard(String orderNo, HttpServletRequest request) log.info("orderNo=" + orderNo); ByteArrayOutputStream outputstream = null; OrderDBDTO orderDashBoard = getOrderDashBoard(orderNo); log.info("orderDashBoard" + orderDashBoard.toString()); try //InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("classpath:pdf/orderDB.pdf"); ClassPathResource classPathResource = new ClassPathResource("pdf/orderDB.pdf"); InputStream inputStream =classPathResource.getInputStream(); //File file = ResourceUtils.getFile("classpath:pdf/orderDB.pdf"); //String currentPath = request.getServletContext().getRealPath("\\\\"); //String TemplatePDF = currentPath + "resources\\\\"+ "pdf\\\\" + "orderDB.pdf"; log.info("inputStream"+inputStream); PdfReader reader = new PdfReader(inputStream); //new PdfReader(file.getAbsolutePath()); outputstream = new ByteArrayOutputStream(4096); PdfStamper stamp = new PdfStamper(reader, outputstream); PdfContentByte under = stamp.getUnderContent(1); /*使用中文字体*/ BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // Font FontChinese = new Font(bf, 10, Font.NORMAL); AcroFields form = stamp.getAcroFields(); form.addSubstitutionFont(bf); //简宋体 form.setField("productionOrderNo", orderDashBoard.getProductionOrderNo()); OrderStatusType[] values = OrderStatusType.values(); for (OrderStatusType value : values) if(orderDashBoard.getStatus().equals(value.getResourceKey())) form.setField("state",value.getResourceKey() ); form.setField("batchNo", orderDashBoard.getBatchNo()); form.setField("lineName", orderDashBoard.getLineName()); form.setField("productionDate", orderDashBoard.getProductionDate().format(DateTimeFormatter.ISO_LOCAL_DATE)); form.setField("expiryDate", orderDashBoard.getExpiryDate().format(DateTimeFormatter.ISO_LOCAL_DATE)); List<OrderSn> orderSnList = orderDashBoard.getOrderSnList(); for (int i = 0; i < orderSnList.size(); i++) form.setField("aggregationLevel"+(i+1), orderSnList.get(i).getAggreLevelName()); form.setField("productCount"+(i+1), orderSnList.get(i).getProductCount()+""); form.setField("breakCount"+(i+1), orderSnList.get(i).getBreakCount()+""); form.setField("sampleCount"+(i+1), orderSnList.get(i).getSampleCount()+""); stamp.setFormFlattening(true); stamp.close(); reader.close(); catch (DocumentException e1) log.info(e1.getMessage()); e1.printStackTrace(); catch (IOException e2) log.info(e2.getMessage()); e2.printStackTrace(); return outputstream;
以上是关于java根据模板生成PDF的主要内容,如果未能解决你的问题,请参考以下文章