4 changed files with 204 additions and 0 deletions
-
4build.gradle
-
61src/main/java/com/gaotao/modules/base/controller/LabelSettingController.java
-
117src/main/java/com/gaotao/modules/base/service/Impl/PdfExportServiceImpl.java
-
22src/main/java/com/gaotao/modules/base/service/PdfExportService.java
@ -0,0 +1,117 @@ |
|||||
|
package com.gaotao.modules.base.service.Impl; |
||||
|
|
||||
|
import com.gaotao.modules.base.service.PdfExportService; |
||||
|
import com.itextpdf.io.image.ImageData; |
||||
|
import com.itextpdf.io.image.ImageDataFactory; |
||||
|
import com.itextpdf.kernel.colors.ColorConstants; |
||||
|
import com.itextpdf.kernel.colors.DeviceRgb; |
||||
|
import com.itextpdf.kernel.geom.PageSize; |
||||
|
import com.itextpdf.kernel.pdf.PdfDocument; |
||||
|
import com.itextpdf.kernel.pdf.PdfWriter; |
||||
|
import com.itextpdf.layout.Document; |
||||
|
import com.itextpdf.layout.borders.SolidBorder; |
||||
|
import com.itextpdf.layout.element.Image; |
||||
|
import com.itextpdf.layout.element.Paragraph; |
||||
|
import com.itextpdf.layout.properties.HorizontalAlignment; |
||||
|
import com.itextpdf.layout.properties.TextAlignment; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.net.URL; |
||||
|
import java.util.Base64; |
||||
|
|
||||
|
/** |
||||
|
* PDF导出服务实现 - Java 21兼容版本 |
||||
|
* @author Kiro |
||||
|
* @date 2025/1/24 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class PdfExportServiceImpl implements PdfExportService { |
||||
|
|
||||
|
@Override |
||||
|
public ByteArrayOutputStream exportPreviewToPdf(String imageUrl, String reportId) throws Exception { |
||||
|
log.info("开始导出预览图为PDF: reportId={}", reportId); |
||||
|
|
||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
||||
|
PdfWriter writer = new PdfWriter(baos); |
||||
|
PdfDocument pdfDoc = new PdfDocument(writer); |
||||
|
Document document = new Document(pdfDoc, PageSize.A4); |
||||
|
|
||||
|
try { |
||||
|
// 处理图片 - 直接添加预览图,不需要标题和时间戳 |
||||
|
ImageData imageData = getImageData(imageUrl); |
||||
|
if (imageData != null) { |
||||
|
Image image = new Image(imageData); |
||||
|
|
||||
|
// 调整图片大小以适应页面,留更多空间给图片 |
||||
|
float pageWidth = PageSize.A4.getWidth() - 72; |
||||
|
float pageHeight = PageSize.A4.getHeight() - 72; |
||||
|
|
||||
|
if (image.getImageWidth() > pageWidth || image.getImageHeight() > pageHeight) { |
||||
|
image.scaleToFit(pageWidth, pageHeight); |
||||
|
} |
||||
|
|
||||
|
// 直接添加带边框的图片,无需容器 |
||||
|
image.setBorder(new SolidBorder(new DeviceRgb(221, 221, 221), 1)); |
||||
|
image.setHorizontalAlignment(HorizontalAlignment.CENTER); |
||||
|
document.add(image); |
||||
|
} else { |
||||
|
// 如果无法加载图片,显示简单的错误信息 |
||||
|
Paragraph errorParagraph = new Paragraph("无法加载预览图片"); |
||||
|
errorParagraph.setTextAlignment(TextAlignment.CENTER); |
||||
|
errorParagraph.setFontSize(12); |
||||
|
document.add(errorParagraph); |
||||
|
} |
||||
|
|
||||
|
log.info("PDF导出成功: reportId={}", reportId); |
||||
|
|
||||
|
} finally { |
||||
|
document.close(); |
||||
|
} |
||||
|
|
||||
|
return baos; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取图片数据 |
||||
|
*/ |
||||
|
private ImageData getImageData(String imageSource) { |
||||
|
try { |
||||
|
if (imageSource == null || imageSource.trim().isEmpty()) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
// 检查是否是Base64数据 |
||||
|
if (imageSource.startsWith("data:image/")) { |
||||
|
String base64Data = imageSource.substring(imageSource.indexOf(",") + 1); |
||||
|
byte[] imageBytes = Base64.getDecoder().decode(base64Data); |
||||
|
return ImageDataFactory.create(imageBytes); |
||||
|
} |
||||
|
|
||||
|
// 检查是否是网络URL |
||||
|
if (imageSource.startsWith("http://") || imageSource.startsWith("https://")) { |
||||
|
return ImageDataFactory.create(new URL(imageSource)); |
||||
|
} |
||||
|
|
||||
|
// 尝试作为本地文件路径处理 |
||||
|
return ImageDataFactory.create(imageSource); |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
log.error("获取图片数据失败: {}", e.getMessage()); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 通过Labelary API生成预览图 |
||||
|
*/ |
||||
|
private String generatePreviewImage(String zplCode, String paperSize, Integer dpi) throws IOException { |
||||
|
log.warn("ZPL预览图生成功能需要实现Labelary API调用"); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
package com.gaotao.modules.base.service; |
||||
|
|
||||
|
import org.springframework.stereotype.Service; |
||||
|
import java.io.ByteArrayOutputStream; |
||||
|
|
||||
|
/** |
||||
|
* PDF导出服务接口 |
||||
|
* @author Kiro |
||||
|
* @date 2025/1/24 |
||||
|
*/ |
||||
|
@Service |
||||
|
public interface PdfExportService { |
||||
|
|
||||
|
/** |
||||
|
* 将预览图片导出为PDF |
||||
|
* @param imageUrl 预览图片URL或Base64数据 |
||||
|
* @param reportId 标签ID |
||||
|
* @return PDF字节数组 |
||||
|
*/ |
||||
|
ByteArrayOutputStream exportPreviewToPdf(String imageUrl, String reportId) throws Exception; |
||||
|
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue