Print Sales Invoice to PDF file from code

You can print an existing Sales invoice to a PDF file named as SalesInvoice_<InvoiceId>.pdf through the following code, regardless of how the Print Management setup of Sales Invoice might look like:

4 thoughts on “Print Sales Invoice to PDF file from code

  1. Hi,

    For Email, we have mentioned PrintMediumType as Email_DC.

    How to get all defaults value from PrintManagement (Docentric) setup for Email setting?
    emailPrintDestSettings should get it from Print management. Here system is allowing only when giving all details.
    .
    VendInvoiceDocumentController vendInvoiceController;
    VendInvoiceDocumentContract vendInvoiceContract;
    VendInvoiceJour vendInvoiceJour;
    SRSPrintDestinationSettings srsPrintDestSettings;
    DocPrintDestSettingsEmail emailPrintDestSettings;

    vendInvoiceController = new VendInvoiceDocumentController();
    vendInvoiceController.parmArgs(args);
    vendInvoiceController.parmShowDialog(false);
    vendInvoiceController.parmLoadFromSysLastValue(false);
    vendInvoiceController.parmReportName(
    PrintMgmtDocType::construct(PrintMgmtDocumentType::PurchaseOrderInvoice).getDefaultReportFormat());
    vendInvoiceContract = vendInvoiceController.parmReportContract().parmRdpContract();
    vendInvoiceContract.parmRecordId(invoiceJour.RecId);
    vendInvoiceContract.parmDocumentTitle(“@SYS99734″);
    vendInvoiceContract.parmPrintCopyOriginal(PrintCopyOriginal::OriginalPrint);
    vendInvoiceController.parmReportContract().parmRdpContract(vendInvoiceContract);
    srsPrintDestSettings = new SRSPrintDestinationSettings();
    srsPrintDestSettings.printMediumType(SRSPrintMediumType::Email_DC);
    srsPrintDestSettings.fileFormat(SRSReportFileFormat::PDF);
    srsPrintDestSettings.overwriteFile(true);
    str filename = strfmt(‘CN-%1.pdf’,invoiceJour.InvoiceId);
    srsPrintDestSettings.fileName(filename);
    emailPrintDestSettings =new DocPrintDestSettingsEmail ();
    emailPrintDestSettings.parmEmailAttachmentName(filename);
    emailPrintDestSettings.parmEmailAttachmentFileFormat(DocOutputFileFormat::PDF);
    emailPrintDestSettings.parmEmailFrom(‘from address’);
    emailPrintDestSettings.parmEmailTo(‘to address’);
    emailPrintDestSettings.parmEmailSubject(‘Auto docentric email subject’);
    emailPrintDestSettings.parmEmailBody(”);
    srsPrintDestSettings.parmEmailPrintDestSettings_DC(emailPrintDestSettings);
    vendInvoiceController.parmReportContract().parmPrintSettings(srsPrintDestSettings);
    vendInvoiceController.startOperation();

    Thanks,

    1. For a precise answer we would need to know your specific use case.

      If you just want to send VendInvoiceJour report from code using the Print management setting, the easiest way is to invoke output menu item PurchInvoice with selected VendInvoiceJour record.

      If you want to load the current (default) Print management settings and change them later use the code below.

      DocPrintMgmtSettingsEntity printMgmtSettingsEntity;

      select firstOnly printMgmtSettingsEntity where
      printMgmtSettingsEntity.DocInstanceDocumentType == PrintMgmtDocumentType::PurchaseOrderInvoice &&
      printMgmtSettingsEntity.DataArea == curExt();

      SRSPrintDestinationSettings srsPrintDestSettings = new SRSPrintDestinationSettings();
      srsPrintDestSettings.unpack(printMgmtSettingsEntity.PrintJobSettings);

      If you need further help on your specific case, please contact support@docentric.com.

  2. Hello,

    I follow the example of code but i did not found  the method parmGeneratedDocFilename of class filePrintDestSettings. So I have error. I tried with filePrintDestSettings.parmFileSystemFilePath(@”C:\Temp\PurchPurchaseOrder.pdf”); but the report is not saved automatically in the path. The code is ignored. The pdf is to be downloaded in path that i should select manually.

    Could you help me please ?

    Thank you in advance

    1. Hi,

      If you have D365FO, this code is not applicable; namely, it’s written for AX 2012. Anyhow, in D365FO cloud environments you cannot save files to local file system; instead, you can use document routing as described here.
      On the other hand, for D365FO on-premises environments there is no such limitation and you can use the similar approach as here; just set up the ‘Save to File system’ option to true and the target folder path to your destination network folder, e.g. \\server\folder.

      Hope that this helped!
      Kind regards,
      Ana

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Docentric respects your privacy. Learn how your comment data is processed >>

Docentric respects your privacy. Learn how your comment data is processed >>