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.
generate PDF file acumatica
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.

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.

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:
report PDF file acumatica

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,
              ReportNode reportNode = ReportProcessor.ProcessReport(_report);

              //Generation PDF
              byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode,
              FileInfo file = new FileInfo("report.pdf", null, data);

              //Saving report
              UploadFileMaintenance graph = new UploadFileMaintenance();
              PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file);

              //Downloading of the report
              throw new PXRedirectToFileException(file, true);

Have a nice reporting!

No comments: