添加礼薄详情导出PDF功能
This commit is contained in:
@@ -59,5 +59,17 @@
|
||||
<groupId>cn.xxzhx.giftBook</groupId>
|
||||
<artifactId>giftBook-common-tenant</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- iText 9 -->
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>kernel</artifactId>
|
||||
<version>9.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.itextpdf</groupId>
|
||||
<artifactId>layout</artifactId>
|
||||
<version>9.1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package cn.xxzhx.giftBook.main.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import cn.xxzhx.giftBook.common.excel.core.ExcelResult;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.constraints.*;
|
||||
@@ -88,7 +89,7 @@ public class TGiftBookDetailsController extends BaseController {
|
||||
@SaCheckPermission("main:giftBookDetails:query")
|
||||
@GetMapping("/{id}")
|
||||
public R<TGiftBookDetailsVo> getInfo(@NotNull(message = "主键不能为空")
|
||||
@PathVariable Long id) {
|
||||
@PathVariable Long id) {
|
||||
return R.ok(tGiftBookDetailsService.queryById(id));
|
||||
}
|
||||
|
||||
@@ -126,4 +127,23 @@ public class TGiftBookDetailsController extends BaseController {
|
||||
@PathVariable Long[] ids) {
|
||||
return toAjax(tGiftBookDetailsService.deleteWithValidByIds(List.of(ids), true));
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出礼薄详情pdf
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SaCheckPermission("main:giftBookDetails:export")
|
||||
@Log(title = "礼薄详情", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/exportPDF")
|
||||
public void exportPDF(TGiftBookDetailsBo bo, HttpServletResponse response) throws IOException {
|
||||
// 设置响应头
|
||||
response.setContentType("application/pdf");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"giftBookDetails.pdf\"");
|
||||
|
||||
// 生成PDF并写入响应流
|
||||
byte[] pdfBytes = tGiftBookDetailsService.exportPDF(bo);
|
||||
response.getOutputStream().write(pdfBytes);
|
||||
response.getOutputStream().flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,5 +21,5 @@ public interface TGiftBookDetailsMapper extends BaseMapperPlus<TGiftBookDetails,
|
||||
|
||||
Page<TGiftBookDetailsVo> selectPageList(@Param("bo") TGiftBookDetailsBo bo, IPage<TGiftBookDetailsVo> page);
|
||||
|
||||
List<TGiftBookDetailsVo> queryList(TGiftBookDetailsBo bo);
|
||||
List<TGiftBookDetailsVo> queryList(@Param("bo") TGiftBookDetailsBo bo,@Param("pageQuery") PageQuery pageQuery);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.xxzhx.giftBook.main.domain.vo.TGiftBookDetailsVo;
|
||||
import cn.xxzhx.giftBook.main.domain.bo.TGiftBookDetailsBo;
|
||||
import cn.xxzhx.giftBook.common.mybatis.core.page.TableDataInfo;
|
||||
import cn.xxzhx.giftBook.common.mybatis.core.page.PageQuery;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -68,4 +69,5 @@ public interface ITGiftBookDetailsService {
|
||||
|
||||
String importData(List<TGiftBookDetailsVo> tGiftBookDetailsVos);
|
||||
|
||||
byte[] exportPDF(TGiftBookDetailsBo bo);
|
||||
}
|
||||
|
||||
@@ -6,29 +6,46 @@ import cn.xxzhx.giftBook.common.core.exception.ServiceException;
|
||||
import cn.xxzhx.giftBook.common.core.utils.MapstructUtils;
|
||||
import cn.xxzhx.giftBook.common.core.utils.StreamUtils;
|
||||
import cn.xxzhx.giftBook.common.core.utils.StringUtils;
|
||||
import cn.xxzhx.giftBook.common.mybatis.core.page.TableDataInfo;
|
||||
import cn.xxzhx.giftBook.common.mybatis.core.page.PageQuery;
|
||||
import cn.xxzhx.giftBook.common.mybatis.core.page.TableDataInfo;
|
||||
import cn.xxzhx.giftBook.main.domain.TGiftBookDetails;
|
||||
import cn.xxzhx.giftBook.main.domain.bo.TGiftBookBo;
|
||||
import cn.xxzhx.giftBook.main.domain.bo.TGiftBookDetailsBo;
|
||||
import cn.xxzhx.giftBook.main.domain.vo.TGiftBookDetailsVo;
|
||||
import cn.xxzhx.giftBook.main.domain.vo.TGiftBookVo;
|
||||
import cn.xxzhx.giftBook.main.mapper.TGiftBookDetailsMapper;
|
||||
import cn.xxzhx.giftBook.main.service.ITGiftBookDetailsService;
|
||||
import cn.xxzhx.giftBook.main.service.ITGiftBookService;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.itextpdf.io.font.PdfEncodings;
|
||||
import com.itextpdf.io.source.ByteArrayOutputStream;
|
||||
import com.itextpdf.kernel.font.PdfFont;
|
||||
import com.itextpdf.kernel.font.PdfFontFactory;
|
||||
import com.itextpdf.kernel.geom.PageSize;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfPage;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
||||
import com.itextpdf.layout.Document;
|
||||
import com.itextpdf.layout.element.AreaBreak;
|
||||
import com.itextpdf.layout.element.Cell;
|
||||
import com.itextpdf.layout.element.Paragraph;
|
||||
import com.itextpdf.layout.element.Table;
|
||||
import com.itextpdf.layout.properties.TextAlignment;
|
||||
import com.itextpdf.layout.properties.UnitValue;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import cn.xxzhx.giftBook.main.domain.bo.TGiftBookDetailsBo;
|
||||
import cn.xxzhx.giftBook.main.domain.vo.TGiftBookDetailsVo;
|
||||
import cn.xxzhx.giftBook.main.domain.TGiftBookDetails;
|
||||
import cn.xxzhx.giftBook.main.mapper.TGiftBookDetailsMapper;
|
||||
import cn.xxzhx.giftBook.main.service.ITGiftBookDetailsService;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -81,7 +98,8 @@ public class TGiftBookDetailsServiceImpl implements ITGiftBookDetailsService {
|
||||
public List<TGiftBookDetailsVo> queryList(TGiftBookDetailsBo bo) {
|
||||
// LambdaQueryWrapper<TGiftBookDetails> lqw = buildQueryWrapper(bo);
|
||||
// return baseMapper.selectVoList(lqw);
|
||||
return baseMapper.queryList(bo);
|
||||
PageQuery pageQuery = new PageQuery(-1, 0);
|
||||
return baseMapper.queryList(bo, pageQuery);
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<TGiftBookDetails> buildQueryWrapper(TGiftBookDetailsBo bo) {
|
||||
@@ -197,4 +215,139 @@ public class TGiftBookDetailsServiceImpl implements ITGiftBookDetailsService {
|
||||
return successMsg.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] exportPDF(TGiftBookDetailsBo bo) {
|
||||
PageQuery pageQuery = new PageQuery(-1, 0);
|
||||
pageQuery.setOrderByColumn("name");
|
||||
pageQuery.setIsAsc("asc");
|
||||
List<TGiftBookDetailsVo> tGiftBookDetailsVos = baseMapper.queryList(bo, pageQuery);
|
||||
try {
|
||||
return createPdf(tGiftBookDetailsVos);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("导出PDF失败!");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] createPdf(List<TGiftBookDetailsVo> list) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
PdfWriter writer = new PdfWriter(baos);
|
||||
// 创建 PDF 文档
|
||||
PdfDocument pdfDoc = new PdfDocument(writer);
|
||||
Document document = new Document(pdfDoc, PageSize.A4, false);
|
||||
|
||||
// 设置字体
|
||||
ClassPathResource fontResource = new ClassPathResource("/fonts/MicrosoftYaHei.ttf");
|
||||
PdfFont font = PdfFontFactory.createFont(fontResource.getFile().getAbsolutePath(), PdfEncodings.IDENTITY_H);
|
||||
document.setFont(font);
|
||||
pdfDoc.addNewPage();
|
||||
|
||||
// 4. 统计每页姓氏
|
||||
Map<Integer, Set<String>> pageSurnameMap = new LinkedHashMap<>();
|
||||
|
||||
// 5. 生成数据页(先不创建目录页)
|
||||
Table table = new Table(new float[]{2, 2, 2, 2}); // 多列表格
|
||||
table.setWidth(UnitValue.createPercentValue(100)).setFont(font).setFontSize(17).setTextAlignment(TextAlignment.CENTER);
|
||||
|
||||
List<Table> tableList = new ArrayList<>();
|
||||
// 拼接
|
||||
List<String> cellList;
|
||||
BigDecimal totalAmount = new BigDecimal(0);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (i % 21 == 0 && i != 0) { // 每 22 行换页
|
||||
table.addCell(new Cell(1, 4).add(new Paragraph("总计:" + totalAmount.intValue())));
|
||||
totalAmount = new BigDecimal(0);
|
||||
tableList.add(table);
|
||||
table = new Table(new float[]{2, 2, 2, 2}); // 多列表格
|
||||
table.setWidth(UnitValue.createPercentValue(100)).setFont(font).setFontSize(17).setTextAlignment(TextAlignment.CENTER);
|
||||
}
|
||||
|
||||
TGiftBookDetailsVo tGiftBookDetailsVo = list.get(i);
|
||||
table.addCell(new Cell().add(new Paragraph(tGiftBookDetailsVo.getYear() + "年")).setWidth(100));
|
||||
table.addCell(new Cell().add(new Paragraph(tGiftBookDetailsVo.getName())).setWidth(100));
|
||||
String amount = null == tGiftBookDetailsVo.getAmount() ? "0" : String.valueOf(tGiftBookDetailsVo.getAmount());
|
||||
table.addCell(new Cell().add(new Paragraph(amount)).setWidth(100));
|
||||
totalAmount = totalAmount.add(new BigDecimal(amount));
|
||||
// 拼接
|
||||
cellList = new ArrayList<>();
|
||||
if (StringUtils.isNotBlank(tGiftBookDetailsVo.getAddress())) {
|
||||
cellList.add(tGiftBookDetailsVo.getAddress());
|
||||
}
|
||||
if (StringUtils.isNotBlank(tGiftBookDetailsVo.getGiftName())) {
|
||||
cellList.add(tGiftBookDetailsVo.getGiftName());
|
||||
}
|
||||
if (StringUtils.isNotBlank(tGiftBookDetailsVo.getEventName())) {
|
||||
cellList.add(tGiftBookDetailsVo.getEventName());
|
||||
}
|
||||
if (StringUtils.isNotBlank(tGiftBookDetailsVo.getRemark())) {
|
||||
cellList.add(tGiftBookDetailsVo.getRemark());
|
||||
}
|
||||
if (StringUtils.isNotBlank(tGiftBookDetailsVo.getIsReturn()) && "1".equals(tGiftBookDetailsVo.getIsReturn())) {
|
||||
cellList.add("已还");
|
||||
}
|
||||
|
||||
table.addCell(new Cell().add(new Paragraph(String.join("/", cellList))));
|
||||
|
||||
// 记录姓氏
|
||||
String surname = tGiftBookDetailsVo.getName().substring(0, 1);
|
||||
pageSurnameMap.computeIfAbsent((tableList.size() + 1), k -> new LinkedHashSet<>()).add(surname);
|
||||
}
|
||||
// 最后一页
|
||||
tableList.add(table);
|
||||
table.addCell(new Cell(1, 4).add(new Paragraph("总计:" + totalAmount.intValue())));
|
||||
|
||||
// 最后插入目录页
|
||||
Document directoryDoc = new Document(pdfDoc);
|
||||
|
||||
for (Map.Entry<Integer, Set<String>> entry : pageSurnameMap.entrySet()) {
|
||||
int pageNum = entry.getKey();
|
||||
String surnames = String.join("、", entry.getValue());
|
||||
directoryDoc.add(new Paragraph("第 " + pageNum + " 页 ------ " + surnames).setFont(font).setFontSize(17));
|
||||
}
|
||||
BigDecimal size = new BigDecimal(pageSurnameMap.size());
|
||||
BigDecimal section = new BigDecimal(20);
|
||||
BigDecimal bigDecimal = size.divide(section, 2, RoundingMode.HALF_UP);
|
||||
int num = 0;
|
||||
// 是否是整数
|
||||
num = bigDecimal.intValue();
|
||||
String string = bigDecimal.toString();
|
||||
String[] split = string.split("\\.");
|
||||
if (Integer.parseInt(split[1]) > 0) {
|
||||
num = bigDecimal.intValue() + 1;
|
||||
}
|
||||
for (int i = 0; i < num; i++) {
|
||||
document.add(new AreaBreak()); // 换页
|
||||
}
|
||||
|
||||
for (int i = 0; i < tableList.size(); i++) {
|
||||
Table table1 = tableList.get(i);
|
||||
document.add(table1);
|
||||
if (i != tableList.size() - 1) {
|
||||
document.add(new AreaBreak()); // 换页
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 7. 添加页码(从第二页开始)
|
||||
// 1. 先给 PDF 添加页码
|
||||
int totalPages = pdfDoc.getNumberOfPages();
|
||||
for (int i = num + 1; i <= totalPages; i++) {
|
||||
PdfPage page = pdfDoc.getPage(i);
|
||||
PdfCanvas canvas = new PdfCanvas(page);
|
||||
String pageText = "第 " + (i - num) + " 页"; // 目录是第一页,所以 -1
|
||||
float x = page.getPageSize().getWidth() / 2 - 20; // 居中
|
||||
float y = 20; // 页脚高度 20px
|
||||
|
||||
canvas.beginText()
|
||||
.setFontAndSize(font, 16)
|
||||
.moveText(x, y)
|
||||
.showText(pageText)
|
||||
.endText()
|
||||
.release();
|
||||
}
|
||||
|
||||
// 8. 关闭文档
|
||||
document.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -35,21 +35,30 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
<where>
|
||||
and tgb.del_flag = 0
|
||||
and tgbd.del_flag = 0
|
||||
<if test="null != giftBookId">
|
||||
and tgb.id = #{giftBookId}
|
||||
<if test="null != bo.giftBookId">
|
||||
and tgb.id = #{bo.giftBookId}
|
||||
</if>
|
||||
<if test="null != name">
|
||||
and tgbd.name like concat('%', #{name}, '%')
|
||||
<if test="null != bo.name">
|
||||
and tgbd.name like concat('%', #{bo.name}, '%')
|
||||
</if>
|
||||
<if test="null != eventType">
|
||||
and tgbd.event_type = #{eventType}
|
||||
<if test="null != bo.eventType">
|
||||
and tgbd.event_type = #{bo.eventType}
|
||||
</if>
|
||||
<if test="null != address">
|
||||
and tgbd.address like concat('%', #{address}, '%')
|
||||
<if test="null != bo.address">
|
||||
and tgbd.address like concat('%', #{bo.address}, '%')
|
||||
</if>
|
||||
<if test="null != eventName">
|
||||
and tgbd.event_name like concat('%', #{eventName}, '%')
|
||||
<if test="null != bo.eventName">
|
||||
and tgbd.event_name like concat('%', #{bo.eventName}, '%')
|
||||
</if>
|
||||
</where>
|
||||
order by
|
||||
<choose>
|
||||
<when test="null != pageQuery.isAsc and pageQuery.isAsc.length() > 0">
|
||||
tgbd.${pageQuery.orderByColumn} ${pageQuery.isAsc}
|
||||
</when>
|
||||
<otherwise>
|
||||
tgbd.update_time desc
|
||||
</otherwise>
|
||||
</choose>
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
Reference in New Issue
Block a user