Hi Everyone,
In this article I want to show you the way how you can dynamically generate PDF file from any report of Acumatica and attach if to an entity.
I will do that on example of printing AR Invoice Form out of AR Invoice with custom button
In general that task can be split in 4 steps:
- Defining reports parameters
- Creation and processing report
- Get report as PDF file
- Attach report to entity
All reports have different parameter so before writing a code, check what parameters are required in that report. All parameter should be provided as dictionary – name/value.
Report
To work with reports you need a reference to PX.Reports.dll, so make sure you have added it before compiling code below. To work with reports you can use PX.Reports.Controls.Report class there.
PDF
Acumatica generated PDF in unattended mode mostly for sending reports by email, so here we are going to reuse some of the mailing features:
PX.Reports.Mail.Message.GenerateReport(reportNode, ReportProcessor.FilterPdf) – will create a PDF file based on generated report.
Attaching File
In general last step is optional and if you just want send report though email or save it somewhere you can skip it and do whatever you need, but in my case I just want to attach report to entity to not loose it and see result.
By the way you also can send this file for downloading right from memory (without saving to DB) by usage of PXRedirectToFileException with InMemory parameter and saving file to session by FileID
FileInfo file = new FileInfo(Guid.NewGuid(), "report.pdf", null, data); PXContext.SessionTyped<PXSessionStatePXData>().FileInfo[file.UID.ToString()] = file; throw new PXRedirectToFileException(file.UID, 0, true, true);
As a result you have report:
Full code snippet you can find here:
public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry> { #regionEvent Handlers public PXAction<PX.Objects.AR.ARInvoice> CreatePDF; [PXButton(CommitChanges = true)] [PXUIField(DisplayName = "Create PDF")] protected void createPDF() { //Report Paramenters Dictionary<String, String> parameters = new Dictionary<String, String>(); parameters["ARInvoice.DocType"] = Base.Document.Current.DocType; parameters["ARInvoice.RefNbr"] = Base.Document.Current.RefNbr; //Report Processing PX.Reports.Controls.Report _report = PXReportTools.LoadReport("AR641000",null); PXReportTools.InitReportParameters(_report, parameters, SettingsProvider.Instance.Default); ReportNode reportNode = ReportProcessor.ProcessReport(_report); //Generation PDF byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode, ReportProcessor.FilterPdf).First(); FileInfo file = new FileInfo("report.pdf", null, data); //Saving report UploadFileMaintenance graph = new UploadFileMaintenance(); graph.SaveFile(file); PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file); //Downloading of the report throw new PXRedirectToFileException(file, true); } #endregion }
Have a nice reporting!
Hi Sergey,
How can i redirect to the file (open it in browser) without downloading it? Im using version 2019 R1.
Thanks.
Hi John, something like this may work for you ViewRevisions;
public PXAction
[PXUIField(DisplayName = ActionsMessages.Version)]
[PXButton]
protected IEnumerable viewRevisions(PXAdapter a)
{
UploadFile file = this.Files.Current;
if (file == null)
return a.Get();
WikiFileMaintenance maint = PXGraph.CreateInstance();(file.Name);
maint.Files.Current = maint.Files.Search
throw new PXRedirectRequiredException(maint, “View Revisions”);
}
Julio,
Make sure you are using correct version of .NET. You need to have 4.7.1 as .net not always can have reference from older version library to newer.
Hello, when add reference, I get this message:
Validating Binary Files
PX.Reports.dll Failed to resolve method reference: System.Void PX.Reports.Barcode.QR.MatrixStatus[0…,0…]::.ctor(System.Int32,System.Int32) declared in PX.Reports.dll
PX.Reports.dll Failed to resolve method reference: PX.Reports.Barcode.QR.MatrixStatus PX.Reports.Barcode.QR.MatrixStatus[0…,0…]::Get(System.Int32,System.Int32) declared in PX.Reports.dll
PX.Reports.dll Failed to resolve method reference: System.Void PX.Reports.Barcode.QR.MatrixStatus[0…,0…]::Set(System.Int32,System.Int32,PX.Reports.Barcode.QR.MatrixStatus) declared in PX.Reports.dll
Validation failed.
Hi Julio,
This PX.Reports.dll is included in Acumatica. Add reference to Acumatica Web Site/bin folder. It is located there.
I mean this:
Report
To work with reports you need a reference to PX.Reports.dll, so make sure you have added it before compiling code below. To work with reports you can use PX.Reports.Controls.Report class there.
Hi Unknown, i'm really sorry but I haven't fully understood your question. What libraries do you mean?
Hello, sorry, do you have the 2 libraries that are needed to generate this? Thanks and greetings.