Java 使用 itextpdf 自定义 生成 pdf
maven 依赖
<!-- iText 7 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.3</version>
<type>pom</type>
</dependency>
<!-- fastjson2 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.28</version>
</dependency>
实现
public static void generatePDF(JSONObject data, File file,String path) throws IOException {
PdfWriter writer = new PdfWriter(file);
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc);
PdfFont simkaiFont = PdfFontFactory.createFont(path + "simkai.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
PdfFont simsunFont = PdfFontFactory.createFont(path + "simsun.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
PdfFont stzhongsFont = PdfFontFactory.createFont(path + "stzhongs.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
document.setMargins(42.7f, 38f, 42.7f, 38f);
Paragraph courtLevel = new Paragraph(data.getString("court_level"))
.setFont(stzhongsFont)
.setFontSize(22)
.setFontColor(DeviceRgb.BLACK)
.setMultipliedLeading(1.5f)
.setMarginTop(10)
.setTextAlignment(TextAlignment.CENTER);
document.add(courtLevel);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler(data.getString("case_name"), simkaiFont, simsunFont));
Paragraph caseType = new Paragraph(getCaseTypeText(data))
.setFont(stzhongsFont)
.setFontSize(26)
.setFontColor(DeviceRgb.BLACK)
.setMultipliedLeading(1.5f)
.setTextAlignment(TextAlignment.CENTER);
document.add(caseType);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler(data.getString("case_name"), simkaiFont, simsunFont));
Paragraph caseId = new Paragraph(data.getString("case_id"))
.setFont(simkaiFont)
.setFontSize(15)
.setFontColor(DeviceRgb.BLACK)
.setFixedLeading(24)
.setMarginTop(12)
.setMarginBottom(24)
.setTextAlignment(TextAlignment.RIGHT);
document.add(caseId);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler(data.getString("case_name"), simkaiFont, simsunFont));
List<String> contentList = JsonUtil.getList(data, "content", String.class);
float indent = 15 * 2;
for (String content : contentList) {
Paragraph contentPara = new Paragraph(content)
.setFont(simkaiFont)
.setFontSize(15)
.setFontColor(DeviceRgb.BLACK)
.setFixedLeading(24)
.setFirstLineIndent(indent)
.setMultipliedLeading(1.5f)
.setTextAlignment(TextAlignment.LEFT);
document.add(contentPara);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler(data.getString("case_name"), simkaiFont, simsunFont));
}
List<String> signatureList = JsonUtil.getList(data, "signature", String.class);
for (String signature : signatureList) {
Paragraph signaturePara = new Paragraph(signature)
.setFont(simkaiFont)
.setFontSize(15)
.setFontColor(DeviceRgb.BLACK)
.setFixedLeading(24)
.setTextAlignment(TextAlignment.RIGHT);
document.add(signaturePara);
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler(data.getString("case_name"), simkaiFont, simsunFont));
}
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new FooterEventHandler(data.getString("case_name"), simkaiFont, simsunFont));
document.close();
}
docker 服务 字体文件找不到问题
服务名: # 我这里是微服务 整合了 docker-compose
build:
dockerfile: # Dockerfile 位置
context: .
image: 镜像名
container_name: 容器名
environment:
JVM_PARAM: ${JVM_PARAM} # jvm启动参数
BOARDING_HOST_IP: ${BOARDING_HOST_IP} # 宿主机IP
BOARDING_HOST_PORT: ${BOARDING_HOST_PORT} # 宿主机IP
restart: always
ports: # 端口映射
- "${BOARDING_HOST_PORT}:${BOARDING_HOST_PORT}"
volumes: # 数据卷 挂载文件目录
- /home/fonts:/usr/local/fonts