ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    InputStream fontInputStream =MamCarAuthenticationController.class.getClassLoader().getResourceAsStream("font/simsun.ttf");
    		PdfFont font = PdfFontFactory.createFont(IOUtils.toByteArray(fontInputStream), PdfEncodings.IDENTITY_H, false);
    		String fileName = "cardtemplate.pdf";
    		String erweimaImg = "erweima.png";
    		InputStream inputStreamPdf = MamCarAuthenticationController.class.getClassLoader().getResourceAsStream("authtemplate/"+fileName);
    		PdfReader pdfReader =new PdfReader(inputStreamPdf);
    		PdfWriter pdfWriter =new PdfWriter(byteArrayOutputStream);
    		PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter);
    		PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
    		Map<String, PdfFormField> fields = form.getFormFields();
    		if(carAuthenticationDto!=null){
    			fields.get("cardno").setValue(carAuthenticationDto.getAuthenticationCode()==null?"":carAuthenticationDto.getAuthenticationCode()).setFont(font).setFontSize(10);
    			fields.get("carvin").setValue(carAuthenticationDto.getVarVin()==null?"":carAuthenticationDto.getVarVin()).setFont(font).setFontSize(12);
    			fields.get("carmodel").setValue(carAuthenticationDto.getCarModelName()==null?"":carAuthenticationDto.getCarModelName()).setFont(font).setFontSize(12);
    			fields.get("mileage").setValue((carAuthenticationDto.getMileage()==null?"0":carAuthenticationDto.getMileage())+"公里").setFont(font).setFontSize(12);
    			if(StringUtils.isNotBlank(carAuthenticationDto.getValidityTime())){
    				try {
    					Date validityTime =DateUtils.parseDate(carAuthenticationDto.getValidityTime(),"yyyy-MM-dd HH:mm:ss");
    					String validityTimeStr = DateUtils.formatDate(validityTime,"yyyy-MM-dd");
    					carAuthenticationDto.setValidityTime(validityTimeStr);
    				} catch (ParseException e) {
    					e.printStackTrace();
    			}else{
    				carAuthenticationDto.setValidityTime("");
    			fields.get("createdate").setValue(carAuthenticationDto.getValidityTime()).setFont(font).setFontSize(12);
    		}else{
    			fields.get("cardno").setValue("").setFont(font).setFontSize(12);
    			fields.get("carvin").setValue("").setFont(font).setFontSize(12);
    			fields.get("carmodel").setValue("").setFont(font).setFontSize(12);
    			fields.get("mileage").setValue("").setFont(font).setFontSize(12);
    			fields.get("createdate").setValue("").setFont(font).setFontSize(12);
    		if(detectionDataBaseinfo!=null){
    			fields.get("engineno").setValue(detectionDataBaseinfo.getEngineNo()==null?"":detectionDataBaseinfo.getEngineNo()).setFont(font).setFontSize(12);
    		}else{
    			fields.get("engineno").setValue("").setFont(font).setFontSize(12);
    		form.flattenFields();//设置表单域不可编辑
    		InputStream inputStreamerweimaImg = MamCarAuthenticationController.class.getClassLoader().getResourceAsStream("authtemplate/"+erweimaImg);
    		Image image = new Image(ImageDataFactory.create(IOUtils.toByteArray(inputStreamerweimaImg)));
    		image.scaleToFit(50, 50);
    		image.setFixedPosition(450, 160);
    		Document doc = new Document(pdfDoc, PageSize.A4.rotate());
    		doc.add(image);
    		doc.close();
    		pdfWriter.flush();
    		pdfWriter.close();
    		byteArrayOutputStream.writeTo(response.getOutputStream());
 
  • 说明 1)字体使用的宋体,原因是宋体兼容好些,不至于生成PDF后乱码。 2)springboot打包成jar,获取资源路径必须用流,使用path会取不到文件,代码如: MamCarAuthenticationController.class.getClassLoader().getResourceAsStream("authtemplate/"+fileName); 3)PDF模板制作,需要添加form元素,加入对应的key;fields.get("engineno").setValue("") value不能设置空对象,所以需要判断空的情况,为空使用“”来设置。
  •