生成pdf
continuous-integration/drone/push Build is passing Details

于相涌/robot_optimize
YXY 1 year ago
parent a81f7cacf1
commit 10824d0a70

@ -22,12 +22,16 @@
box-shadow: none; box-shadow: none;
line-height: 30px; line-height: 30px;
margin: 0px 10%; margin: 0px 10%;
height: auto !important; }
padding: 0px 75px 100px 75px !important; table{
height:700px
} }
.sign { .sign {
margin-left: 70%; margin-left: 70%;
} }
.bigRow{
height: 180px;
}
</style> </style>
</head> </head>
@ -35,7 +39,7 @@
<div class="mybody"> <div class="mybody">
<h1>课程反馈表</h1> <h1>课程反馈表</h1>
<div class="content_class"> <div class="content_class">
<table border="1" width="500" cellspacing="0" cellpadding="0"> <table border="1" width="600" cellspacing="0" cellpadding="0">
<tr> <tr>
<td>专业 Program</td> <td>专业 Program</td>
<td>${program!}</td> <td>${program!}</td>
@ -53,22 +57,22 @@
<td>${endClassTime!}</td> <td>${endClassTime!}</td>
</tr> </tr>
<tr colspan="4"> <tr colspan="4" class="bigRow">
<td>作业完成情况 <br /> <td>作业完成情况 <br />
Feedback</td> Feedback</td>
<td colspan="4">${feedback!}</td> <td colspan="4"><b>评分 ${feedbackScore}</b><br/>${feedback!}</td>
</tr> </tr>
<tr colspan="4"> <tr colspan="4" class="bigRow">
<td>课堂表现 <br /> <td>课堂表现 <br />
Performance</td> Performance</td>
<td colspan="4">${performance!}</td> <td colspan="4"><b>评分 ${performanceScore}</b><br/>${performance!}</td>
</tr> </tr>
<tr colspan="4"> <tr colspan="4" class="bigRow">
<td>课堂内容 <br /> <td>课堂内容 <br />
Content</td> Content</td>
<td colspan="4">${content!}</td> <td colspan="4">${content!}</td>
</tr> </tr>
<tr colspan="4"> <tr colspan="4" class="bigRow">
<td>课后作业 <br /> <td>课后作业 <br />
Work</td> Work</td>
<td colspan="4">${work!}</td> <td colspan="4">${work!}</td>

