Commit 0ed84579 authored by 盖献康's avatar 盖献康

PDF - 检验报告模板 (一半)、通过角色名查询用户信息接口

parent d88f7418
package com.ruoyi.domain.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 检验报告PDF VO类
* @author gxk
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class InspectionReportPdfVO {
/**
* 报告编号
*/
private String reportNumber;
/**
* 产品名称
*/
private String productName;
/**
* 产品型号
*/
private String productModel;
/**
* 委托单位
*/
private String entrustedUnit;
/**
* 检验中心名称
*/
private String inspectionCenterName;
/**
* 检验中心地址
*/
private String inspectionCenterAddress;
/**
* 检验中心电话
*/
private String inspectionCenterTelephone;
/**
* 检验中心邮编
*/
private String inspectionCenterPostcode;
/**
* 委托单位地址
*/
private String clientAddress;
/**
* 委托单位电话
*/
private String entrustedUnitTelephone;
/**
* 委托单位邮编
*/
private String clientPostalCode;
/**
* 样品名称
*/
private String sampleName;
/**
* 型号规格
*/
private String typeSpecification;
/**
* 生产企业
*/
private String productionEnterprise;
/**
* 送样者
*/
private String sampleSender;
/**
* 送样日期
*/
private String sampleDeliveryDate;
/**
* 样品数量
*/
private String sampleQuantity;
/**
* 生产日期
*/
private String productionDate;
/**
* 检验项目
*/
private String inspectionItem;
/**
* 检验结论
*/
private String testConclusion;
/**
* 签发日期
*/
private String issueDate;
/**
* 备注
*/
private String remark;
/**
* 任务编号
*/
private String taskNumber;
/**
*
*/
private String sampleSum;
/**
* 车辆类型
*/
private String vehicleType;
/**
* 车辆识别码
*/
private String vehicleIdentificationNumber;
/**
* 样品编号汇总
*/
private String sampleNumberSummary;
}
package com.ruoyi.service;
import javax.servlet.http.HttpServletResponse;
/**
* PDF模板管理Service接口
* @author gxk
*/
public interface PdfTemplateManagementService {
/**
* 检验报告PDF下载
* @param response
* @throws Exception
*/
void generateInspectionReport(HttpServletResponse response) throws Exception;
}
......@@ -270,60 +270,60 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
@Override
public void generateTempPDF(HttpServletResponse response) throws Exception {
PdfReader reader = null;
PdfStamper ps = null;
OutputStream fos = null;
ByteArrayOutputStream bos = null;
Base64.InputStream fin = null;
ServletOutputStream out = null;
try {
// 模板绝对路径--服务器
String fileName = "/template/receipt_template.pdf";
// 读取现有模板内容
reader = new PdfReader(fileName);
// 创建输出流
bos = new ByteArrayOutputStream();
// 实例化PdfStamper准备编辑pdf内容
ps = new PdfStamper(reader, bos);
// 获取表单所有元素
AcroFields fields = ps.getAcroFields();
// 设置具体字体格式的编码, 不设置的话中文可能不会显示
BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
fields.addSubstitutionFont(bf);
// 动态添加所需要的数据,key跟模板中对应文本域名称一致
Map<String, String> map = new HashMap<>();
map.put("code", "1234567890fjdksjfdsfdsffdsjfdssssssttttttttttttttt");
map.put("table_image", "C:/Users/gxk/Pictures/Saved Pictures/nvm.png");
// 渲染
fillData(fields, map, ps);
//必须要调用这个,否则文档不会生成的
ps.setFormFlattening(true);
if(ps != null){
ps.close();
}
//生成pdf路径存放的路径
fos = response.getOutputStream();
fos.write(bos.toByteArray());
}catch (Exception e){
e.printStackTrace();
}finally {
if(fos!=null){
fos.flush();
fos.close();
}
if (bos != null){
bos.close();
}
if(reader != null){
reader.close();
}
}
// PdfReader reader = null;
// PdfStamper ps = null;
// OutputStream fos = null;
// ByteArrayOutputStream bos = null;
// Base64.InputStream fin = null;
// ServletOutputStream out = null;
// try {
// // 模板绝对路径--服务器
// String fileName = "/template/receipt_template.pdf";
// // 读取现有模板内容
// reader = new PdfReader(fileName);
// // 创建输出流
// bos = new ByteArrayOutputStream();
// // 实例化PdfStamper准备编辑pdf内容
// ps = new PdfStamper(reader, bos);
//
// // 获取表单所有元素
// AcroFields fields = ps.getAcroFields();
//
// // 设置具体字体格式的编码, 不设置的话中文可能不会显示
// BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// fields.addSubstitutionFont(bf);
//
// // 动态添加所需要的数据,key跟模板中对应文本域名称一致
// Map<String, String> map = new HashMap<>();
// map.put("code", "1234567890fjdksjfdsfdsffdsjfdssssssttttttttttttttt");
// map.put("table_image", "C:/Users/gxk/Pictures/Saved Pictures/nvm.png");
//
// // 渲染
// fillData(fields, map, ps);
//
// //必须要调用这个,否则文档不会生成的
// ps.setFormFlattening(true);
// if(ps != null){
// ps.close();
// }
// //生成pdf路径存放的路径
// fos = response.getOutputStream();
// fos.write(bos.toByteArray());
//
// }catch (Exception e){
// e.printStackTrace();
// }finally {
// if(fos!=null){
// fos.flush();
// fos.close();
// }
// if (bos != null){
// bos.close();
// }
// if(reader != null){
// reader.close();
// }
// }
}
/**
......@@ -333,19 +333,19 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
* @param ps
*/
public void fillData(AcroFields fields, Map<String, String> data, PdfStamper ps) {
try {
for (String key : data.keySet()) {
String value = data.get(key);
if (key.contains("image")) {
addImageToPdf(key, fields, ps, value);
continue;
}
// 为字段赋值,注意字段名称是区分大小写的
fields.setField(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
// try {
// for (String key : data.keySet()) {
// String value = data.get(key);
// if (key.contains("image")) {
// addImageToPdf(key, fields, ps, value);
// continue;
// }
// // 为字段赋值,注意字段名称是区分大小写的
// fields.setField(key, value);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
}
/**
......@@ -360,21 +360,21 @@ public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements Bo
*/
private static void addImageToPdf(String key,AcroFields form, PdfStamper stamper, String filePath) throws DocumentException, IOException, IOException {
// 通过图片域名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 读图片
Image image = Image.getInstance(filePath);
// 获取操作的页面
PdfContentByte under = stamper.getOverContent(pageNo);
// 根据域的大小缩放图片
image.scaleToFit(signRect.getWidth() * 2, signRect.getHeight());
// 添加图片并设置位置(个人通过此设置使得图片垂直水平居中,可参考,具体情况已实际为准)
image.setAbsolutePosition(x, y);
under.addImage(image);
// // 通过图片域名获取所在页和坐标,左下角为起点
// int pageNo = form.getFieldPositions(key).get(0).page;
// Rectangle signRect = form.getFieldPositions(key).get(0).position;
// float x = signRect.getLeft();
// float y = signRect.getBottom();
//
// // 读图片
// Image image = Image.getInstance(filePath);
// // 获取操作的页面
// PdfContentByte under = stamper.getOverContent(pageNo);
// // 根据域的大小缩放图片
// image.scaleToFit(signRect.getWidth() * 2, signRect.getHeight());
// // 添加图片并设置位置(个人通过此设置使得图片垂直水平居中,可参考,具体情况已实际为准)
// image.setAbsolutePosition(x, y);
// under.addImage(image);
}
}
......
package com.ruoyi.service.impl;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.domain.vo.PhotoStorageVO;
import com.ruoyi.service.PdfTemplateManagementService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Map;
/**
* PDF模板管理Service业务层处理
* @author gxk
*/
@Service
@Transactional
public class PdfTemplateManagementServiceImpl implements PdfTemplateManagementService {
/**
* 检验报告PDF下载
* @param response
*/
@Override
public void generateInspectionReport(HttpServletResponse response) throws Exception {
PdfReader reader = null;
PdfStamper ps = null;
OutputStream fos = null;
ByteArrayOutputStream bos = null;
try {
// 模板绝对路径--服务器
String fileName = "/template/receipt_template_02_27.pdf";
// 读取现有模板内容
reader = new PdfReader(fileName);
// 创建输出流
bos = new ByteArrayOutputStream();
// 实例化PdfStamper准备编辑pdf内容
ps = new PdfStamper(reader, bos);
// 获取表单所有元素
AcroFields fields = ps.getAcroFields();
// 设置具体字体格式的编码, 不设置的话中文可能不会显示
BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
fields.addSubstitutionFont(bf);
// 渲染
// fillData(fields, map, ps);
renderData(fields);
PhotoStorageVO photoStorageVO = new PhotoStorageVO();
Class<? extends PhotoStorageVO> aClass = photoStorageVO.getClass();
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field : declaredFields) {
String name = field.getName();
System.out.println(name);
}
//必须要调用这个,否则文档不会生成的
ps.setFormFlattening(true);
if(ps != null){
ps.close();
}
//生成pdf路径存放的路径
fos = response.getOutputStream();
fos.write(bos.toByteArray());
}catch (Exception e){
e.printStackTrace();
}finally {
if(fos!=null){
fos.flush();
fos.close();
}
if (bos != null){
bos.close();
}
if(reader != null){
reader.close();
}
}
}
/**
* 渲染数据
* @param fields
*/
private void renderData(AcroFields fields) {
}
/**
* 填充模板中的数据
* @param fields
* @param data 是一个Map<String,String> 主要存储 key 表单模板中的单元格名 value为想要赋的值,遍历
* @param ps
*/
public void fillData(AcroFields fields, Map<String, String> data, PdfStamper ps) {
try {
for (String key : data.keySet()) {
String value = data.get(key);
if (key.contains("image")) {
addImageToPdf(key, fields, ps, value);
continue;
}
// 为字段赋值,注意字段名称是区分大小写的
fields.setField(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加图片
* @param key 应为模板名
* @param form 动态字段
* @param stamper
* @param filePath 本地图片路径
* @throws DocumentException
* @throws IOException
* @throws IOException
*/
private static void addImageToPdf(String key,AcroFields form, PdfStamper stamper, String filePath) throws DocumentException, IOException, IOException {
// 通过图片域名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 读图片
Image image = Image.getInstance(filePath);
// 获取操作的页面
PdfContentByte under = stamper.getOverContent(pageNo);
// 根据域的大小缩放图片
image.scaleToFit(signRect.getWidth() * 2, signRect.getHeight());
// 添加图片并设置位置(个人通过此设置使得图片垂直水平居中,可参考,具体情况已实际为准)
image.setAbsolutePosition(x, y);
under.addImage(image);
}
/**
* 通过属性名获取属性在对象里的值
* @param obj
* @param propertyName
* @return
* @param <T>
*/
public static <T> T getPropertyValue(Object obj, String propertyName) {
try {
// 获取对象的类
Class<?> objClass = obj.getClass();
// 获取类的指定属性
Field field = objClass.getDeclaredField(propertyName);
// 设置属性为可访问
field.setAccessible(true);
// 获取属性的值
@SuppressWarnings("unchecked")
T value = (T) field.get(obj);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
package com.ruoyi.web;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.service.PdfTemplateManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
/**
* @author gxk
*/
@Api(tags = "PDF模板管理")
@RestController
@RequestMapping("/pdf")
public class PdfTemplateManagementController {
@Autowired
private PdfTemplateManagementService pdfTemplateManagementService;
/**
* 检验报告PDF下载
* @param response
*/
@Anonymous
@ApiOperation("检验报告PDF下载")
@GetMapping(value = "/inspectionReport", produces = "application/json")
public void download(HttpServletResponse response) {
response.setHeader("content-disposition","attachment;fileName="+"ReceiptPrinter.pdf");
try {
// demo-模板
pdfTemplateManagementService.generateInspectionReport(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.ruoyi.web;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
......
package com.ruoyi.web.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 通过角色查询用户信息DTO
* @author gxk
*/
@Data
@ApiModel
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserByRoleRequest {
@ApiModelProperty("角色名称")
@NotNull(message = "角色名称不能为空")
@NotEmpty(message = "角色名称不能为空")
private String roleName;
}
......@@ -3,6 +3,12 @@ package com.ruoyi.web.controller.system;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.web.request.UserByRoleRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
......@@ -40,6 +46,7 @@ import com.ruoyi.system.service.ISysUserService;
*
* @author ruoyi
*/
@Api(tags = "用户信息")
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController
......@@ -70,6 +77,26 @@ public class SysUserController extends BaseController
return getDataTable(list);
}
/**
* 通过角色名获取用户信息
* @param request
* @return
*/
@ApiOperation("通过角色名获取用户信息")
@PostMapping("/getUserByRole")
public R<List<SysUser>> getUserByRole(@Validated @RequestBody UserByRoleRequest request) {
// 通过角色名获取角色信息
SysRole role = new SysRole();
role.setRoleName(request.getRoleName());
List<SysRole> roleList = roleService.selectRoleList(role);
if (CollUtil.isNotEmpty(roleList)) {
List<SysUser> list = userService.selectUserListByRoleId(roleList.get(0).getRoleId());
return R.ok(list);
} else {
return R.fail("角色不存在");
}
}
@Trace
@Tags({@Tag(key = "param", value = "arg[0]"), @Tag(key = "param1", value = "arg[1]"), @Tag(key = "result", value = "returnedObj")})
@Log(title = "用户管理", businessType = BusinessType.EXPORT)
......
......@@ -218,4 +218,5 @@ public interface ISysUserService
* @return 结果
*/
public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName);
}
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