Commit 2f01b626 authored by xixinzhao's avatar xixinzhao

交接班记录pdf下载

parent 0653b820
...@@ -47,6 +47,18 @@ ...@@ -47,6 +47,18 @@
<artifactId>easypoi-annotation</artifactId> <artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version> <version>3.0.3</version>
</dependency> </dependency>
<!-- itext pdf相关 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- itext asian 字体相关 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
package com.yeejoin.amos.boot.biz.common.utils;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import javax.servlet.http.HttpServletResponse;
/**
* PDF工具类
*/
public class PdfUtils {
private static int maxWidth = 520;
public static Document createFile(HttpServletResponse response) { // 设置页面大小,联系文件
Document document=new Document();
document.setPageSize(PageSize.A4);
document.setPageCount(3);
try {
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
} catch (Exception e) {
e.printStackTrace();
}
return document;
}
/**
*
* @param value 单元格数据
* @param font 单元格样式
* @param align 位置
* @param colspan 占几列
* @param row 占几行
* @param size 高度
* @return
*/
public static PdfPCell createCell(String value, Font font, int align, int colspan, int row, int size){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value,font));
cell.setFixedHeight(size);
if (row != 1) {
cell.setRowspan(row);
}
return cell;
}
public static PdfPCell createCell(Image image, int align, int colspan){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setImage(image);
cell.setPadding(3.0f);
cell.setBorderWidthLeft(0);
cell.setBorderWidthRight(0);
cell.setBorderWidthTop(0);
cell.setBorderWidthBottom(1);
return cell;
}
// 创建一个几列的Table
public static PdfPTable createTable(int colNumber){
PdfPTable table = new PdfPTable(colNumber);
try{
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
}
...@@ -2,7 +2,10 @@ package com.yeejoin.amos.boot.module.jcs.api.service; ...@@ -2,7 +2,10 @@ package com.yeejoin.amos.boot.module.jcs.api.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itextpdf.text.DocumentException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -59,4 +62,9 @@ public interface IShiftChangeService { ...@@ -59,4 +62,9 @@ public interface IShiftChangeService {
* @return Map<String, Object> * @return Map<String, Object>
*/ */
Map<String, Object> lastRecord(); Map<String, Object> lastRecord();
/**
* 根据交接班记录id生成pdf
*/
void exportPdfById(HttpServletResponse response, Long shiftChangeId) throws IOException, DocumentException;
} }
package com.yeejoin.amos.boot.module.jcs.biz.controller; package com.yeejoin.amos.boot.module.jcs.biz.controller;
import com.itextpdf.text.DocumentException;
import com.yeejoin.amos.boot.biz.common.controller.BaseController; import com.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.module.jcs.api.dto.ShiftChangeInfoDto; import com.yeejoin.amos.boot.module.jcs.api.dto.ShiftChangeInfoDto;
import com.yeejoin.amos.boot.module.jcs.api.service.IShiftChangeService; import com.yeejoin.amos.boot.module.jcs.api.service.IShiftChangeService;
...@@ -14,6 +15,7 @@ import org.typroject.tyboot.core.restful.utils.ResponseHelper; ...@@ -14,6 +15,7 @@ import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel; import org.typroject.tyboot.core.restful.utils.ResponseModel;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** /**
...@@ -100,8 +102,13 @@ public class ShiftChangeController extends BaseController { ...@@ -100,8 +102,13 @@ public class ShiftChangeController extends BaseController {
@TycloudOperation(ApiLevel = UserType.AGENCY) @TycloudOperation(ApiLevel = UserType.AGENCY)
@ApiOperation(httpMethod = "GET", value = "pdf下载", notes = "pdf下载") @ApiOperation(httpMethod = "GET", value = "pdf下载", notes = "pdf下载")
@GetMapping("/pdf/export") @GetMapping("/pdf/export")
public void exportPdf(HttpServletResponse response,@RequestParam Long shiftChangeId){ public void exportPdf(HttpServletResponse response,@RequestParam Long shiftChangeId) {
try {
iShiftChangeService.exportPdfById(response, shiftChangeId);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("系统异常!");
}
} }
@TycloudOperation(ApiLevel = UserType.AGENCY) @TycloudOperation(ApiLevel = UserType.AGENCY)
......
...@@ -4,9 +4,14 @@ import com.alibaba.fastjson.JSONObject; ...@@ -4,9 +4,14 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Sequence; import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.yeejoin.amos.boot.biz.common.utils.PdfUtils;
import com.yeejoin.amos.boot.module.common.api.entity.DynamicFormInstance; import com.yeejoin.amos.boot.module.common.api.entity.DynamicFormInstance;
import com.yeejoin.amos.boot.module.common.biz.service.impl.DynamicFormInstanceServiceImpl; import com.yeejoin.amos.boot.module.common.biz.service.impl.DynamicFormInstanceServiceImpl;
import com.yeejoin.amos.boot.module.jcs.api.dto.ShiftChangeDto; import com.yeejoin.amos.boot.module.jcs.api.dto.ShiftChangeDto;
import com.yeejoin.amos.boot.module.jcs.api.dto.ShiftChangeInfoDto;
import com.yeejoin.amos.boot.module.jcs.api.entity.ShiftChange; import com.yeejoin.amos.boot.module.jcs.api.entity.ShiftChange;
import com.yeejoin.amos.boot.module.jcs.api.mapper.ShiftChangeMapper; import com.yeejoin.amos.boot.module.jcs.api.mapper.ShiftChangeMapper;
import com.yeejoin.amos.boot.module.jcs.api.service.IShiftChangeService; import com.yeejoin.amos.boot.module.jcs.api.service.IShiftChangeService;
...@@ -17,6 +22,8 @@ import org.typroject.tyboot.core.foundation.utils.Bean; ...@@ -17,6 +22,8 @@ import org.typroject.tyboot.core.foundation.utils.Bean;
import org.typroject.tyboot.core.rdbms.service.BaseService; import org.typroject.tyboot.core.rdbms.service.BaseService;
import org.typroject.tyboot.core.restful.exception.instance.BadRequest; import org.typroject.tyboot.core.restful.exception.instance.BadRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -110,4 +117,89 @@ public class ShiftChangeServiceImpl extends BaseService<ShiftChangeDto, ShiftCha ...@@ -110,4 +117,89 @@ public class ShiftChangeServiceImpl extends BaseService<ShiftChangeDto, ShiftCha
map = list.isEmpty() ? map : list.get(0); map = list.isEmpty() ? map : list.get(0);
return map; return map;
} }
@Override
public void exportPdfById(HttpServletResponse response, Long shiftChangeId) throws IOException, DocumentException {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/pdf");
response.setContentType("application/pdf;charset=UTF-8");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=交接班记录表.pdf");
// 创建document对象
Document document = PdfUtils.createFile(response);
// 获取pdf文件数据
ShiftChangeDto shiftChange = this.queryBySeq(shiftChangeId);
Map<String, Object> baseInfo = dynamicFormInstanceService.getOneMap(shiftChange.getInstanceId(), GROUP_CODE);
ShiftChangeInfoDto ShiftChangeInfoDto = (ShiftChangeInfoDto) Bean.mapToBean(baseInfo, ShiftChangeInfoDto.class);
// 创建pdf格式
createPDFReport(document,ShiftChangeInfoDto,shiftChange);
}
public void createPDFReport(Document document,ShiftChangeInfoDto shiftChangeInfoDto,ShiftChangeDto shiftChange) throws IOException, DocumentException {
document.open();
// 基本样式参数
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font cellContent = new Font(bfChinese, 11, Font.NORMAL);
Font cellTitle = new Font(bfChinese, 11, Font.BOLD);
Font title = new Font(bfChinese, 20, Font.BOLD);
int size = 35;
// 表头
Paragraph docTitle = new Paragraph("119接警坐席交接班记录\n", title);
docTitle.setAlignment(Element.ALIGN_CENTER);
// docTitle.setSpacingBefore(20);
// 表格内容
PdfPTable table1 = PdfUtils.createTable(8);
table1.setWidthPercentage(100);
table1.setSpacingBefore(10);
// 第一行
table1.addCell(PdfUtils.createCell("时间:", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChange.getRecDate().toString(), cellContent,Element.ALIGN_CENTER,3,1,size));
table1.addCell(PdfUtils.createCell("值班领导:", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getJsDutyLeader(), cellContent,Element.ALIGN_CENTER,3,1,size));
// 第二行
table1.addCell(PdfUtils.createCell("值班员", cellTitle,Element.ALIGN_CENTER,1,3,size));
table1.addCell(PdfUtils.createCell("正班", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getDutyPersonOne(), cellContent,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell("值班开始时间", cellContent,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell("交接班时间", cellContent,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell("接班员", cellTitle,Element.ALIGN_CENTER,1,3,size));
table1.addCell(PdfUtils.createCell("正班", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getSucceedPersonOne(), cellContent,Element.ALIGN_CENTER,1,1,size));
// 第三行
table1.addCell(PdfUtils.createCell("副班", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getDutyPersonTwo(), cellContent,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getBeginDate(), cellContent,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getEndDate(), cellContent,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell("副班", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getSucceedPersonTwo(), cellContent,Element.ALIGN_CENTER,1,1,size));
// 第四行
table1.addCell(PdfUtils.createCell("经理", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getDutyPersonLeader(), cellContent,Element.ALIGN_CENTER,3,1,size));
table1.addCell(PdfUtils.createCell("经理", cellTitle,Element.ALIGN_CENTER,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChangeInfoDto.getSucceedPersonLeader(), cellContent,Element.ALIGN_CENTER,1,1,size));
// 值班情况
table1.addCell(PdfUtils.createCell("值班情况", cellTitle,Element.ALIGN_LEFT,1,4,size));
table1.addCell(PdfUtils.createCell("接警情况:", cellContent,Element.ALIGN_LEFT,7,1,size));
table1.addCell(PdfUtils.createCell(shiftChange.getDutyJson().toString(), cellContent,Element.ALIGN_LEFT,7,1,120));
table1.addCell(PdfUtils.createCell("力量出动:", cellContent,Element.ALIGN_LEFT,7,1,size));
table1.addCell(PdfUtils.createCell(shiftChange.getPowerJson().toString(), cellContent,Element.ALIGN_LEFT,7,1,120));
// 移交事宜
table1.addCell(PdfUtils.createCell("移交事宜", cellTitle,Element.ALIGN_LEFT,1,1,size));
table1.addCell(PdfUtils.createCell(shiftChange.getRemark(), cellContent,Element.ALIGN_LEFT,7,1,120));
try{
document.add(docTitle);
document.add(table1);;
}catch (DocumentException E){
throw new RuntimeException("PDF写入文件报错");
}
document.close();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment