Tuesday, April 20, 2010

Print Sales Invoice Report in PDF / HTML / RTF / ASCII etc., format through code

I have observed that lot of developers face difficulty in printing the Sales Invoice report into file system through code. Here is the code snippet for it.

SalesFormLetter salesFormLetter;
PrintJobSettings printJobSettings;
CustInvoiceJour custInvoiceJour;
SalesId salesId;
salesid = "SO-100010";
salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice,false);
printJobSettings = new PrintJobSettings();
printJobSettings.fileName( '//' + salesId+#pdf );
printJobSettings.warnIfFileExists(false); SalesFormLetter.updatePrinterSettingsFormLetter(printJobSettings.packPrintJobSettings());
select custInvoiceJour where custInvoiceJour.SalesId == salesId;

In the above Code, I have tried to print a Sales Invoice Report in pdf format. In the same way by changing the extension you can print the same report in all valid formats like html, rtf, ascii etc.,


Stefan said...

Hello Mr. Giridhar Raj, I'd like to print sales invoice from code too. But i have little problem with look of the invoice in PDF format. Problem is that pdf invoce has approximaty 5mm border compare to the printed look of the same sales invoice report. Same problem i have with print to PDF from AX form.
So, is it possible to change setting of PDF converter in AX?

Anonymous said...

There could be a problem with overwriting Target parameter, maybe this would help: http://mariusdax.blogspot.com/2010/08/printing-existing-sales-invoice-into.html

Stewart89 said...

Hi, Im using above code to save our payslip as PDF, and it is working like a charm. But for security reason I would like to password protect the PDF files, how is this possible ?