June B
June B

Reputation: 142

How can I save multiple pages of the PDF version of an Acumatica report to a file programmatically?

I have a report that successfully prints 1 record per page in PDF view when run manually, and exporting it as PDF is also successful.

I need to generate the report programmatically and save it to the SO record. With the code I have, I'm only getting the first page. How can I save all pages to the file?

private IEnumerable ExportReport(PXAdapter adapter, string reportID, Dictionary<String, String> parameters)
    {
        //Press save if the SO is not completed 
        if (Base.Document.Current.Completed == false)
        {
            Base.Save.Press();
        }

        PX.SM.FileInfo file = null;
        using (Report report = PXReportTools.LoadReport(reportID, null))
        {
            if (report == null)
            {
                throw new Exception("Unable to access Acumatica report writer for specified report : " + reportID);
            }

            PXReportTools.InitReportParameters(report, parameters, PXSettingProvider.Instance.Default);
            ReportNode reportNode = ReportProcessor.ProcessReport(report);
            IRenderFilter renderFilter = ReportProcessor.GetRenderer(ReportProcessor.FilterPdf);

            //Generate the PDF
            using (StreamManager streamMgr = new StreamManager())
            {
                renderFilter.Render(reportNode, null, streamMgr);
                UploadFileMaintenance graphUploadFile = PXGraph.CreateInstance<UploadFileMaintenance>();
                file = new PX.SM.FileInfo(reportNode.ExportFileName + ".pdf", null, streamMgr.MainStream.GetBytes());
            }

            //Save the PDF to the SO; if it already exists save as a new version.
            UploadFileMaintenance graph = new UploadFileMaintenance();
            graph.SaveFile(file, FileExistsAction.CreateVersion);
            PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file);
        }

        //Return the info on the file
        return adapter.Get();
    }

I have also tried generating the PDF using this code:

//Generate the PDF
            byte[] data = PX.Reports.Mail.Message.GenerateReport(reportNode, ReportProcessor.FilterPdf).First();
            file = new PX.SM.FileInfo(reportNode.ExportFileName + ".pdf", null, data);  

And it appears GenerateReport is returning an IList, so I'm guessing each page is a separate list item. What is the proper way to combine them and save as PX.SM.FileInfo?

Upvotes: 0

Views: 280

Answers (1)

June B
June B

Reputation: 142

As suggested in the last comment by HB_ACUMATICA, the problem lay in the parameters being passed. I had the parameter misnamed in my code so it was coming through to the report blank. Thank you for the point in the correct direction!

Upvotes: 1

Related Questions