Wednesday, 10 May 2017

Get PDF file from Report using Code

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
Parameters
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, truetrue);

As a result you have report:

Full code snippet you can find here:

public class ARInvoiceEntry_Extension : PXGraphExtension<ARInvoiceEntry>
{
       #region Event 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!

No comments: