Nov 30

Thanks to Microsoft Reporting Services PDF Rendering Extension, all RTC reports can be exported to PDF. At least that is benefit coming back to us, while struggling with the report creations (read post from Alex Chow: How to define Set/GetData).

In order to get the PDF, that gets generated on the service tier, available on the client tier, we need to copy the file from the server to the client tier. The Dynamics NAV team blog have posted a couple interesting articles about how to move files between the tiers. In this article we will put it all together in an easy to understand example that will give you emailing capabilities with PDF attachments.

First off we need the helper function to move the file from server tier to client tier, it looks like this:

DownloadToClientFileName2(FileToDownload : Text[250];ToFile : Text[250]) : Text[250]
IF NOT ISSERVICETIER THEN
  EXIT;

FileVar.OPEN(FileToDownload);
FileVar.CREATEINSTREAM(IStream);
DOWNLOADFROMSTREAM(IStream,'','<TEMP>','',MagicPath);
FileVar.CLOSE;

CREATE(FileSystemObject,TRUE,TRUE);
IF FileSystemObject.FileExists(ToFile) THEN
  FileSystemObject.DeleteFile(ToFile,TRUE);
FileSystemObject.CopyFile(MagicPath,ToFile);
FileSystemObject.DeleteFile(MagicPath,TRUE);
EXIT(ToFile);

FileSystemObject is an automation control of type ‘Microsoft Scripting Runtime’.FileSystemObject.

On the actual page we will add a new Action to allow us to send the PDF file by E-mail. Open the Page Designer and insert a new action item and add this code:

<Action1000000000> - OnAction()
CurrPage.SETSELECTIONFILTER(Rec);
Name := STRSUBSTNO('Sales Invoice No. %1.pdf', "No.");
ToFile := Name;
FileName := TEMPORARYPATH + ToFile;
REPORT.SAVEASPDF(REPORT::"Sales Invoice", FileName, Rec);
ToFile := AppMgmt.DownloadToClientFileName(FileName, ToFile);
Mail.NewMessage('','',Name,'',ToFile,TRUE);
FILE.ERASE(FileName);

In the example i placed the helper function in codeunit 1 Application Mgmt. Now just start the RTC, open a Posted Sales Invoice document and click on your action and you should get a new email message with the attachment.

References:
Lars Lohndorf-Larsen: Magicpath
Claus Lundstrøm: Send email with PDF attachment

As you can see from the above posts, the DownloadToClientFileName() function is changed to use the FileSystemObject. Again it is kind of embarrasing that the original post was done in October of 2009, and i am just getting to it now. Hard to keep up with all the new technologies. Anyways, i hope you get your eyes open to this and enjoy it.

Oh, before i forget. Using the standard Mail and SMTP Mail codeunits does not create your emails with your default signature etc., but if you click on the E-Mail icon in the RTC it actually does – unfortunately you cant programatically control this and add attachments (yet!).

And thanks to Aaron Thomas for reminding me of this functionality today.

3 Responses to “Send Dynamics NAV RTC reports and documents as PDF”

  1. Oh, and in case you just wanted the PDF to show on your screen you would replace the last 2 lines with:

    HYPERLINK(FileName);

    That would open the PDF on the client tier.

  2. Bayu Septian says:

    what Data type of variabel FileVar?
    thx before

Leave a Reply

preload preload preload
pornpants.com pornofri.com kilporn.com