以编程方式生成报告
Generate Report Programmatically
假设我使用 Acumatica Report Designer 创建了一份报告,并且它链接到相关的 DAC。
我还有一个带有操作的自定义屏幕。当用户执行此操作时,我想生成报告,以便用户将其下载为 pdf。
通过 Acumatica 以编程方式生成 PDF 报告的方法是什么 API?
请参阅下面的代码片段,以了解如何以编程方式生成报告作为 PDF 文件并将 SaveFileDialog 显示到 download/save 生成的 PDF 的示例:
public PXAction<IOITInboundTestWorkOrder> GenerateReportAndRedirectToFile;
[PXButton]
[PXUIField(DisplayName = "Generate Report and Download as PDF")]
protected void generateReportAndSaveToDB()
{
Actions.PressSave();
PXLongOperation.StartOperation(this, () =>
{
PX.SM.FileInfo file = null;
using (Report report = PXReportTools.LoadReport("SO641012", null))
{
var orderNbr = ITWO.Current.OrderNbr;
if (report == null) throw new Exception("Unable to access Acumatica report writter for specified report : " + "SO641012");
Dictionary<string, string> prams = new Dictionary<string, string>();
prams["ITWONbr"] = orderNbr;
PXReportTools.InitReportParameters(report, prams, PXSettingProvider.Instance.Default);
ReportNode repNode = ReportProcessor.ProcessReport(report);
IRenderFilter renderFilter = ReportProcessor.GetRenderer(ReportProcessor.FilterPdf);
using (StreamManager streamMgr = new StreamManager())
{
renderFilter.Render(repNode, null, streamMgr);
string fileName = string.Format("Inbound Test Work Order #{0}.pdf", orderNbr);
file = new PX.SM.FileInfo(fileName, null, streamMgr.MainStream.GetBytes());
}
}
if (file != null)
{
throw new PXRedirectToFileException(file, true);
}
});
}
由于我使用的是 REST API 并且由于某种原因 PXRedirectToFileException 确实在那里工作(它没有 return 位置 header,所以我无法接受答案.我想出了这个非常笨拙的解决方案,它导致文件 URL 在异常中暴露。
using SiteStatus = PX.Objects.IN.Overrides.INDocumentRelease.SiteStatus;
using System.Linq;
using PX.Common;
using CRLocation = PX.Objects.CR.Standalone.Location;
using PX.Objects.AR.CCPaymentProcessing;
using PX.Objects.AR.CCPaymentProcessing.Common;
using PX.Objects.AR.CCPaymentProcessing.Helpers;
using PX.Objects.Common;
using PX.Objects;
using PX.Objects.SO;
using PX.Reports;
using PX.Reports.Data;
using PX.Data.Reports;
using PX.SM;
namespace PX.Objects.SO
{
public class SOInvoiceEntry_Extension:PXGraphExtension<SOInvoiceEntry>
{
#region Event Handlers
protected virtual void ARInvoice_RowSelected(PXCache cache, PXRowSelectedEventArgs e){
CreateInvoicePDF.SetEnabled(true);
}
public PXAction<ARInvoice> CreateInvoicePDF;
[PXButton]
[PXUIField(DisplayName = "Create Invoice PDF", Enabled = true, Visible = false)]
public virtual void createInvoicePDF()
{
//Report Paramenters
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters["DocType"] = Base.Document.Current.DocType;
parameters["RefNbr"] = Base.Document.Current.RefNbr;
//Report Processing
PX.Reports.Controls.Report _report = PXReportTools.LoadReport("SO643000",null);
PXReportTools.InitReportParameters(_report, parameters, SettingsProvider.Instance.Default);
ReportNode reportNode = ReportProcessor.ProcessReport(_report);
// Generate PDF
byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode, ReportProcessor.FilterPdf).First();
FileInfo file = new FileInfo(Guid.NewGuid(), "Invoice" + Base.Document.Current.RefNbr + ".pdf", null, data);
// Store data in session
PXContext.SessionTyped<PXSessionStatePXData>().FileInfo[file.UID.ToString()] = file;
// Include file URL in exception. The client will parse the filname and fetch the URL in a subsequent request.
PXRedirectToFileException e = new PXRedirectToFileException(file.UID, 0, true, true);
string url = e.Url;
throw new FileUrlException(url);
}
#endregion
}
class FileUrlException : PXException {
public FileUrlException(string message) : base(message) {
}
}
}
缺点是此操作只能通过 API 使用。
我将操作添加到 Web 服务端点。在另一端,我使用正则表达式从异常消息中提取字符串并执行获取请求以获取文件。
如果您不想处理公开 URL,可接受的方法似乎是调用一种方法将文件存储到 Acumatica 记录,然后通过 API: Get report output in PDF fromat via Acumatica REST API
假设我使用 Acumatica Report Designer 创建了一份报告,并且它链接到相关的 DAC。
我还有一个带有操作的自定义屏幕。当用户执行此操作时,我想生成报告,以便用户将其下载为 pdf。
通过 Acumatica 以编程方式生成 PDF 报告的方法是什么 API?
请参阅下面的代码片段,以了解如何以编程方式生成报告作为 PDF 文件并将 SaveFileDialog 显示到 download/save 生成的 PDF 的示例:
public PXAction<IOITInboundTestWorkOrder> GenerateReportAndRedirectToFile;
[PXButton]
[PXUIField(DisplayName = "Generate Report and Download as PDF")]
protected void generateReportAndSaveToDB()
{
Actions.PressSave();
PXLongOperation.StartOperation(this, () =>
{
PX.SM.FileInfo file = null;
using (Report report = PXReportTools.LoadReport("SO641012", null))
{
var orderNbr = ITWO.Current.OrderNbr;
if (report == null) throw new Exception("Unable to access Acumatica report writter for specified report : " + "SO641012");
Dictionary<string, string> prams = new Dictionary<string, string>();
prams["ITWONbr"] = orderNbr;
PXReportTools.InitReportParameters(report, prams, PXSettingProvider.Instance.Default);
ReportNode repNode = ReportProcessor.ProcessReport(report);
IRenderFilter renderFilter = ReportProcessor.GetRenderer(ReportProcessor.FilterPdf);
using (StreamManager streamMgr = new StreamManager())
{
renderFilter.Render(repNode, null, streamMgr);
string fileName = string.Format("Inbound Test Work Order #{0}.pdf", orderNbr);
file = new PX.SM.FileInfo(fileName, null, streamMgr.MainStream.GetBytes());
}
}
if (file != null)
{
throw new PXRedirectToFileException(file, true);
}
});
}
由于我使用的是 REST API 并且由于某种原因 PXRedirectToFileException 确实在那里工作(它没有 return 位置 header,所以我无法接受答案.我想出了这个非常笨拙的解决方案,它导致文件 URL 在异常中暴露。
using SiteStatus = PX.Objects.IN.Overrides.INDocumentRelease.SiteStatus;
using System.Linq;
using PX.Common;
using CRLocation = PX.Objects.CR.Standalone.Location;
using PX.Objects.AR.CCPaymentProcessing;
using PX.Objects.AR.CCPaymentProcessing.Common;
using PX.Objects.AR.CCPaymentProcessing.Helpers;
using PX.Objects.Common;
using PX.Objects;
using PX.Objects.SO;
using PX.Reports;
using PX.Reports.Data;
using PX.Data.Reports;
using PX.SM;
namespace PX.Objects.SO
{
public class SOInvoiceEntry_Extension:PXGraphExtension<SOInvoiceEntry>
{
#region Event Handlers
protected virtual void ARInvoice_RowSelected(PXCache cache, PXRowSelectedEventArgs e){
CreateInvoicePDF.SetEnabled(true);
}
public PXAction<ARInvoice> CreateInvoicePDF;
[PXButton]
[PXUIField(DisplayName = "Create Invoice PDF", Enabled = true, Visible = false)]
public virtual void createInvoicePDF()
{
//Report Paramenters
Dictionary<String, String> parameters = new Dictionary<String, String>();
parameters["DocType"] = Base.Document.Current.DocType;
parameters["RefNbr"] = Base.Document.Current.RefNbr;
//Report Processing
PX.Reports.Controls.Report _report = PXReportTools.LoadReport("SO643000",null);
PXReportTools.InitReportParameters(_report, parameters, SettingsProvider.Instance.Default);
ReportNode reportNode = ReportProcessor.ProcessReport(_report);
// Generate PDF
byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode, ReportProcessor.FilterPdf).First();
FileInfo file = new FileInfo(Guid.NewGuid(), "Invoice" + Base.Document.Current.RefNbr + ".pdf", null, data);
// Store data in session
PXContext.SessionTyped<PXSessionStatePXData>().FileInfo[file.UID.ToString()] = file;
// Include file URL in exception. The client will parse the filname and fetch the URL in a subsequent request.
PXRedirectToFileException e = new PXRedirectToFileException(file.UID, 0, true, true);
string url = e.Url;
throw new FileUrlException(url);
}
#endregion
}
class FileUrlException : PXException {
public FileUrlException(string message) : base(message) {
}
}
}
缺点是此操作只能通过 API 使用。 我将操作添加到 Web 服务端点。在另一端,我使用正则表达式从异常消息中提取字符串并执行获取请求以获取文件。
如果您不想处理公开 URL,可接受的方法似乎是调用一种方法将文件存储到 Acumatica 记录,然后通过 API: Get report output in PDF fromat via Acumatica REST API