@ -2,16 +2,19 @@ package com.ruoyi.common.utils;
import cn.hutool.core.io.resource.ClassPathResource; import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.Resource; import cn.hutool.core.io.resource.Resource;
import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import org.xhtmlrenderer.pdf.ITextRenderer; import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File; import java.io.*;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Locale; import java.util.Locale;
/** /**
@ -19,6 +22,7 @@ import java.util.Locale;
* @create 2023-05-21 10:38 * @create 2023-05-21 10:38
*/ */
@Slf4j @Slf4j
@Component
public class PdfUtils { public class PdfUtils {
private final static String TEMPLATE_BASE_PATH = "/template/";//存放文件模板的地址 private final static String TEMPLATE_BASE_PATH = "/template/";//存放文件模板的地址
@ -26,23 +30,46 @@ public class PdfUtils {
private final static String DEFAULT_FONT = "font/simsun.ttc";//默认字体资源文件([宋体][simsun.ttc] private final static String DEFAULT_FONT = "font/simsun.ttc";//默认字体资源文件([宋体][simsun.ttc]
private final static String ENCODING = "UTF-8";//指定编码 private final static String ENCODING = "UTF-8";//指定编码
public void createFile(String templateCode, Object data){
try {
String templatePath = ResourceUtils.getURL("classpath:").getPath();
createPDF(templateCode,data,templatePath);
waterMark(getPath("test.pdf"),templatePath+"photo/watermark.jpg");
}catch (Exception e){
log.error("PDF导出异常", e);
}
}
private String getPath(String fileName){
return System.getProperty("user.dir") + "/" + fileName;
}
private FileOutputStream getOutputStream(String fileName) throws IOException {
String path = getPath(fileName);
File file = new File(path);
if (!file.exists()){
file.createNewFile();
}
return new FileOutputStream(file);
}
/** /**
* pdf * pdf
* @param templateCode * @param templateCode
* @param data freemarker * @param data freemarker
* @param out pdf
*/ */
public static void createPDF(String templateCode, Object data, OutputStream out, String imgName) { public void createPDF(String templateCode, Object data, String templatePath) {
FileOutputStream fileOutputStream = null;
try { try {
fileOutputStream = getOutputStream("test.pdf");
// 创建一个FreeMarker实例, 负责管理FreeMarker模板的Configuration实例 // 创建一个FreeMarker实例, 负责管理FreeMarker模板的Configuration实例
Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
String path = ResourceUtils.getURL("classpath:").getPath();
// 指定FreeMarker模板文件的位置 // 指定FreeMarker模板文件的位置
cfg.setDirectoryForTemplateLoading(new File(path + TEMPLATE_BASE_PATH)); cfg.setDirectoryForTemplateLoading(new File(templatePath + TEMPLATE_BASE_PATH));
ITextRenderer renderer = new ITextRenderer(); ITextRenderer renderer = new ITextRenderer();
// 设置 css中 的字体样式(暂时仅支持宋体和黑体) // 设置 css中 的字体样式(暂时仅支持宋体和黑体)
String fontPath = path + DEFAULT_FONT; String fontPath = templatePath + DEFAULT_FONT;
renderer.getFontResolver().addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.getFontResolver().addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 设置模板的编码格式 // 设置模板的编码格式
cfg.setEncoding(Locale.CHINA, ENCODING); cfg.setEncoding(Locale.CHINA, ENCODING);
@ -56,12 +83,83 @@ public class PdfUtils {
// 把html代码传入渲染器中 // 把html代码传入渲染器中
renderer.setDocumentFromString(html); renderer.setDocumentFromString(html);
renderer.layout(); renderer.layout();
renderer.createPDF(out, false); renderer.createPDF(fileOutputStream, false);
renderer.finishPDF(); renderer.finishPDF();
out.flush(); fileOutputStream.flush();
out.close();
} catch (Exception e) { } catch (Exception e) {
log.error("PDF导出异常", e); log.error("PDF导出异常", e);
}finally {
if (fileOutputStream != null) {
try {
fileOutputStream.flush();
fileOutputStream.close();
} catch (final IOException e) {
log.error("failed to close outputStream!", e);
}
}
}
}
public boolean waterMark(String inputFile, String picFilePath) throws IOException, DocumentException {
PdfReader reader = null;
PdfStamper stamper = null;
FileOutputStream fileOutputStream = null;
try {
int picRowNum = 7;
int picColNum = 4;
float transDegree = 0.5f;
reader = new PdfReader(inputFile);
fileOutputStream = getOutputStream("test1.pdf");
stamper = new PdfStamper(reader,fileOutputStream);
// 设置水印支持中文
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
int total = reader.getNumberOfPages() + 1;
PdfContentByte watermark;
// 遍历pdf文件的每页以处理
for (int i = 1; i < total; i++) {
// 获得PDF最顶层
watermark = stamper.getUnderContent(i); //在内容上方加水印
watermark.saveState();
PdfGState gs = new PdfGState();
// 设置透明度范围为0到1
gs.setFillOpacity(transDegree);
watermark.setGState(gs);
watermark.beginText();
watermark.setFontAndSize(base, 15); //字体大小
watermark.setColorFill(BaseColor.BLACK); //字体颜色
for (int col = 1; col < picColNum+1; col++) {
for (int row = 1; row < picRowNum+1; row++) {
if (picFilePath != null || !"".contentEquals(picFilePath)) {
// 设置图片水印
Image image = Image.getInstance(picFilePath);
image.setAbsolutePosition(100f, 250f);
image.scaleAbsolute(500, 500); // 自定义大小
watermark.setGState(gs); // 设置透明度
watermark.addImage(image); // 添加水印图片
}
}
}
// 添加水印文字
watermark.endText();
watermark.setLineWidth(1f);
watermark.stroke();
}
stamper.close();
reader.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}finally {
if (stamper != null){
stamper.close();
}
if (reader != null){
reader.close();
}
if (fileOutputStream != null){
fileOutputStream.close();
}
return false;
} }
} }

@ -11,6 +11,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -20,6 +21,8 @@ public class EdiController extends BaseController {
@Autowired @Autowired
private EdiOperateService ediOperateService; private EdiOperateService ediOperateService;
@Resource
private PdfUtils pdfUtils;
/** /**
* *
@ -30,14 +33,20 @@ public class EdiController extends BaseController {
// } // }
@RequestMapping("sync2Table") @RequestMapping("sync2Table")
@ResponseBody public void exportPdfDemo(@RequestParam("param")String param) throws Exception{
public void exportPdfDemo(HttpServletResponse response, @RequestParam("param")String param) throws Exception{
CourseFeedbackTemplateDto resultDTO = new CourseFeedbackTemplateDto(); CourseFeedbackTemplateDto resultDTO = new CourseFeedbackTemplateDto();
resultDTO.setWatermark("");//设置图片,没有图片设置空即可 resultDTO.setProgram("program");
resultDTO.setTemplateUrl("COURSE_FEEDBACK");//参数 resultDTO.setStudentName("student");
resultDTO.setProgram("aaaa"); resultDTO.setTutor("tutor");
resultDTO.setStudentName("aaaa"); resultDTO.setStartClassTime("2023-05-21 15:51:45");
PdfUtils.createPDF("COURSE_FEEDBACK.ftl",resultDTO,response.getOutputStream(),""); resultDTO.setEndClassTime("2023-05-22 15:51:45");
resultDTO.setFeedback("作业完成情况");
resultDTO.setFeedbackScore("7 /10");
resultDTO.setPerformance("课堂表现");
resultDTO.setPerformanceScore("9/10");
resultDTO.setContent("<p>1、课堂内容1</p><p>1、课堂内容2</p>");
resultDTO.setWork("<p>1、课后作业1</p><p>1、课后作业2</p><p>1、课后作业3</p>");
pdfUtils.createFile("COURSE_FEEDBACK.ftl",resultDTO);
} }
} }

@ -19,7 +19,9 @@ public class CourseFeedbackTemplateDto {
private String endClassTime; private String endClassTime;
private String tutor; private String tutor;
private String feedback; private String feedback;
private String feedbackScore;
private String performance; private String performance;
private String performanceScore;
private String content; private String content;
private String work; private String work;

Loading…
Cancel
Save