|
|
|
@ -1,11 +1,36 @@
|
|
|
|
|
package com.ruoyi.gysl.service.impl;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
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.pdf.PdfDocument;
|
|
|
|
|
import com.itextpdf.kernel.pdf.PdfWriter;
|
|
|
|
|
import com.itextpdf.layout.Document;
|
|
|
|
|
import com.itextpdf.layout.element.*;
|
|
|
|
|
import com.itextpdf.layout.properties.AreaBreakType;
|
|
|
|
|
import com.itextpdf.layout.properties.HorizontalAlignment;
|
|
|
|
|
import com.itextpdf.layout.properties.TextAlignment;
|
|
|
|
|
import com.itextpdf.layout.properties.UnitValue;
|
|
|
|
|
import com.ruoyi.common.config.RuoYiConfig;
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
|
import com.ruoyi.common.utils.file.FileUtils;
|
|
|
|
|
import com.ruoyi.gysl.entity.GyslProjectHandbook;
|
|
|
|
|
import com.ruoyi.gysl.entity.response.BasicInformationResponse;
|
|
|
|
|
import com.ruoyi.gysl.mapper.BasicInformationMapper;
|
|
|
|
|
import com.ruoyi.gysl.mapper.ProjectHandBookMapper;
|
|
|
|
|
import com.ruoyi.gysl.service.GyslProjectHandbookService;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 项目手册(GyslProjectHandbook)表服务实现类
|
|
|
|
|
*
|
|
|
|
@ -15,5 +40,167 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
@Service("gyslProjectHandbookService")
|
|
|
|
|
public class GyslProjectHandbookServiceImpl extends ServiceImpl<ProjectHandBookMapper, GyslProjectHandbook> implements GyslProjectHandbookService {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private BasicInformationMapper basicInformationMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成pdf
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void generatePdfs(HttpServletResponse response, Integer id) throws FileNotFoundException, MalformedURLException {
|
|
|
|
|
//获取当前手册信息
|
|
|
|
|
GyslProjectHandbook byId = getById(id);
|
|
|
|
|
//获取手册对应项目信息
|
|
|
|
|
java.util.List<BasicInformationResponse> basicInfoList = basicInformationMapper.idListToProject
|
|
|
|
|
(Arrays.asList(byId.getXmId().split(",")));
|
|
|
|
|
// 设置响应头
|
|
|
|
|
response.setContentType("application/pdf");
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=\"product_catalog.pdf\"");
|
|
|
|
|
try (
|
|
|
|
|
// 直接绑定到 response 的输出流(不再生成本地文件)
|
|
|
|
|
PdfDocument pdf = new PdfDocument(new PdfWriter(response.getOutputStream()));
|
|
|
|
|
Document document = new Document(pdf)
|
|
|
|
|
) {
|
|
|
|
|
addCoverPage(document, byId);
|
|
|
|
|
addTableOfContents(document, basicInfoList);
|
|
|
|
|
addProductPages(document, basicInfoList);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
response.reset(); // 清除已写入的内容
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
|
|
|
throw new RuntimeException("PDF生成失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 封面页设计
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addCoverPage(Document document, GyslProjectHandbook gyslProjectHandbook) throws MalformedURLException {
|
|
|
|
|
//添加手册名称
|
|
|
|
|
Paragraph coverTitle = new Paragraph(gyslProjectHandbook.getName())
|
|
|
|
|
.setFontSize(30)
|
|
|
|
|
.setBold()
|
|
|
|
|
.setFontColor(ColorConstants.BLUE)
|
|
|
|
|
.setTextAlignment(TextAlignment.CENTER)
|
|
|
|
|
.setMarginTop(200); // 垂直居中
|
|
|
|
|
document.add(coverTitle);
|
|
|
|
|
//添加副标题
|
|
|
|
|
Paragraph ft = new Paragraph(gyslProjectHandbook.getSubtitle())
|
|
|
|
|
.setFontSize(22)
|
|
|
|
|
.setFontColor(ColorConstants.BLUE)
|
|
|
|
|
.setTextAlignment(TextAlignment.RIGHT)
|
|
|
|
|
.setMarginTop(300); // 垂直居中
|
|
|
|
|
document.add(ft);
|
|
|
|
|
//添加封面图片
|
|
|
|
|
try {
|
|
|
|
|
if (!FileUtils.checkAllowDownload(gyslProjectHandbook.getCoverImg())) {
|
|
|
|
|
throw new ServiceException("资源文件({})非法! ");
|
|
|
|
|
}
|
|
|
|
|
// 本地资源路径
|
|
|
|
|
String localPath = RuoYiConfig.getProfile();
|
|
|
|
|
// 数据库资源地址
|
|
|
|
|
String downloadPath = localPath + StringUtils.substringAfter(gyslProjectHandbook.getCoverImg(), Constants.RESOURCE_PREFIX);
|
|
|
|
|
ImageData imageData = ImageDataFactory.create(downloadPath);
|
|
|
|
|
Image image = new Image(imageData).setWidth(300).setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
document.add(image);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new ServiceException("下载文件失败! ");
|
|
|
|
|
}
|
|
|
|
|
document.add(new AreaBreak()); // 强制分页
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 目录页设计
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addTableOfContents(Document document, java.util.List<BasicInformationResponse> basicInfoList) {
|
|
|
|
|
Paragraph tocTitle = new Paragraph("目录")
|
|
|
|
|
.setFontSize(20)
|
|
|
|
|
.setBold()
|
|
|
|
|
.setTextAlignment(TextAlignment.CENTER);
|
|
|
|
|
document.add(tocTitle);
|
|
|
|
|
List tocList = new List()
|
|
|
|
|
.setSymbolIndent(20);
|
|
|
|
|
basicInfoList.forEach(x -> tocList.add(x.getBasicInformation().getName()));
|
|
|
|
|
document.add(tocList);
|
|
|
|
|
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 内容页设计
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addProductPages(Document document, java.util.List<BasicInformationResponse> basicInfoList) throws MalformedURLException {
|
|
|
|
|
basicInfoList.forEach(x -> {
|
|
|
|
|
Paragraph chapter1 = new Paragraph(x.getBasicInformation().getName())
|
|
|
|
|
.setFontSize(18)
|
|
|
|
|
.setBold();
|
|
|
|
|
document.add(chapter1);
|
|
|
|
|
// 1. 添加园区信息表格
|
|
|
|
|
Table parkTable = new Table(UnitValue.createPercentArray(new float[]{30, 70}))
|
|
|
|
|
.setWidth(UnitValue.createPercentValue(100))
|
|
|
|
|
.setMarginBottom(20)
|
|
|
|
|
.setKeepTogether(true); // 保持表格在同一页;
|
|
|
|
|
// 园区信息标题(蓝色背景)
|
|
|
|
|
Cell cell = new Cell(1, 2)
|
|
|
|
|
.setBackgroundColor(new DeviceRgb(33, 150, 243))
|
|
|
|
|
.add(new Paragraph("园区信息")
|
|
|
|
|
.setFontColor(DeviceRgb.WHITE)
|
|
|
|
|
.setFontSize(16)
|
|
|
|
|
.setBold())
|
|
|
|
|
.setTextAlignment(TextAlignment.CENTER)
|
|
|
|
|
.setPadding(8);
|
|
|
|
|
parkTable.addCell(cell);
|
|
|
|
|
// 添加园区信息行
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("总投资额")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("项目标签")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("项目标签")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("2.87万m²")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("现状分类")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("毛坯")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("重点发展产业")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("70个")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("建设模式")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("宿舍78间")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("所属功能区")).setPadding(5));
|
|
|
|
|
parkTable.addCell(new Cell().add(new Paragraph("A栋18900m²,B栋5500m²整租价格可议")).setPadding(5));
|
|
|
|
|
document.add(parkTable);
|
|
|
|
|
// 2. 添加厂房信息表格
|
|
|
|
|
Table factoryTable = new Table(UnitValue.createPercentArray(new float[]{25, 35, 35}))
|
|
|
|
|
.setWidth(UnitValue.createPercentValue(100)).setKeepTogether(true); // 保持表格在同一页;
|
|
|
|
|
// 厂房信息标题(蓝色背景)
|
|
|
|
|
Cell cell2 = new Cell(1, 2)
|
|
|
|
|
.setBackgroundColor(new DeviceRgb(33, 150, 243))
|
|
|
|
|
.add(new Paragraph("厂房信息")
|
|
|
|
|
.setFontColor(DeviceRgb.WHITE)
|
|
|
|
|
.setFontSize(16)
|
|
|
|
|
.setBold())
|
|
|
|
|
.setTextAlignment(TextAlignment.CENTER)
|
|
|
|
|
.setPadding(8);
|
|
|
|
|
factoryTable.addCell(cell2);
|
|
|
|
|
// 子标题(浅灰色背景)
|
|
|
|
|
Cell z1 = new Cell()
|
|
|
|
|
.setBackgroundColor(new DeviceRgb(245, 245, 245))
|
|
|
|
|
.add(new Paragraph("A栋具体参数").setBold())
|
|
|
|
|
.setTextAlignment(TextAlignment.CENTER)
|
|
|
|
|
.setPadding(5);
|
|
|
|
|
factoryTable.addCell(z1);
|
|
|
|
|
Cell z2 = new Cell()
|
|
|
|
|
.setBackgroundColor(new DeviceRgb(245, 245, 245))
|
|
|
|
|
.add(new Paragraph("B栋具体参数").setBold())
|
|
|
|
|
.setTextAlignment(TextAlignment.CENTER)
|
|
|
|
|
.setPadding(5);
|
|
|
|
|
factoryTable.addCell(z2);
|
|
|
|
|
// 添加参数行
|
|
|
|
|
factoryTable.addCell(new Cell().add(new Paragraph("租金")).setPadding(5));
|
|
|
|
|
factoryTable.addCell(new Cell().add(new Paragraph("首层35元/m²,2-3层30元/m²,4-9层25元/m²")).setPadding(5));
|
|
|
|
|
factoryTable.addCell(new Cell().add(new Paragraph("首层32元/m²,2-3层26元/m²,4-5层25元/m²")).setPadding(5));
|
|
|
|
|
factoryTable.addCell(new Cell().add(new Paragraph("租金")).setPadding(5));
|
|
|
|
|
factoryTable.addCell(new Cell().add(new Paragraph("首层35元/m²,2-3层30元/m²,4-9层25元/m²")).setPadding(5));
|
|
|
|
|
factoryTable.addCell(new Cell().add(new Paragraph("首层32元/m²,2-3层26元/m²,4-5层25元/m²")).setPadding(5));
|
|
|
|
|
document.add(factoryTable);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|