打印工具类增加自动判断编译新模板

This commit is contained in:
tangw 2025-07-25 18:29:14 +08:00
parent cc306258c7
commit 2ae4249d84

View File

@ -36,8 +36,9 @@ import javax.print.attribute.standard.OrientationRequested;
@Component
@Slf4j
public class JasperReportUtil {
private static String template="template";
private static String templatep="template/";
private static final String template="template";
private static final String templatep="template/";
private static final String TEMPLATE_PATH = JasperReportUtil.getTemplatePath();
/**
* 核心打印/导出方法
* @param printerName 打印机名称(可为null,null时使用默认打印机)
@ -100,6 +101,7 @@ public class JasperReportUtil {
private static JasperReport loadTemplate(String templateName) throws Exception {
// 模板路径:resources/templates/
String templatePath = templatep + templateName;
compileAndSave(templateName);
InputStream jasperStream = JasperReportUtil.class.getClassLoader().getResourceAsStream(templatePath + ".jasper");
if (jasperStream != null) {
@ -107,7 +109,6 @@ public class JasperReportUtil {
return (JasperReport) JRLoader.loadObject(jasperStream);
} else {
// 不存在jasper,编译jrxml
compileAndSave(templateName);
InputStream jrxmlStream = JasperReportUtil.class.getClassLoader().getResourceAsStream(templatePath + ".jrxml");
if (jrxmlStream == null) {
throw new JRException("模板不存在:" + templatePath + ".jrxml/.jasper");
@ -289,29 +290,28 @@ public class JasperReportUtil {
String jrxmlName = jrxmlFileName + ".jrxml"; // jrxml文件名
String jasperName = jrxmlFileName + ".jasper"; // 编译后文件名
// 2. 获取resources/templates下的jrxml输入流
InputStream jrxmlStream = JasperReportUtil.class
.getClassLoader()
.getResourceAsStream(templatep + jrxmlName);
if(needCompile(TEMPLATE_PATH+jrxmlName, TEMPLATE_PATH+jasperName)) {
// 2. 获取resources/templates下的jrxml输入流
InputStream jrxmlStream = JasperReportUtil.class
.getClassLoader()
.getResourceAsStream(templatep + jrxmlName);
if (jrxmlStream == null) {
throw new Exception("未找到jrxml模板:" + templatep + jrxmlName);
}
// 3. 编译jrxml为JasperReport对象
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlStream);
if (jrxmlStream == null) {
throw new Exception("未找到jrxml模板:"+templatep + jrxmlName);
// 4. 确定输出目录(resources/templates的同级目录,即classes/templates)
File outputDir = getTemplateOutputDir();
if (!outputDir.exists()) {
outputDir.mkdirs(); // 目录不存在则创建
}
// 5. 输出jasper文件到目标目录
File jasperFile = new File(outputDir, jasperName);
JRSaver.saveObject(jasperReport, jasperFile); // 保存编译结果
System.out.println("编译成功:" + jasperFile.getAbsolutePath());
}
// 3. 编译jrxml为JasperReport对象
JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlStream);
// 4. 确定输出目录(resources/templates的同级目录,即classes/templates)
File outputDir = getTemplateOutputDir();
if (!outputDir.exists()) {
outputDir.mkdirs(); // 目录不存在则创建
}
// 5. 输出jasper文件到目标目录
File jasperFile = new File(outputDir, jasperName);
JRSaver.saveObject(jasperReport, jasperFile); // 保存编译结果
System.out.println("编译成功:" + jasperFile.getAbsolutePath());
}
/**
@ -330,5 +330,57 @@ public class JasperReportUtil {
// 返回templates目录的File对象
return new File(templateUrl.toURI());
}
/**
* 检查是否需要编译jrxml(jrxml修改时间晚于jasper时需要编译)
* @param jrxmlPath jrxml文件路径(如"src/main/resources/templates/barcode.jrxml")
* @param jasperPath jasper文件路径(如"src/main/resources/templates/barcode.jasper")
* @return true:需要编译;false:无需编译
*/
public static boolean needCompile(String jrxmlPath, String jasperPath) {
// 1. 判断jasper文件是否存在:不存在则需要编译
File jasperFile = new File(jasperPath);
if (!jasperFile.exists()) {
log.info("jasper文件不存在,需要编译{}",jasperPath);
return true;
}
// 2. 判断jrxml文件是否存在
File jrxmlFile = new File(jrxmlPath);
if (!jrxmlFile.exists()) {
throw new IllegalArgumentException("jrxml文件不存在:" + jrxmlPath);
}
// 3. 比较修改时间:jrxml比jasper新 → 需要编译
long jrxmlTime = jrxmlFile.lastModified();
long jasperTime = jasperFile.lastModified();
boolean needCompile = jrxmlTime > jasperTime;
if (needCompile) {
log.info("jrxml已修改(" + jrxmlTime + "),晚于jasper(" + jasperTime + "),需要编译");
} else {
// log.info("jasper文件最新,无需编译");
}
return needCompile;
}
/**
* 获取 resources/templates/ 目录的绝对路径
* @return 目录路径(如 "D:/project/target/classes/templates/")
*/
public static String getTemplatePath() {
try {
// 通过类加载器获取 templates 目录的URL
URL templateUrl = JasperReportUtil.class.getClassLoader().getResource(template);
// 如果URL为空,说明目录不存在,但仍返回理论路径
if (templateUrl == null) {
URL resourcesUrl = JasperReportUtil.class.getClassLoader().getResource("");
return resourcesUrl.toURI().getPath() + templatep;
}
// 返回实际路径
return templateUrl.toURI().getPath()+"/";
} catch (URISyntaxException e) {
throw new RuntimeException("获取路径失败", e);
}
}
}