|
|
@@ -57,6 +57,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
@@ -401,7 +402,7 @@ public class TaskSignServiceImpl implements TaskSignService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 将PDF转为PNG图片流(用于小程序预览)
|
|
|
+ * 将PDF转为PNG图片流(用于小程序预览)
|
|
|
*
|
|
|
* @param pdfBytes PDF文件字节数组
|
|
|
* @return PNG图片的字节输出流
|
|
|
@@ -409,13 +410,28 @@ public class TaskSignServiceImpl implements TaskSignService {
|
|
|
*/
|
|
|
@Override
|
|
|
public ByteArrayOutputStream pdfToImage(byte[] pdfBytes) throws Exception {
|
|
|
+ // 参数校验
|
|
|
+ if (pdfBytes == null || pdfBytes.length == 0) {
|
|
|
+ throw new ServiceException(400, "PDF数据不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证PDF文件格式(至少需要包含 %PDF 标识)
|
|
|
+ if (pdfBytes.length < 5 || !(pdfBytes[0] == '%' && pdfBytes[1] == 'P' && pdfBytes[2] == 'D' && pdfBytes[3] == 'F')) {
|
|
|
+ log.error("无效的PDF文件格式,文件大小: {} bytes, 前10字节: {}", pdfBytes.length,
|
|
|
+ java.util.Arrays.toString(java.util.Arrays.copyOfRange(pdfBytes, 0, Math.min(10, pdfBytes.length))));
|
|
|
+ throw new ServiceException(400, "文件格式不正确,不是有效的PDF文件");
|
|
|
+ }
|
|
|
+
|
|
|
try (PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes))) {
|
|
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
|
|
BufferedImage image = pdfRenderer.renderImageWithDPI(0, 150);
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
ImageIO.write(image, "png", bos);
|
|
|
-
|
|
|
+
|
|
|
return bos;
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("PDF转图片失败,文件大小: {} bytes, 错误信息: {}", pdfBytes.length, e.getMessage(), e);
|
|
|
+ throw new ServiceException(500, "PDF文件解析失败,可能文件已损坏或格式不正确");
|
|
|
}
|
|
|
}
|
|
|
|