面试通过函详情及通过函生成和OSS服务器上传文件

This commit is contained in:
pserimal
2023-06-15 19:02:01 +08:00
parent 3fb8e439ec
commit d80c70deb7
15 changed files with 266 additions and 65 deletions

View File

@@ -94,10 +94,11 @@ public class PDFUtils {
* @param color 字体颜色
*/
private static void addContent(PdfReader reader, OutputStream outputStream, String content, float x, float y, BaseFont baseFont, float fontSize, int fontStyle, Color color) {
PdfStamper stamper = null;
try {
//生成 paragraph 并放在正确位置
//抠模板
PdfStamper stamper = new PdfStamper(reader, outputStream);
stamper = new PdfStamper(reader, outputStream);
PdfContentByte over = stamper.getOverContent(1);
Font font = new Font(baseFont, fontSize, fontStyle, color);
Paragraph insertContent = new Paragraph(content, font);
@@ -110,6 +111,14 @@ public class PDFUtils {
stamper.close();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (stamper != null) {
stamper.close();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}

View File

@@ -22,19 +22,16 @@ public class PassLetterUtils {
* 返回通过函编码
* @param partnerName 加盟商姓名
* @param verifyCity 审批城市
* @param passCode 通过函编码
* @param passTime 审批通过时间
*/
public static String genPassLetter(String partnerName, String passCode, String verifyCity, DateTime passTime) {
String randomNum = RandomUtil.randomNumbers(5);
if (ObjectUtil.isEmpty(passCode)) {
passCode = "HSAY" + DateUtil.format(passTime, "yyMMdd") + "-" + randomNum;
}
public static ByteArrayOutputStream genPassLetter(String partnerName, String passCode, String verifyCity, DateTime passTime) {
String passTimeStr = DateUtil.format(passTime, "yyyy年MM月dd日");
Document document = new Document();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
//1. 创建 pdf document
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FileOutputStream outputStream = new FileOutputStream("passLetter.pdf");
// FileOutputStream outputStream = new FileOutputStream("passLetter.pdf");
PdfWriter.getInstance(document, outputStream);
document.open();
@@ -44,21 +41,38 @@ public class PassLetterUtils {
document.close();
//3. 填写通过函模板信息
addContentToPdf(partnerName + " 先生/女士", 122, 640);
addContentToPdf(passCode, 122, 558);
addContentToPdf(verifyCity, 155, 494);
addContentToPdf("60天", 135, 450);
addContentToPdf(passTimeStr, 393, 152);
return passCode;
addContentToPdf(outputStream, partnerName + " 先生/女士", 122, 640);
addContentToPdf(outputStream, passCode, 122, 558);
addContentToPdf(outputStream, verifyCity, 155, 494);
addContentToPdf(outputStream, "60天", 135, 450);
addContentToPdf(outputStream, passTimeStr, 393, 152);
return outputStream;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
document.close();
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static void addContentToPdf(String content, float x, float y) {
/**
* 生成 passCode 的方法,拆分出来方便单独获取 passCode
* @return
*/
public static String genPassCode(DateTime passTime) {
String randomNum = RandomUtil.randomNumbers(5);
String passCode = "HSAY" + DateUtil.format(passTime, "yyMMdd") + "-" + randomNum;
return passCode;
}
private static void addContentToPdf(ByteArrayOutputStream outputStream, String content, float x, float y) {
try {
PdfReader pdfReader = new PdfReader(Files.newInputStream(Paths.get("passLetter.pdf")));
OutputStream outputStream = Files.newOutputStream(Paths.get("passLetter.pdf"));
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
PdfReader pdfReader = new PdfReader(inputStream);
PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, 20, new Color(255, 82,25));
} catch (IOException e) {
throw new RuntimeException(e);