主程序
This commit is contained in:
parent
dfde8dfc82
commit
3530fb58a9
@ -40,7 +40,8 @@ import javax.print.attribute.standard.OrientationRequested;
|
||||
@Component
|
||||
@Slf4j
|
||||
public class JasperReportUtil {
|
||||
|
||||
/**打印模板默认子文件夹*/
|
||||
private static final String templates="templates";
|
||||
/**
|
||||
* 核心打印/导出方法
|
||||
* @param printerName 打印机名称(可为null,null时使用默认打印机)
|
||||
@ -71,7 +72,7 @@ public class JasperReportUtil {
|
||||
) throws Exception {
|
||||
// 1. 在 printbase 中获取配置路径并初始化目录
|
||||
String templateDir = getTemplateDir(); // 获取配置路径
|
||||
String templatePath = templateDir + File.separator;
|
||||
String templatePath = templateDir + File.separator+templates+File.separator;
|
||||
initTemplateDir(templateDir); // 初始化目录(不存在则创建)
|
||||
// 在 printbase 方法开头添加
|
||||
if (pdfType && (exportPath == null || exportPath.trim().isEmpty())) {
|
||||
@ -434,48 +435,6 @@ public class JasperReportUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成PDF报表
|
||||
* @param templatePath JRXML模板文件路径
|
||||
* @param outputPath 生成的PDF文件保存路径
|
||||
* @param parameters 报表参数
|
||||
* @param connection 报表数据源
|
||||
* @throws JRException JasperReports异常
|
||||
*/
|
||||
public static void generatePdf(String templatePath, String outputPath,
|
||||
Map<String, Object> parameters,
|
||||
Connection connection) throws Exception {
|
||||
|
||||
// 检查模板文件是否存在 + 是否为文件(避免误传目录路径)
|
||||
File templateFile = new File(templatePath);
|
||||
if (!templateFile.exists() || !templateFile.isFile()) {
|
||||
throw new IllegalArgumentException("模板文件不存在或路径非法!模板路径:" + templatePath);
|
||||
}
|
||||
File outputFile = new File(outputPath);
|
||||
File outputParentDir = outputFile.getParentFile(); // 获取PDF的父目录(如:C:/output/report.pdf → 父目录是C:/output)
|
||||
|
||||
// 若父目录不存在,则创建(mkdirs()支持创建多级目录,比mkdir()更实用)
|
||||
if (outputParentDir != null && !outputParentDir.exists()) {
|
||||
boolean isDirCreated = outputParentDir.mkdirs();
|
||||
// 校验目录是否创建成功(防止权限不足等问题导致创建失败)
|
||||
if (!isDirCreated) {
|
||||
throw new IOException("PDF输出目录创建失败!请检查路径权限。输出目录:" + outputParentDir.getAbsolutePath());
|
||||
}
|
||||
System.out.println("已自动创建PDF输出目录:" + outputParentDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
// 加载JRXML模板
|
||||
JasperDesign jasperDesign = JRXmlLoader.load(templatePath);;
|
||||
|
||||
// 编译模板
|
||||
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
|
||||
|
||||
// 填充报表数据
|
||||
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
|
||||
|
||||
// 导出为PDF
|
||||
JasperExportManager.exportReportToPdfFile(jasperPrint, outputPath);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -109,16 +109,17 @@ public class ReportUtil {
|
||||
|
||||
public Result printToPdf(BarCodeParam barCodeParam, String pdfName){
|
||||
String templatePath = RuoYiConfig.getProfile()+"/templates/barcode_zy.jrxml"; // 模板文件路径
|
||||
return printToPdf(templatePath,barCodeParam,pdfName,false);
|
||||
return printToPdf("barcode_zy",barCodeParam,pdfName,false);
|
||||
}
|
||||
|
||||
public Result printToPdf(String path,BarCodeParam barCodeParam, String pdfName,boolean base64){
|
||||
public Result printToPdf(String templatename,BarCodeParam barCodeParam, String pdfName,boolean base64){
|
||||
String outputPath = RuoYiConfig.getProfile()+"/config/static/printPDF/"+UUID.randomUUID().toString()+".pdf"; // PDF输出路径(多级目录会自动创建)
|
||||
log.info("打印输入输出地址:{},{}",path,outputPath);
|
||||
// log.info("打印输入输出地址:{},{}",path,outputPath);
|
||||
Map<String, Object> stringObjectMap = BeanUtil.beanToMap(barCodeParam);
|
||||
try {
|
||||
Connection connection = dataSource.getConnection();
|
||||
JasperReportUtil.generatePdf(path,outputPath,stringObjectMap,connection);
|
||||
//JasperReportUtil.generatePdf(templatename,outputPath,stringObjectMap,connection);
|
||||
JasperReportUtil.exportToPdf(templatename,stringObjectMap,connection,outputPath);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.error("打印条码失败,参数{}",barCodeParam,e);
|
||||
@ -127,7 +128,9 @@ public class ReportUtil {
|
||||
log.info("传给前端的pdf地址:{}",httpPath);
|
||||
if(base64){
|
||||
File file = new File(outputPath);
|
||||
return new Result("0","打印成功!",Base64.encode(file));
|
||||
String data=Base64.encode(file);
|
||||
FileUtils.deleteFile(outputPath);
|
||||
return new Result("0","打印成功!",data);
|
||||
}else{
|
||||
return new Result("0","打印成功!",httpPath);
|
||||
}
|
||||
@ -308,7 +311,7 @@ public class ReportUtil {
|
||||
}
|
||||
|
||||
public Result printReport(Date jyrq,String yq,String ybh,String sqh){
|
||||
String hospitalName = commonUtil.getComDictNameById("HOS", "1");
|
||||
String hospitalName = commonUtil.getCompany(commonUtil.getLabInstr(yq).getYljg());//commonUtil.getComDictNameById("HOS", "1");
|
||||
BarCodeParam barCodeParam = new BarCodeParam();
|
||||
barCodeParam.setJyrq(jyrq);
|
||||
barCodeParam.setYq(yq);
|
||||
@ -316,7 +319,8 @@ public class ReportUtil {
|
||||
barCodeParam.setSqh(sqh);
|
||||
barCodeParam.setHospitalName(hospitalName);
|
||||
String templatePath = RuoYiConfig.getProfile()+"/templates/report_ST.jrxml"; // 报告模板文件路径
|
||||
return printToPdf(templatePath,barCodeParam,UUID.randomUUID().toString(),true);
|
||||
//return printToPdf(templatePath,barCodeParam,UUID.randomUUID().toString(),true);
|
||||
return printToPdf("report_ST",barCodeParam,UUID.randomUUID().toString(),true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user