Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Finance forum / Print report via x++
Finance forum
Answered

Print report via x++

Posted on by Microsoft Employee

Hey - i am trying to print a report to a PDF file in a local directory.

There are many working examples for AX2012 and if you copy the code everything is working except that ax downloads the PDF in the Browser instead of saving a PDF in a directory.

My goal is to stream a SalesInvoice Report via webserivce.

What we used to do in 2012:

1) generate the Report and save it t c:\temp\xyz.pdf

2) read the file and pass the content to the AIF

3) delete the file

The used code is not working anymore in AX 365 - it keeps telling me that the file does not exist.

So I created a runnable class:

class TestPrintJob
{        
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        Args                            args;

        ContactPerson                   contactPerson;
        SrsReportRunController          controller;
        SRSPrintDestinationSettings     srsPrintDestinationSettings;
        CustTable                       custTable;
        CustInvoiceJour                 custInvoiceJour = custInvoiceJour::findRecId(5637163320);
        SalesTable                      salesTable;
        SalesInvoiceContract            rdpContract;
        System.Byte[]                   pdfDocBuffer;
        System.IO.FileInfo              fi_pdfDoc;
        System.IO.FileStream            fs;
        guid                            myGuid;
        str                             pdfString = '',
                                        savedFile,
                                        filePath,
                                        myGuidStr,
                                        reportPath;


        if (custInvoiceJour.RecId != 0)
        {

            myGuid = newguid();
            myGuidStr = guid2str(myGuid);

            controller = new SrsReportRunController();

            controller.parmReportName(ssrsReportStr(SalesInvoice, Report));
            controller.parmExecutionMode(SysOperationExecutionMode::Synchronous);
            controller.parmShowDialog(false);
            controller.objectOnServer();

            rdpContract  = new SalesInvoiceContract();
            rdpContract.parmRecordId(custInvoiceJour.RecId);
            rdpContract.parmCountryRegionISOCode(SysCountryRegionCode::countryInfo());
            rdpContract.parmDocumentTitle("@SYS14204");

            // (1) Try by passing args
            args = new Args();
            args.record(custInvoiceJour);
            args.parmEnum(PrintCopyOriginal::OriginalPrint);
            args.parmEnumType(enumNum(PrintCopyOriginal));

            controller.parmReportContract().parmRdpContract(rdpContract);
            controller.parmArgs(args);

            // Change print settings as needed
            srsPrintDestinationSettings = controller.parmReportContract().parmPrintSettings();
            srsPrintDestinationSettings.printMediumType(SRSPrintMediumType::Custom);
            srsPrintDestinationSettings.fileFormat(SRSReportFileFormat::PDF);
            srsPrintDestinationSettings.overwriteFile(true);

            filePath = @'c:\tmp\InvoiceReport_' + myGuidStr + '.pdf';

            srsPrintDestinationSettings.fileName(filePath);

            controller.startOperation();

        }

    }

}


If I run the job AX is rendering the Report and it gets downloaded instantly while C:\tmp stays empty.

I cant figure out why the old way is not working anymore.

Is there any way to save the report to a file or in a string?

Thanks in advance - Alex

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Print report via x++

    Hey Martin - the solution with the memory stream seems to be a faster approach.  Thanks anyways.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Print report via x++

    Wow - thank you! I already tried it and it seems to work fine.

  • Martin Dráb Profile Picture
    Martin Dráb 225,783 Super User on at
    RE: Print report via x++

    You could create a custom controller which would send the file to a web service instead of returning it to the user.

    Note that AIF doesn't exist anymore.

  • Verified answer
    Mea_ Profile Picture
    Mea_ 60,274 on at
    RE: Print report via x++

    Hi Alex,

    You can print report to memory stream or byte array and then send it, here is an example http://www.meritsolutions.com/render-report-memory-stream-d365-aka-ax7/

    So you can avoid extra step to save file and then read it. 

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,148 on at
    RE: Print report via x++

    D365FO in the cloud has no way of seeing your local directories. So it's not possible to print to a local directory.

    For sure you may be able to print to a local directory on the VM but what would be the business value? In prod, you can't access it anyway.

    Unless you are working with the on premises version of D365FO. Is that the case?

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,459 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,783 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans