refactor: 更新 pdf 模板

11111
winter 10 months ago
parent bc13f67a29
commit 079f2fbf6f

@ -3,16 +3,8 @@ package cn.teammodel.controller.frontend;
import cn.teammodel.common.R; import cn.teammodel.common.R;
import cn.teammodel.repository.AppraiseRepository; import cn.teammodel.repository.AppraiseRepository;
import cn.teammodel.service.EvaluationService; import cn.teammodel.service.EvaluationService;
import cn.teammodel.utils.ChartUtil;
import cn.teammodel.utils.PdfUtil;
import com.itextpdf.text.DocumentException; import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -20,12 +12,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
@RestController @RestController
@RequestMapping("/") @RequestMapping("/")
@ -51,37 +39,37 @@ public class HelloController {
} }
@GetMapping("public/pdf") @GetMapping("public/pdf")
public void freepdf(HttpServletResponse response) throws DocumentException, IOException { public void freepdf(HttpServletResponse response) throws DocumentException, IOException { // 设置response参数
// 设置response参数 // response.reset();
response.reset(); // response.setContentType("application/pdf");
response.setContentType("application/pdf"); // response.setHeader("Content-disposition",
response.setHeader("Content-disposition", // "attachment;filename=report_student_" + System.currentTimeMillis() + ".pdf");
"attachment;filename=report_student_" + System.currentTimeMillis() + ".pdf"); // ClassPathResource resource = new ClassPathResource("templates/pdf_templates/student_report.pdf");
ClassPathResource resource = new ClassPathResource("templates/pdf_templates/student_report.pdf"); // InputStream in = resource.getInputStream();
InputStream in = resource.getInputStream(); // ServletOutputStream os = response.getOutputStream();
ServletOutputStream os = response.getOutputStream(); // // 处理 stampter
// 处理 stampter // PdfReader pdfReader = new PdfReader(in);
PdfReader pdfReader = new PdfReader(in); // PdfStamper stamper = new PdfStamper(pdfReader, os);
PdfStamper stamper = new PdfStamper(pdfReader, os); //
// Map<String, String> data = PdfUtil.data();
Map<String, String> data = PdfUtil.data(); // DefaultPieDataset dataset = new DefaultPieDataset( );
DefaultPieDataset dataset = new DefaultPieDataset( ); // dataset.setValue( "IPhone 5s" , new Double( 20 ) );
dataset.setValue( "IPhone 5s" , new Double( 20 ) ); // dataset.setValue( "SamSung Grand" , new Double( 20 ) );
dataset.setValue( "SamSung Grand" , new Double( 20 ) ); // dataset.setValue( "MotoG" , new Double( 40 ) );
dataset.setValue( "MotoG" , new Double( 40 ) ); // dataset.setValue( "Nokia Lumia" , new Double( 10 ) );
dataset.setValue( "Nokia Lumia" , new Double( 10 ) ); //
// JFreeChart pieChart = ChartUtil.pieChart("手机销量统计", dataset);
JFreeChart pieChart = ChartUtil.pieChart("手机销量统计", dataset); // ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // ChartUtils.writeChartAsJPEG(bos, pieChart, 850, 440);
ChartUtils.writeChartAsJPEG(bos, pieChart, 850, 440); // // 填充表单
// 填充表单 // PdfUtil.fillPdfForm(stamper, data);
PdfUtil.fillPdfForm(stamper, data); // PdfUtil.fillImage(stamper, "praiseDistribution", bos.toByteArray());
PdfUtil.fillImage(stamper, "praiseDistribution", bos.toByteArray()); // PdfUtil.fillImage(stamper, "criticalDistribution", bos.toByteArray());
PdfUtil.fillImage(stamper, "criticalDistribution", bos.toByteArray()); // // 关闭流
// 关闭流 // stamper.setFormFlattening(true);
stamper.setFormFlattening(true); // stamper.close();
stamper.close(); // os.close();
os.close(); //
} }

@ -668,7 +668,7 @@ public class EvaluationServiceImpl implements EvaluationService {
PdfUtil.fillImage(stamper, "praiseDistribution", praiseBos.toByteArray()); PdfUtil.fillImage(stamper, "praiseDistribution", praiseBos.toByteArray());
PdfUtil.fillImage(stamper, "criticalDistribution", criticalBos.toByteArray()); PdfUtil.fillImage(stamper, "criticalDistribution", criticalBos.toByteArray());
// 关闭流 // 关闭流
stamper.setFormFlattening(true); // stamper.setFormFlattening(true);
stamper.close(); stamper.close();
os.close(); os.close();

Loading…
Cancel
Save