feat: 历史文件格式转为pdf
This commit is contained in:
@@ -9,4 +9,6 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class ApiHostProperty {
|
public class ApiHostProperty {
|
||||||
private String dataTool;
|
private String dataTool;
|
||||||
|
|
||||||
|
private String stirlingPdf; // pdf 处理相关
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package mjkf.xinke.main.controller;
|
package mjkf.xinke.main.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.EasyExcel;
|
||||||
@@ -11,14 +12,15 @@ import io.swagger.annotations.ApiModelProperty;
|
|||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import mjkf.xinke.dev.modular.file.entity.DevFile;
|
|
||||||
import mjkf.xinke.dev.modular.file.service.DevFileService;
|
import mjkf.xinke.dev.modular.file.service.DevFileService;
|
||||||
import mjkf.xinke.main.common.http.FuHttpResponse;
|
import mjkf.xinke.main.common.http.FuHttpResponse;
|
||||||
import mjkf.xinke.main.constant.PricePublishStatus;
|
import mjkf.xinke.main.constant.PricePublishStatus;
|
||||||
import mjkf.xinke.main.constant.PricePublishType;
|
import mjkf.xinke.main.constant.PricePublishType;
|
||||||
|
import mjkf.xinke.main.handler.CustomSheetWriteHandler;
|
||||||
import mjkf.xinke.main.model.db.PricePublish;
|
import mjkf.xinke.main.model.db.PricePublish;
|
||||||
import mjkf.xinke.main.model.vo.PublicResponse;
|
import mjkf.xinke.main.model.vo.PublicResponse;
|
||||||
import mjkf.xinke.main.model.vo.PublicTrendResponse;
|
import mjkf.xinke.main.model.vo.PublicTrendResponse;
|
||||||
|
import mjkf.xinke.main.service.PdfService;
|
||||||
import mjkf.xinke.main.service.PricePublishService;
|
import mjkf.xinke.main.service.PricePublishService;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -29,6 +31,7 @@ import javax.annotation.Resource;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
import java.io.File;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
@@ -48,6 +51,8 @@ import java.util.stream.Collectors;
|
|||||||
public class PublicController {
|
public class PublicController {
|
||||||
@Resource
|
@Resource
|
||||||
PricePublishService pricePublishService;
|
PricePublishService pricePublishService;
|
||||||
|
@Resource
|
||||||
|
PdfService pdfService;
|
||||||
|
|
||||||
@ApiOperation("查询")
|
@ApiOperation("查询")
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
@@ -142,12 +147,18 @@ public class PublicController {
|
|||||||
query.orderByAsc(PricePublish::getMaterialId);
|
query.orderByAsc(PricePublish::getMaterialId);
|
||||||
var result = pricePublishService.list(query);
|
var result = pricePublishService.list(query);
|
||||||
result = result.stream().filter(i->ObjectUtil.isNotEmpty(i.getMaterialId())).collect(Collectors.toList());
|
result = result.stream().filter(i->ObjectUtil.isNotEmpty(i.getMaterialId())).collect(Collectors.toList());
|
||||||
|
// excel
|
||||||
String filename = URLUtil.encode("历史文件", StandardCharsets.UTF_8) + ".xlsx";
|
var tempFile = File.createTempFile("temp_history_file", ".xlsx");
|
||||||
|
EasyExcel.write(tempFile, PricePublish.class)
|
||||||
|
.registerWriteHandler(new CustomSheetWriteHandler())
|
||||||
|
.sheet("sheet1").doWrite(result);
|
||||||
|
// to pdf
|
||||||
|
var inputStream = pdfService.convertFilePdf(tempFile);
|
||||||
|
String filename = URLUtil.encode("历史文件", StandardCharsets.UTF_8) + ".pdf";
|
||||||
response.setCharacterEncoding("utf-8");
|
response.setCharacterEncoding("utf-8");
|
||||||
response.setHeader("Content-disposition", "attachment;filename=" + filename);
|
response.setHeader("Content-disposition", "attachment;filename=" + filename);
|
||||||
response.setHeader("filename", filename);
|
response.setHeader("filename", filename);
|
||||||
EasyExcel.write(response.getOutputStream(), PricePublish.class).sheet("sheet1").doWrite(result);
|
IoUtil.copy(inputStream, response.getOutputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("历史文件列表")
|
@ApiOperation("历史文件列表")
|
||||||
|
@@ -0,0 +1,24 @@
|
|||||||
|
package mjkf.xinke.main.handler;
|
||||||
|
|
||||||
|
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||||
|
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||||
|
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
||||||
|
import org.apache.poi.ss.usermodel.PrintSetup;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.util.CellRangeAddress;
|
||||||
|
|
||||||
|
public class CustomSheetWriteHandler implements SheetWriteHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
|
||||||
|
Sheet sheet = writeSheetHolder.getSheet();
|
||||||
|
// 获取打印设置
|
||||||
|
PrintSetup printSetup = sheet.getPrintSetup();
|
||||||
|
printSetup.setPaperSize(PrintSetup.A3_PAPERSIZE); // 设置页面大小为A3
|
||||||
|
printSetup.setLandscape(true); // 横向
|
||||||
|
// 设置每页都有表头
|
||||||
|
sheet.setRepeatingRows(CellRangeAddress.valueOf("1:1"));
|
||||||
|
// 设置页面居中打印
|
||||||
|
sheet.setHorizontallyCenter(true); // 水平居中
|
||||||
|
}
|
||||||
|
}
|
@@ -1,8 +1,12 @@
|
|||||||
package mjkf.xinke.main.model.db;
|
package mjkf.xinke.main.model.db;
|
||||||
|
|
||||||
import com.alibaba.excel.annotation.ExcelIgnore;
|
import com.alibaba.excel.annotation.ExcelIgnore;
|
||||||
|
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||||
import com.alibaba.excel.annotation.ExcelProperty;
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
||||||
|
import com.alibaba.excel.annotation.write.style.ContentStyle;
|
||||||
|
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
|
||||||
|
import com.alibaba.excel.enums.poi.BorderStyleEnum;
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
@@ -19,6 +23,7 @@ import lombok.Getter;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import mjkf.xinke.auth.core.pojo.SaBaseLoginUser;
|
import mjkf.xinke.auth.core.pojo.SaBaseLoginUser;
|
||||||
import mjkf.xinke.main.model.vo.PricePublishEditRequest;
|
import mjkf.xinke.main.model.vo.PricePublishEditRequest;
|
||||||
|
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -31,6 +36,10 @@ import mjkf.xinke.main.model.vo.PricePublishEditRequest;
|
|||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@TableName("PRICE_PUBLISH")
|
@TableName("PRICE_PUBLISH")
|
||||||
|
@ColumnWidth(8)
|
||||||
|
@HeadRowHeight(40)
|
||||||
|
@ContentStyle(borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
|
||||||
|
@ExcelIgnoreUnannotated
|
||||||
@ApiModel(value = "PricePublish对象", description = "发布价格")
|
@ApiModel(value = "PricePublish对象", description = "发布价格")
|
||||||
public class PricePublish extends Model<PricePublish> {
|
public class PricePublish extends Model<PricePublish> {
|
||||||
|
|
||||||
@@ -38,22 +47,18 @@ public class PricePublish extends Model<PricePublish> {
|
|||||||
|
|
||||||
@ApiModelProperty("最后更新人id")
|
@ApiModelProperty("最后更新人id")
|
||||||
@TableField("UPDATE_USER_ID")
|
@TableField("UPDATE_USER_ID")
|
||||||
@ExcelIgnore
|
|
||||||
private String updateUserId;
|
private String updateUserId;
|
||||||
|
|
||||||
@ApiModelProperty("最后更新人名称")
|
@ApiModelProperty("最后更新人名称")
|
||||||
@TableField("UPDATE_USER_NAME")
|
@TableField("UPDATE_USER_NAME")
|
||||||
@ExcelIgnore
|
|
||||||
private String updateUserName;
|
private String updateUserName;
|
||||||
|
|
||||||
@ApiModelProperty("最后更新时间")
|
@ApiModelProperty("最后更新时间")
|
||||||
@TableField("UPDATE_TIME")
|
@TableField("UPDATE_TIME")
|
||||||
@ExcelIgnore
|
|
||||||
private LocalDateTime updateTime;
|
private LocalDateTime updateTime;
|
||||||
|
|
||||||
@ApiModelProperty("创建人id")
|
@ApiModelProperty("创建人id")
|
||||||
@TableField("CREATE_USER_ID")
|
@TableField("CREATE_USER_ID")
|
||||||
@ExcelIgnore
|
|
||||||
private String createUserId;
|
private String createUserId;
|
||||||
|
|
||||||
@ApiModelProperty("创建人名称")
|
@ApiModelProperty("创建人名称")
|
||||||
@@ -63,59 +68,54 @@ public class PricePublish extends Model<PricePublish> {
|
|||||||
|
|
||||||
@ApiModelProperty("创建时间")
|
@ApiModelProperty("创建时间")
|
||||||
@TableField("CREATE_TIME")
|
@TableField("CREATE_TIME")
|
||||||
@ExcelIgnore
|
|
||||||
private LocalDateTime createTime;
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
@ApiModelProperty("删除人id")
|
@ApiModelProperty("删除人id")
|
||||||
@TableField("DELETE_USER_ID")
|
@TableField("DELETE_USER_ID")
|
||||||
@ExcelIgnore
|
|
||||||
private String deleteUserId;
|
private String deleteUserId;
|
||||||
|
|
||||||
@ApiModelProperty("删除人名称")
|
@ApiModelProperty("删除人名称")
|
||||||
@TableField("DELETE_USER_NAME")
|
@TableField("DELETE_USER_NAME")
|
||||||
@ExcelIgnore
|
|
||||||
private String deleteUserName;
|
private String deleteUserName;
|
||||||
|
|
||||||
@ApiModelProperty("删除时间")
|
@ApiModelProperty("删除时间")
|
||||||
@TableField("DELETE_TIME")
|
@TableField("DELETE_TIME")
|
||||||
@ExcelIgnore
|
|
||||||
private LocalDateTime deleteTime;
|
private LocalDateTime deleteTime;
|
||||||
|
|
||||||
@TableId(value = "ID", type = IdType.AUTO)
|
@TableId(value = "ID", type = IdType.AUTO)
|
||||||
@ExcelIgnore
|
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
@ApiModelProperty("编号")
|
@ApiModelProperty("编号")
|
||||||
@TableField("MATERIAL_ID")
|
@TableField("MATERIAL_ID")
|
||||||
@ExcelProperty({"材料代码"})
|
@ExcelProperty({"材料代码"})
|
||||||
@ColumnWidth(15)
|
@ColumnWidth(15)
|
||||||
|
// @ContentStyle(borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
|
||||||
private String materialId;
|
private String materialId;
|
||||||
|
|
||||||
@ApiModelProperty("年份")
|
@ApiModelProperty("年份")
|
||||||
@TableField("YEAR")
|
@TableField("YEAR")
|
||||||
@ExcelIgnore
|
|
||||||
private Integer year;
|
private Integer year;
|
||||||
|
|
||||||
@ApiModelProperty("月份")
|
@ApiModelProperty("月份")
|
||||||
@TableField("MONTH")
|
@TableField("MONTH")
|
||||||
@ExcelIgnore
|
|
||||||
private Integer month;
|
private Integer month;
|
||||||
|
|
||||||
@ApiModelProperty("材料名称")
|
@ApiModelProperty("材料名称")
|
||||||
@TableField("`NAME`")
|
@TableField("`NAME`")
|
||||||
@ExcelProperty({"材料名称"})
|
@ExcelProperty({"材料名称"})
|
||||||
@ColumnWidth(20)
|
@ColumnWidth(20)
|
||||||
|
// @ContentStyle(borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@ApiModelProperty("规格")
|
@ApiModelProperty("规格")
|
||||||
@TableField("`SPEC`")
|
@TableField("`SPEC`")
|
||||||
@ExcelProperty({"规格"})
|
@ExcelProperty({"规格"})
|
||||||
@ColumnWidth(35)
|
@ColumnWidth(34)
|
||||||
|
// @ContentStyle(borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
|
||||||
private String spec;
|
private String spec;
|
||||||
|
|
||||||
@ApiModelProperty("价格")
|
@ApiModelProperty("价格")
|
||||||
@TableField("PRICE")
|
@TableField("PRICE")
|
||||||
@ExcelIgnore
|
|
||||||
private BigDecimal price;
|
private BigDecimal price;
|
||||||
|
|
||||||
@ApiModelProperty("福州价格")
|
@ApiModelProperty("福州价格")
|
||||||
@@ -171,30 +171,29 @@ public class PricePublish extends Model<PricePublish> {
|
|||||||
@ApiModelProperty("漳州开发区价格")
|
@ApiModelProperty("漳州开发区价格")
|
||||||
@TableField("PRICE_ZHANGZHOUKFQ")
|
@TableField("PRICE_ZHANGZHOUKFQ")
|
||||||
@ExcelProperty({"漳州开发区"})
|
@ExcelProperty({"漳州开发区"})
|
||||||
|
// @ContentStyle(borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
|
||||||
private BigDecimal priceZhangzhouKfq;
|
private BigDecimal priceZhangzhouKfq;
|
||||||
|
|
||||||
@ApiModelProperty("税率")
|
@ApiModelProperty("税率")
|
||||||
@TableField("TAX")
|
@TableField("TAX")
|
||||||
@ExcelIgnore
|
|
||||||
private BigDecimal tax;
|
private BigDecimal tax;
|
||||||
|
|
||||||
@ApiModelProperty("状态")
|
@ApiModelProperty("状态")
|
||||||
@TableField("STATUS")
|
@TableField("STATUS")
|
||||||
@ExcelIgnore
|
|
||||||
private Integer status;
|
private Integer status;
|
||||||
|
|
||||||
@ApiModelProperty("类型")
|
@ApiModelProperty("类型")
|
||||||
@TableField("TYPE")
|
@TableField("TYPE")
|
||||||
@ExcelIgnore
|
|
||||||
private Integer type;
|
private Integer type;
|
||||||
|
|
||||||
@ApiModelProperty("单位")
|
@ApiModelProperty("单位")
|
||||||
@TableField("UNIT")
|
@TableField("UNIT")
|
||||||
|
@ColumnWidth(5)
|
||||||
|
// @ContentStyle(borderLeft = BorderStyleEnum.THIN, borderRight = BorderStyleEnum.THIN, borderTop = BorderStyleEnum.THIN, borderBottom = BorderStyleEnum.THIN)
|
||||||
@ExcelProperty(value={"单位"}, index=3)
|
@ExcelProperty(value={"单位"}, index=3)
|
||||||
private String unit;
|
private String unit;
|
||||||
|
|
||||||
@ApiModelProperty("接壤城市数据")
|
@ApiModelProperty("接壤城市数据")
|
||||||
@ExcelIgnore
|
|
||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private DataAdjacent adjacent;
|
private DataAdjacent adjacent;
|
||||||
|
|
||||||
|
56
src/main/java/mjkf/xinke/main/service/PdfService.java
Normal file
56
src/main/java/mjkf/xinke/main/service/PdfService.java
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package mjkf.xinke.main.service;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import cn.hutool.http.HttpResponse;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import mjkf.xinke.common.cache.CommonCacheOperator;
|
||||||
|
import mjkf.xinke.common.exception.CommonException;
|
||||||
|
import mjkf.xinke.main.common.property.ApiHostProperty;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class PdfService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
ApiHostProperty apiHostProperty;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
CommonCacheOperator commonCacheOperator;
|
||||||
|
|
||||||
|
static private Integer TIMEOUT = 60000; //超时,毫秒
|
||||||
|
static private String SUCCESS_CODE = "200";
|
||||||
|
static private String CAMERA_ACCESSTOEKN_CACHE_KEY = "CAMERA_ACCESSTOEKN_CACHE_KEY:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换文件为pdf
|
||||||
|
*/
|
||||||
|
public InputStream convertFilePdf(File file) {
|
||||||
|
var uri = "api/v1/convert/file/pdf";
|
||||||
|
var url = MessageFormat.format("{0}/{1}", apiHostProperty.getStirlingPdf(), uri);
|
||||||
|
Map<String, Object> paramMap = Map.of(
|
||||||
|
"fileInput", file
|
||||||
|
);
|
||||||
|
HttpResponse response = HttpRequest.post(url)
|
||||||
|
.form(paramMap)
|
||||||
|
.timeout(TIMEOUT)
|
||||||
|
.execute();
|
||||||
|
if (response.isOk()) {
|
||||||
|
return response.bodyStream();
|
||||||
|
} else {
|
||||||
|
log.error("PdfService.convertFilePdf Failed : " + response.body());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
26
src/test/java/mjkf/xinke/PdfTest.java
Normal file
26
src/test/java/mjkf/xinke/PdfTest.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
package mjkf.xinke;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import mjkf.xinke.main.service.PdfService;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
|
||||||
|
@SpringBootTest(classes = Application.class)
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
public class PdfTest {
|
||||||
|
@Resource
|
||||||
|
PdfService pdfService;
|
||||||
|
|
||||||
|
@org.junit.Test
|
||||||
|
public void test() throws Exception {
|
||||||
|
var inputStream = pdfService.convertFilePdf(new File("C:\\Users\\Administrator\\Documents\\%E5%8E%86%E5%8F%B2%E6%96%87%E4%BB%B6.xlsx"));
|
||||||
|
FileUtil.writeFromStream(inputStream, new File("test.pdf"));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user