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;
#File
salesid = "SO-100010";
salesFormLetter = SalesFormLetter::construct(DocumentStatus::Invoice,false);
printJobSettings = new PrintJobSettings();
printJobSettings.setTarget(PrintMedium::File);
printJobSettings.format(PrintFormat);
printJobSettings.fileName( '//10.0.57.22/AOSPrintShare//' + salesId+#pdf );
printJobSettings.warnIfFileExists(false); SalesFormLetter.updatePrinterSettingsFormLetter(printJobSettings.packPrintJobSettings());
select custInvoiceJour where custInvoiceJour.SalesId == salesId;
custInvoiceJour.printJournal(salesFormLetter);

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.,

3 comments:

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 ?