PDFUtil.java
6.26 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.subsidy.util;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
@Slf4j
@Component
public class PDFUtil {
static BaseFont baseFont;
{
try {
baseFont = BaseFont.createFont("https://zhongzhi-cms.oss-cn-shanghai.aliyuncs.com/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void tableStyle(PdfPTable table, int[] cellsWidth) throws DocumentException {
table.setWidths(cellsWidth);
table.setWidthPercentage(100);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setFixedHeight(30);
table.getDefaultCell().setBackgroundColor(BaseColor.BLUE);
table.getDefaultCell().setPadding(0);
table.getDefaultCell().setBorderWidth(0);
}
/**
* 流化下载
*
* @param bytes
* @param filename
*/
public static void renderPdf(final byte[] bytes, final String filename, String type) {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse();
initResponseHeader(response, "application/" + type);
setFileDownloadHeader(response, filename, "." + type);
if (null != bytes) {
try {
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
}
/**
* 分析并设置contentType与headers.
*/
private static HttpServletResponse initResponseHeader(HttpServletResponse response, final String contentType, final String... headers) {
// 分析headers参数
String encoding = "utf-8";
boolean noCache = true;
for (String header : headers) {
String headerName = StringUtils.substringBefore(header, ":");
String headerValue = StringUtils.substringAfter(header, ":");
if (StringUtils.equalsIgnoreCase(headerName, "utf-8")) {
encoding = headerValue;
} else if (StringUtils.equalsIgnoreCase(headerName, "no-cache")) {
noCache = Boolean.parseBoolean(headerValue);
} else {
throw new IllegalArgumentException(headerName + "不是一个合法的header类型");
}
}
// 设置headers参数
String fullContentType = contentType + ";charset=" + encoding;
response.setContentType(fullContentType);
if (noCache) {
// Http 1.0 header
response.setDateHeader("Expires", 0);
response.addHeader("Pragma", "no-cache");
// Http 1.1 header
response.setHeader("Cache-Control", "no-cache");
}
return response;
}
/**
* 设置让浏览器弹出下载对话框的Header.
*
* @param
*/
public static void setFileDownloadHeader(HttpServletResponse response, String fileName, String fileType) {
try {
// 中文文件名支持
String encodedfileName = new String(fileName.getBytes(StandardCharsets.UTF_8), "ISO8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + fileType + "\"");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String idCard2Gender(String idCard) {
int genderDigit = Integer.parseInt(idCard.substring(16, 17));
return (genderDigit % 2 == 0) ? "女" : "男";
}
public static Integer birthDate2Age(String birthDateStr){
if (birthDateStr == null || birthDateStr.trim().isEmpty()) {
return -1;
}
try {
// 定义日期格式(yyyy/MM/dd)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
// 解析出生日期
LocalDate birthDate = LocalDate.parse(birthDateStr, formatter);
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 计算基础年龄
int age = currentDate.getYear() - birthDate.getYear();
// 校验月份和日期,调整年龄
if (currentDate.getMonthValue() < birthDate.getMonthValue()) {
age--;
} else if (currentDate.getMonthValue() == birthDate.getMonthValue()) {
if (currentDate.getDayOfMonth() < birthDate.getDayOfMonth()) {
age--;
}
}
// 防止出生日期异常(如未来日期)
return age < 0 ? -1 : age;
} catch (DateTimeParseException e) {
// 日期格式错误(如 "2001/13/03" 或 "2001-01-03")
return -1;
}
}
public static String idCard2BirthDate(String idCard) {
if (idCard == null || idCard.length() != 18) {
throw new IllegalArgumentException("身份证号码不合法");
}
String birthDateStr = idCard.substring(6, 14);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
try {
Date birthDate = sdf.parse(birthDateStr);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy/MM/dd");
return outputFormat.format(birthDate);
} catch (Exception e) {
throw new IllegalArgumentException("身份证号码中的出生日期不合法");
}
}
}