Commit 00bf95bd authored by 徐俊's avatar 徐俊

xujun

parent 66734b50
package com.yiboshi.science.rest.v1; package com.yiboshi.science.rest.v1;
import com.itextpdf.text.DocumentException;
import com.yiboshi.arch.base.ResponseDataModel; import com.yiboshi.arch.base.ResponseDataModel;
import com.yiboshi.science.base.Pagination; import com.yiboshi.science.base.Pagination;
import com.yiboshi.science.config.annotation.Logs; import com.yiboshi.science.config.annotation.Logs;
...@@ -15,6 +16,7 @@ import com.yiboshi.science.rest.BaseController; ...@@ -15,6 +16,7 @@ import com.yiboshi.science.rest.BaseController;
import com.yiboshi.science.service.ComFileService; import com.yiboshi.science.service.ComFileService;
import com.yiboshi.science.service.ComProjectService; import com.yiboshi.science.service.ComProjectService;
import com.yiboshi.science.service.ComProjectTaskService; import com.yiboshi.science.service.ComProjectTaskService;
import com.yiboshi.science.utils.ProjectInfoToPDF;
import com.yiboshi.science.utils.StringUtil; import com.yiboshi.science.utils.StringUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -207,4 +209,14 @@ public class ComProjectController extends BaseController<ComProjectService, ComP ...@@ -207,4 +209,14 @@ public class ComProjectController extends BaseController<ComProjectService, ComP
(@Validated @RequestBody List<ProjectImportDTO> list) throws IOException { (@Validated @RequestBody List<ProjectImportDTO> list) throws IOException {
return ResponseDataModel.ok(comProjectService.projectImport(list)); return ResponseDataModel.ok(comProjectService.projectImport(list));
} }
@ApiOperation(value = "项目信息导出", httpMethod = "POST", notes = "项目信息导出")
@RequestMapping("/projectExport/{id}")
@PostMapping
public ResponseDataModel<String> projectExport(@PathVariable String id) throws DocumentException, IOException {
ComProjectDTO dto = comProjectService.getProjectById(id);
String outputPath = "D:\\申请书和合同书.pdf";
ProjectInfoToPDF.generateProjectPDF(dto, outputPath);
return ResponseDataModel.ok("项目信息导入成功!");
}
} }
\ No newline at end of file
package com.yiboshi.science.utils;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.yiboshi.science.param.dto.ComProjectDTO;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.util.Objects;
public class ProjectInfoToPDF {
/**
* 生成项目申请书和合同书PDF
* @param project 项目信息
* @param outputPath 输出路径
*/
public static void generateProjectPDF(ComProjectDTO project, String outputPath) throws DocumentException, IOException {
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPath));
document.open();
// 设置中文字体
BaseFont bfChinese;
try {
// 从资源文件夹加载字体
InputStream fontStream = ProjectInfoToPDF.class.getResourceAsStream("/fonts/simsun.ttc");
if (fontStream == null) {
throw new FileNotFoundException("在resources/fonts目录下未找到simsun.ttc字体文件");
}
// 将字体文件复制到临时文件
File tempFile = File.createTempFile("simsun", ".ttc");
tempFile.deleteOnExit();
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int length;
while ((length = fontStream.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
}
bfChinese = BaseFont.createFont(tempFile.getAbsolutePath() + ",0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (Exception e) {
throw new RuntimeException("加载字体文件失败,请确保resources/fonts目录下存在simsun.ttc文件:" + e.getMessage(), e);
}
Font labelFont = new Font(bfChinese, 16, Font.NORMAL);
Font valueFont = new Font(bfChinese, 16, Font.NORMAL);
Font normalFont = new Font(bfChinese, 12, Font.NORMAL);
Font boldFont = new Font(bfChinese, 12, Font.BOLD);
//首页内容
FirstPageInfo(document, project, bfChinese);
// 添加项目基本信息表格
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.setSpacingBefore(10f);
table.setSpacingAfter(10f);
// 设置列宽度
float[] columnWidths = {0.3f, 0.7f};
table.setWidths(columnWidths);
// 添加表格内容
addTableRow(table, "项目名称:", project.getProjName(), labelFont, valueFont);
addTableRow(table, "项目申报单位:", project.getProjNo(), labelFont, valueFont);
addTableRow(table, "申请单位:", project.getAppUnitName(), labelFont, valueFont);
addTableRow(table, "项目负责人:", project.getAppPersonName(), labelFont, valueFont);
addTableRow(table, "联系电话:", project.getMobile(), labelFont, valueFont);
addTableRow(table, "电子邮箱:", project.getEmail(), labelFont, valueFont);
document.add(table);
// 添加项目内容章节
addSection(document, "一、项目实施目标", boldFont);
addContent(document, project.getResearchContent(), normalFont);
addSection(document, "二、主要技术指标", boldFont);
addContent(document, project.getTechnologyTarget(), normalFont);
addSection(document, "三、主要经济指标", boldFont);
addContent(document, project.getEconomyTarget(), normalFont);
addSection(document, "四、项目实施中形成的示范基地、中试线、生产线及其规模等", boldFont);
addContent(document, project.getAchievementTarget(), normalFont);
addSection(document, "五、科技报告考核指标", boldFont);
addContent(document, project.getTechnologyReportsTarget(), normalFont);
addSection(document, "六、其他应考核的指标", boldFont);
addContent(document, project.getOtherTarget(), normalFont);
// 添加签字栏
addSignatureSection(document, normalFont);
document.close();
}
//首页
private static Document FirstPageInfo(Document document, ComProjectDTO project, BaseFont bfChinese) throws DocumentException {
Font titleFont = new Font(bfChinese, 20, Font.BOLD);
Font normalFont = new Font(bfChinese, 12, Font.NORMAL);
// 添加项目编号到左上角
Paragraph projNo;
if (Objects.nonNull(project.getProjNo())) {
Chunk text = new Chunk("项目编号:", normalFont);
Chunk number = new Chunk(project.getProjNo(), normalFont);
number.setUnderline(0.1f, -2f); // 设置下划线
projNo = new Paragraph();
projNo.add(text);
projNo.add(number);
projNo.add(Chunk.NEWLINE);
projNo.add(Chunk.NEWLINE);
projNo.add(Chunk.NEWLINE);
projNo.add(Chunk.NEWLINE);
} else {
Chunk text = new Chunk("项目编号:", normalFont);
Chunk underline = new Chunk(" ", normalFont);
underline.setUnderline(0.1f, -2f); // 设置下划线
projNo = new Paragraph();
projNo.add(text);
projNo.add(underline);
projNo.add(Chunk.NEWLINE);
projNo.add(Chunk.NEWLINE);
projNo.add(Chunk.NEWLINE);
projNo.add(Chunk.NEWLINE);
}
projNo.setAlignment(Element.ALIGN_LEFT);
projNo.setSpacingAfter(10);
document.add(projNo);
// 添加标题
Paragraph title = new Paragraph("省级临床医学中心科研项目申请书", titleFont);
title.setAlignment(Element.ALIGN_CENTER);
title.setSpacingAfter(20);
document.add(title);
return document;
}
private static void addTableRow(PdfPTable table, String label, String value, Font labelFont, Font valueFont) {
PdfPCell labelCell = new PdfPCell(new Phrase(label, labelFont));
labelCell.setMinimumHeight(25f);
labelCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
labelCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
labelCell.setPaddingRight(5f);
labelCell.setBorder(Rectangle.NO_BORDER);
// 创建带下划线的值
Chunk valueChunk;
if (value != null && !value.trim().isEmpty()) {
valueChunk = new Chunk(value, valueFont);
} else {
valueChunk = new Chunk(" ", valueFont);
}
valueChunk.setUnderline(0.1f, -2f); // 添加下划线
PdfPCell valueCell = new PdfPCell(new Phrase(valueChunk));
valueCell.setMinimumHeight(25f);
valueCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
valueCell.setPaddingLeft(5f);
valueCell.setBorder(Rectangle.NO_BORDER);
table.addCell(labelCell);
table.addCell(valueCell);
}
private static void addSection(Document document, String title, Font font) throws DocumentException {
Paragraph section = new Paragraph(title, font);
section.setSpacingBefore(15);
section.setSpacingAfter(10);
document.add(section);
}
private static void addContent(Document document, String content, Font font) throws DocumentException {
if (content != null) {
Paragraph paragraph = new Paragraph(content, font);
paragraph.setIndentationLeft(20);
paragraph.setSpacingAfter(10);
document.add(paragraph);
}
}
private static void addSignatureSection(Document document, Font font) throws DocumentException {
Paragraph signature = new Paragraph("\n\n", font);
signature.add(new Chunk("申请人签字:", font));
signature.add(new Chunk(" ", font));
signature.add(new Chunk("日期:", font));
signature.add(new Chunk(" ", font));
signature.setAlignment(Element.ALIGN_RIGHT);
signature.setSpacingBefore(50);
document.add(signature);
Paragraph unitSignature = new Paragraph("\n", font);
unitSignature.add(new Chunk("申请单位(盖章):", font));
unitSignature.add(new Chunk(" ", font));
unitSignature.add(new Chunk("日期:", font));
unitSignature.add(new Chunk(" ", font));
unitSignature.setAlignment(Element.ALIGN_RIGHT);
unitSignature.setSpacingBefore(30);
document.add(unitSignature);
}
}
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