diff --git a/SGGL/BLL/Common/FastReport.cs b/SGGL/BLL/Common/FastReport.cs
index 9ecf8a8b..e3667678 100644
--- a/SGGL/BLL/Common/FastReport.cs
+++ b/SGGL/BLL/Common/FastReport.cs
@@ -1,12 +1,19 @@
-using FastReport;
+using FastReport;
+using FastReport.Export.Pdf;
+using System;
using System.Collections.Generic;
using System.Data;
+using System.IO;
+using System.Text;
+using System.Timers;
using System.Web;
namespace BLL
{
public static class FastReportService
{
+ private static Timer tempReportCleanupTimer;
+
///
/// 重置数据
///
@@ -32,6 +39,159 @@ namespace BLL
}
+ ///
+ /// 启动临时报表清理定时器,定期删除超过 1 小时的 PDF。
+ ///
+ public static void StartTempReportCleanupMonitor()
+ {
+ if (tempReportCleanupTimer != null)
+ {
+ tempReportCleanupTimer.Stop();
+ tempReportCleanupTimer.Dispose();
+ tempReportCleanupTimer = null;
+ }
+
+ CleanExpiredTempReports();
+ tempReportCleanupTimer = new Timer
+ {
+ AutoReset = true,
+ Interval = 1000 * 60 * 30
+ };
+ tempReportCleanupTimer.Elapsed += TempReportCleanupTimer_Elapsed;
+ tempReportCleanupTimer.Start();
+ }
+
+ private static void TempReportCleanupTimer_Elapsed(object sender, ElapsedEventArgs e)
+ {
+ CleanExpiredTempReports();
+ }
+
+ ///
+ /// 删除 File/Fastreport/Temp 下创建时间超过 1 小时的 PDF 临时文件。
+ ///
+ public static void CleanExpiredTempReports()
+ {
+ try
+ {
+ string tempDirectory = Path.Combine(Funs.RootPath, @"File\Fastreport\Temp\");
+ if (!Directory.Exists(tempDirectory))
+ {
+ return;
+ }
+
+ DateTime expiredTime = DateTime.Now.AddHours(-1);
+ foreach (string file in Directory.GetFiles(tempDirectory, "*.pdf", SearchOption.TopDirectoryOnly))
+ {
+ FileInfo fileInfo = new FileInfo(file);
+ if (fileInfo.CreationTime < expiredTime)
+ {
+ fileInfo.Attributes = FileAttributes.Normal;
+ fileInfo.Delete();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ ErrLogInfo.WriteLog(ex, "FastReport临时报表清理", "FastReportService.CleanExpiredTempReports");
+ }
+ }
+
+ ///
+ /// 导出报表 PDF 到临时目录,并返回可访问的相对 URL。
+ ///
+ /// 报表模板路径,支持绝对路径或相对 Funs.RootPath 的路径。
+ public static string ExportReport(string reportPath)
+ {
+ List dataTables = (List)HttpContext.Current.Session["ReportDataTables"];
+ Dictionary parameterValues = (Dictionary)HttpContext.Current.Session["ReportParameterValues"];
+ return ExportReport(reportPath, dataTables, parameterValues);
+ }
+
+ ///
+ /// 导出报表 PDF 到临时目录,并返回可访问的相对 URL。
+ ///
+ /// 报表模板路径,支持绝对路径或相对 Funs.RootPath 的路径。
+ /// 报表数据源集合。
+ /// 报表参数集合。
+ public static string ExportReport(string reportPath, List dataTables, Dictionary parameterValues)
+ {
+ string fullReportPath = GetReportFullPath(reportPath);
+ if (string.IsNullOrEmpty(fullReportPath) || !File.Exists(fullReportPath))
+ {
+ throw new FileNotFoundException("打印模板不存在!", reportPath);
+ }
+
+ string relativeDirectory = @"File\Fastreport\Temp\";
+ string tempDirectory = Path.Combine(Funs.RootPath, relativeDirectory);
+ if (!Directory.Exists(tempDirectory))
+ {
+ Directory.CreateDirectory(tempDirectory);
+ }
+
+ string fileName = "report_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString("N") + ".pdf";
+ string filePath = Path.Combine(tempDirectory, fileName);
+ using (Report report = BuildPreparedReport(fullReportPath, dataTables, parameterValues))
+ using (PDFExport pdfExport = new PDFExport())
+ {
+ // Web 打印先导出 PDF,前端用 iframe 装载后调用浏览器打印。
+ pdfExport.PrintScaling = false;
+ pdfExport.ShowPrintDialog = true;
+ report.Export(pdfExport, filePath);
+ }
+
+ return "~/" + relativeDirectory.Replace("\\", "/") + fileName;
+ }
+
+ private static Report BuildPreparedReport(string reportPath, List dataTables, Dictionary parameterValues)
+ {
+ FastReport.Utils.Config.WebMode = true;
+ Report report = new Report();
+ report.Load(reportPath);
+ if (report.Dictionary.Connections.Count > 0)
+ {
+ var reportConnection = report.Dictionary.Connections[0];
+ if (reportConnection.ConnectionString != Funs.ConnString)
+ {
+ // 打印时只替换本次报表连接,避免把运行环境连接串写回模板文件。
+ reportConnection.ConnectionString = Funs.ConnString;
+ }
+ }
+
+ if (dataTables != null && dataTables.Count > 0)
+ {
+ for (int i = 0; i < dataTables.Count; i++)
+ {
+ report.RegisterData(dataTables[i], dataTables[i].TableName);
+ }
+ }
+
+ if (parameterValues != null && parameterValues.Count > 0)
+ {
+ foreach (KeyValuePair kvp in parameterValues)
+ {
+ report.SetParameterValue(kvp.Key, kvp.Value);
+ }
+ }
+
+ report.Prepare();
+ return report;
+ }
+
+ private static string GetReportFullPath(string reportPath)
+ {
+ if (string.IsNullOrEmpty(reportPath))
+ {
+ return reportPath;
+ }
+
+ if (Path.IsPathRooted(reportPath))
+ {
+ return reportPath;
+ }
+
+ return Path.Combine(Funs.RootPath, reportPath.TrimStart('\\', '/'));
+ }
+
///
/// 合并报表后打印
///
diff --git a/SGGL/FineUIPro.Web/CLGL/InPlanMaster.aspx.cs b/SGGL/FineUIPro.Web/CLGL/InPlanMaster.aspx.cs
index 3b0232d4..d0f6a212 100644
--- a/SGGL/FineUIPro.Web/CLGL/InPlanMaster.aspx.cs
+++ b/SGGL/FineUIPro.Web/CLGL/InPlanMaster.aspx.cs
@@ -1,4 +1,5 @@
using BLL;
+using FastReport;
using Model;
using System;
using System.Collections.Generic;
@@ -433,8 +434,7 @@ namespace FineUIPro.Web.CLGL
{
BindDetailGrid(Grid1.SelectedRowID);
}
-
- private void Print(string Id)
+ private void Print(string Id)
{
BLL.FastReportService.ResetData();
if (string.IsNullOrEmpty(Id))
diff --git a/SGGL/FineUIPro.Web/CLGL/InputMaster.aspx b/SGGL/FineUIPro.Web/CLGL/InputMaster.aspx
index 19c3dd18..e2c7d0bf 100644
--- a/SGGL/FineUIPro.Web/CLGL/InputMaster.aspx
+++ b/SGGL/FineUIPro.Web/CLGL/InputMaster.aspx
@@ -254,6 +254,54 @@
function reloadGrid() {
__doPostBack(null, 'reloadGrid');
}
+ function printByHiddenFrame(url) {
+ var frameId = 'fastreport-print-frame-' + new Date().getTime();
+ var frame = document.createElement('iframe');
+ frame.id = frameId;
+ frame.name = frameId;
+ frame.style.position = 'fixed';
+ frame.style.left = '-10000px';
+ frame.style.top = '-10000px';
+ frame.style.width = '1px';
+ frame.style.height = '1px';
+ frame.style.border = '0';
+ frame.style.opacity = '0';
+ frame.onload = function () {
+ setTimeout(function () {
+ try {
+ frame.contentWindow.onafterprint = cleanupPrintFrame;
+ frame.contentWindow.focus();
+ frame.contentWindow.print();
+ } catch (e) {
+ window.print();
+ }
+ }, 500);
+ };
+ frame.src = url;
+ document.body.appendChild(frame);
+ var cleaned = false;
+ function cleanupPrintFrame() {
+ if (cleaned) {
+ return;
+ }
+ cleaned = true;
+ if (frame && frame.parentNode) {
+ frame.parentNode.removeChild(frame);
+ }
+ window.removeEventListener('afterprint', cleanupPrintFrame);
+ }
+ window.addEventListener('afterprint', cleanupPrintFrame);
+ setTimeout(cleanupPrintFrame, 300000);
+ }
+ window.addEventListener('message', function (event) {
+ if (!event.data || event.data.type !== 'fastreport-print-finished') {
+ return;
+ }
+ var frame = document.getElementById(event.data.frameId);
+ if (frame && frame.parentNode) {
+ frame.parentNode.removeChild(frame);
+ }
+ });