1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| public TableDataInfo getCertificateImage(ResourceFile resourceFile) { TableDataInfo tableDataInfo = new TableDataInfo(); PageData pd = getPageData();
try { List<ResourceFile> resourceFiles = resourceFileService.list(new QueryWrapper<ResourceFile>().eq("id", resourceFile.getId())); String oldFilePath = resourceFiles.get(0).getResourcePath() + resourceFiles.get(0).getResourceFileName(); String fileName = "/" +UUID.randomUUID() + ".pdf"; String resourcePath = "/certificate" + getFilePath(); String newPDFPath = linuxPath + resourcePath + "/" + fileName;
PdfReader reader = new PdfReader(oldFilePath); ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos); PdfContentByte under = ps.getUnderContent(1); System.out.println("采用PDF模板:" + oldFilePath);
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>(); fontList.add(bf);
AcroFields fields = ps.getAcroFields(); fields.setSubstitutionFonts(fontList);
fileData(fields, data());
ps.setFormFlattening(true); ps.close();
OutputStream fos = new FileOutputStream(newPDFPath); fos.write(bos.toByteArray()); System.out.println("新证书已生成:" + newPDFPath);
resourceFile.setCreateId(getUserId().intValue()); resourceFile.setCreator(getLoginUser().getUser().getNickName()); resourceFile.setCreateTime(new Date()); resourceFile.setResourcePath(resourcePath + "/"); resourceFile.setResourceFileName(fileName); resourceFile.setRealFileName(pd.getString("testPeople") + "的证书"); resourceFileService.save(resourceFile);
fos.flush(); fos.close(); bos.close();
PageData pdd = new PageData(); List<PageData> list = new ArrayList(); pdd.put("resourceFileId", resourceFile.getId()); pdd.put("creator", getLoginUser().getUser().getNickName()); pdd.put("fileName", fileName); pdd.put("realFileName", pd.getString("testPeople") + "的证书"); pdd.put("fileUrl", path + resourcePath + "/" + fileName); tableDataInfo.setMsg("下载证书中,请耐心等待"); list.add(pdd); tableDataInfo.setRows(list); tableDataInfo.setCode(HttpStatus.SUCCESS); return tableDataInfo; } catch (Exception e) { tableDataInfo.setMsg("下载证书失败,请联系管理人员"); tableDataInfo.setCode(HttpStatus.ERROR); return tableDataInfo; } public static void fileData(AcroFields fields, Map<String, String> data) throws IOException, DocumentException { for (String key : data.keySet()) { String value = data.get(key); System.out.println(key + "字段:" + value); fields.setField(key, value); } }
public static Map<String, String> data() { Map<String, String> data = new HashMap<String, String>(); data.put("name", "最帅"); data.put("time", "天荒地老"); return data; }
public String getFilePath() { String path = DateUtilsTwo.getDay(); return "/" + path.replaceAll("-", ""); }
|