diff --git a/pom.xml b/pom.xml index 8ec0e1d..8b18d9d 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,24 @@ spring-boot-configuration-processor + + + com.itextpdf + itextpdf + 5.5.10 + + + com.itextpdf + itext-asian + 5.2.0 + + + + org.jfree + jfreechart + 1.5.0 + + com.squareup.okhttp3 okhttp diff --git a/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java b/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java index c1699cf..36439a7 100644 --- a/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java +++ b/src/main/java/cn/teammodel/controller/frontend/AppraiseController.java @@ -7,6 +7,7 @@ import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.model.vo.appraise.AppraiseRecordVo; import cn.teammodel.model.vo.appraise.StudentReportVo; import cn.teammodel.service.EvaluationService; +import com.itextpdf.text.DocumentException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.PostMapping; @@ -15,7 +16,9 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; +import java.io.IOException; import java.util.List; /** @@ -83,6 +86,9 @@ public class AppraiseController { return R.success(res); } - - + @PostMapping("studentReportPDF") + @ApiOperation(value = "导出学生当前的学期的实时评价报告 PDF") + public void exportStuReportPdf(@Valid @RequestBody IdRequest idRequest, HttpServletResponse response) throws DocumentException, IOException { + evaluationService.exportStuReportPdf(idRequest, response); + } } diff --git a/src/main/java/cn/teammodel/controller/frontend/HelloController.java b/src/main/java/cn/teammodel/controller/frontend/HelloController.java index 2690529..126e3ec 100644 --- a/src/main/java/cn/teammodel/controller/frontend/HelloController.java +++ b/src/main/java/cn/teammodel/controller/frontend/HelloController.java @@ -1,7 +1,11 @@ package cn.teammodel.controller.frontend; +import cn.hutool.core.map.MapUtil; import cn.teammodel.common.R; import cn.teammodel.repository.AppraiseRepository; +import cn.teammodel.service.EvaluationService; +import cn.teammodel.utils.PdfUtil; +import com.itextpdf.text.DocumentException; import io.swagger.annotations.Api; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.context.SecurityContextHolder; @@ -10,12 +14,17 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Map; @RestController @RequestMapping("/") @Api(tags = "鉴权测试") public class HelloController { + @Resource + private EvaluationService evaluationService; @Resource private AppraiseRepository appraiseRepository; @@ -31,4 +40,12 @@ public class HelloController { public R free() { return new R(200, "success","hello world"); } + + @GetMapping("public/pdf") + public void freepdf(HttpServletResponse response) throws DocumentException, IOException { + Map data = MapUtil.ofEntries(MapUtil.entry("Text1", "1000"), MapUtil.entry("Text2", "2000"), MapUtil.entry("Text3", "3000")); + PdfUtil.fillStudentPdfForm(data, response); + } + + } \ No newline at end of file diff --git a/src/main/java/cn/teammodel/service/EvaluationService.java b/src/main/java/cn/teammodel/service/EvaluationService.java index 8df8500..2e6a6c3 100644 --- a/src/main/java/cn/teammodel/service/EvaluationService.java +++ b/src/main/java/cn/teammodel/service/EvaluationService.java @@ -6,7 +6,10 @@ import cn.teammodel.model.entity.appraise.Appraise; import cn.teammodel.model.entity.appraise.AppraiseTreeNode; import cn.teammodel.model.vo.appraise.AppraiseRecordVo; import cn.teammodel.model.vo.appraise.StudentReportVo; +import com.itextpdf.text.DocumentException; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; import java.util.List; /** @@ -59,4 +62,6 @@ public interface EvaluationService { * 学生评价报告 */ StudentReportVo studentReport(IdRequest idRequest); + + void exportStuReportPdf(IdRequest idRequest, HttpServletResponse response) throws IOException, DocumentException; } diff --git a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java index 8dc582a..964069c 100644 --- a/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/EvaluationServiceImpl.java @@ -22,6 +22,7 @@ import cn.teammodel.utils.RepositoryUtil; import cn.teammodel.utils.SchoolDateUtil; import com.azure.cosmos.models.CosmosPatchOperations; import com.azure.spring.data.cosmos.core.query.CosmosPageRequest; +import com.itextpdf.text.DocumentException; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Page; @@ -29,6 +30,9 @@ import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; import java.time.Instant; import java.time.LocalDate; import java.util.*; @@ -602,6 +606,14 @@ public class EvaluationServiceImpl implements EvaluationService { .build(); } + @Override + public void exportStuReportPdf(IdRequest idRequest, HttpServletResponse response) throws IOException, DocumentException{ + + + ServletOutputStream os = response.getOutputStream(); + + } + /** * 递归收集 id 的节点及 id 节点的孩子节点 (迭代器删除居然也报错) */ diff --git a/src/main/java/cn/teammodel/utils/PdfUtil.java b/src/main/java/cn/teammodel/utils/PdfUtil.java new file mode 100644 index 0000000..269f130 --- /dev/null +++ b/src/main/java/cn/teammodel/utils/PdfUtil.java @@ -0,0 +1,43 @@ +package cn.teammodel.utils; + +import com.itextpdf.text.DocumentException; +import com.itextpdf.text.pdf.AcroFields; +import com.itextpdf.text.pdf.PdfReader; +import com.itextpdf.text.pdf.PdfStamper; +import org.springframework.core.io.ClassPathResource; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Map; + +/** + * @author winter + * @create 2024-01-24 16:02 + */ +public class PdfUtil { + + public static void fillStudentPdfForm(Map data, HttpServletResponse response) throws IOException, DocumentException { + response.reset(); + response.setContentType("application/pdf"); + response.setHeader("Content-disposition", + "attachment;filename=report_student_" + System.currentTimeMillis() + ".pdf"); + + ClassPathResource resource = new ClassPathResource("templates/pdf_templates/template.pdf"); + InputStream in = resource.getInputStream(); + ServletOutputStream os = response.getOutputStream(); + fillPdfForm(in, os, data); + } + public static void fillPdfForm(InputStream in, OutputStream os, Map data) throws IOException, DocumentException { + PdfReader pdfReader = new PdfReader(in); + PdfStamper stamper = new PdfStamper(pdfReader, os); + AcroFields fields = stamper.getAcroFields(); + for (Map.Entry entry : data.entrySet()) { + fields.setField(entry.getKey(), entry.getValue()); + } + stamper.close(); + os.close(); + } +} \ No newline at end of file diff --git a/src/main/resources/templates/pdf_templates/template.pdf b/src/main/resources/templates/pdf_templates/template.pdf new file mode 100644 index 0000000..5305d16 Binary files /dev/null and b/src/main/resources/templates/pdf_templates/template.pdf differ