Browse Source

增加推盘二维码的pdf下载

dist.1.3.0
chenlong 8 months ago
parent
commit
9ca9ce78cd
  1. 15
      blade-service/logpm-basicdata/pom.xml
  2. 6
      blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataTrayController.java
  3. 2
      blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/service/IBasicdataTrayService.java
  4. 87
      blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/service/impl/BasicdataTrayServiceImpl.java
  5. 22
      blade-service/logpm-basicdata/src/main/resources/static/tray-pdf-template.html

15
blade-service/logpm-basicdata/pom.xml

@ -83,6 +83,21 @@
<artifactId>xxl-job-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>8.0.5</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>5.0.5</version>
</dependency>
</dependencies>

6
blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/controller/BasicdataTrayController.java

@ -260,6 +260,12 @@ public class BasicdataTrayController extends BladeController {
return R.data(trayService.printCode(Func.toLongList(ids)));
}
@GetMapping("/getPDF")
public void getPDF(@ApiParam(value = "主键集合", required = true) @RequestParam String ids,
HttpServletResponse response) throws Exception {
trayService.downloadCodePDF(Func.toLongList(ids), response);
}
/**
* 导出数据
*/

2
blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/service/IBasicdataTrayService.java

@ -29,6 +29,7 @@ import com.logpm.warehouse.vo.WarehouseTraybarCode;
import org.springblade.core.mp.base.BaseService;
import org.springblade.core.tool.api.R;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@ -104,6 +105,7 @@ public interface IBasicdataTrayService extends BaseService<BasicdataTrayEntity>
* 托盘 打印包条
*/
WarehouseTraybarCode printCode(List<Long> ids) throws Exception;
void downloadCodePDF(List<Long> ids, HttpServletResponse response) throws Exception;
/**
* 托盘内容查询
* @param id

87
blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/service/impl/BasicdataTrayServiceImpl.java

@ -22,6 +22,12 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ibm.icu.impl.data.ResourceReader;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.font.FontProvider;
import com.logpm.basic.entity.BasicPrintTemplateEntity;
import com.logpm.basic.feign.IBasicPrintTemplateClient;
import com.logpm.basicdata.dto.BasicdataTrayDTO;
@ -42,6 +48,7 @@ import com.logpm.warehouse.vo.WarehouseTrayQRCode;
import com.logpm.warehouse.vo.WarehouseTraybarCode;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.springblade.common.constant.CodeNumConstant;
import org.springblade.common.utils.QRCodeUtil;
import org.springblade.common.utils.TemplateUtil;
@ -52,12 +59,19 @@ import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTML;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* 托盘 服务实现类
@ -239,7 +253,80 @@ public class BasicdataTrayServiceImpl extends BaseServiceImpl<BasicdataTrayMappe
}
@Override
public void downloadCodePDF(List<Long> ids, HttpServletResponse response) throws Exception {
BasicPrintTemplateEntity template = basicPrintTemplateClient.getTrayCode();
if (ObjectUtil.isEmpty(template)) {
throw new ServiceException("模板内容未找到");
}
if(ObjectUtils.isNull(ids)){
throw new ServiceException("参数错误");
}
//查询登录租户
BladeUser user = AuthUtil.getUser();
user.getTenantId();
response.setContentType(MediaType.APPLICATION_PDF_VALUE);
PdfWriter pdfWriter = new PdfWriter(response.getOutputStream());
PdfDocument document = new PdfDocument(pdfWriter);
ConverterProperties converterProperties = new ConverterProperties();
FontProvider fontProvider = new FontProvider();
fontProvider.addStandardPdfFonts();
fontProvider.addFont("STSong-Light", "UniGB-UCS2-H");
converterProperties.setFontProvider(fontProvider);
StringBuilder PDFHtml = new StringBuilder();
String htmlTemplate = getTrayCodePdfTemplate();
int index = 0;
for (Long id : ids) {
index++;
WarehouseTrayQRCode trayQRCode = baseMapper.getTray(id);
String filename = QRCodeUtil.createCodeToFile(trayQRCode.getTrayCode());
String fileTypeName = QRCodeUtil.createCodeToFileType(trayQRCode.getTrayCode());
String code = QRCodeUtil.getEmpAutograph(filename);
String html = htmlTemplate.replaceAll("\\$\\{qrCode}", code)
.replaceAll("\\$\\{palletName}", trayQRCode.getPalletName())
.replaceAll("\\$\\{trayCode}", trayQRCode.getTrayCode())
.replaceAll("\\$\\{warehouseName}", trayQRCode.getWarehouseName())
.replaceAll("\\$\\{tenant}", "汇通物流")
.replaceAll("\\$\\{imgType}", QRCodeUtil.getEmpAutograph(fileTypeName));
PDFHtml.append(html);
if (index % 4 == 0) {
PDFHtml.append("<div style=\"page-break-before: always;\"></div>");
}
}
HtmlConverter.convertToPdf(new ByteArrayInputStream(PDFHtml.toString().getBytes()), document, converterProperties);
document.close();
pdfWriter.close();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("托盘码.pdf", "UTF-8"));
}
private String getTrayCodePdfTemplate() throws IOException {
ClassLoader classLoader = ResourceReader.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream("static/tray-pdf-template.html");
if (resourceAsStream != null) {
try {
return IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}finally {
resourceAsStream.close();
}
}
return "";
}
@Override
public BasicdataTrayVO getOneById(Long id) {

22
blade-service/logpm-basicdata/src/main/resources/static/tray-pdf-template.html

@ -0,0 +1,22 @@
<div>
<div>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Document</title></div>
<table style="margin-bottom:50px;border-collapse: collapse;width: 100%;text-align: center;font-size: 17px;font-weight: bold;border-left: 1px dashed #000;border-top: 1px dashed #000;">
<tr> <!-- 二维码 -->
<td rowspan="3"
style="width: 50%;padding: 10px;border-right: 1px dashed #000;border-bottom: 1px dashed #000;">
<img style="width: 140px; height: 140px" src="${qrCode}"/>
<div style="text-align: center">${trayCode}</div>
</td>
<td style="border-right: 1px dashed #000; height:58px;border-bottom: 1px dashed #000">${tenant}</td>
</tr>
<tr>
<td style="border-right: 1px dashed #000; height:58px;border-bottom: 1px dashed #000"> ${warehouseName}</td>
</tr>
<tr>
<td style="border-right: 1px dashed #000; height:58px;border-bottom: 1px dashed #000">${palletName}</td>
</tr>
</table>
</div>
Loading…
Cancel
Save