Using Dynamics NAV Web Services to send a pdf to a Web Service, in this case for a Web Portal.

Using Dynamics NAV Web Services to send a pdf to a Web Service, in this case for a Web Portal.

IF THIS POST IS USEFUL AND HELPED YOU KINDLY LIKE AND SHARE THIS PAGE SO OTHERS CAN FIND IT, THANK YOU. NOTE: Click on image for a clearer view.

I have always wanted to do this and now that I know how I want to share it with the world! 🙂 I drew inspiration from here:

http://www.candyworm.com/post/Transferring-a-file-using-NAV-Web-Services-and-NET-Interop

The basic steps are:

  1. Create a function that will run a NAV Report.
  2. Save this Report as a PDF.
  3. Stream the PDF into a Byte Array.
  4. Return the Byte Array.
  5. In Visual Studio return the Byte Array to a file format (.pdf).
  6. Save the PDF into a directory, and open it.

Our variables:

Here is the function called CustomerTop10 in our codeunit:

// This section gives us a temporary file name in a temporary location and saves our ‘Customer Top 10’ as PDF to this path
tempFile := Path.GetTempPath() + Path.GetRandomFileName();

IF NOT CustomerTop10.SAVEASPDF(tempFile) THEN ERROR(‘Could not save file %1’,tempFile);

// This section opens the saved PDF.
FileMode := 4;
FileAccess := 1;
FileStream := _File.Open(tempFile, FileMode, FileAccess);

// This section creates a MemoryStream and reads the contents of the file to its buffer.
// Next, this byte buffer is converted to Base64 string and added to out return variable.
MemoryStream := MemoryStream.MemoryStream();
MemoryStream.SetLength(FileStream.Length);
FileStream.Read(MemoryStream.GetBuffer(), 0, FileStream.Length);
exitString.ADDTEXT(Convert.ToBase64String(MemoryStream.GetBuffer()));

// And finally we have to dispose all the resources.
MemoryStream.Close();
MemoryStream.Dispose();
FileStream.Close();
FileStream.Dispose();
_File.Delete(tempFile);

Create a web Service in NAV for the Codeunit:

Here is our visual studio code:

 

And our file is saved to the location above:

Have fun!!

Codeunit 50003 here:

50003-Codeunit

Leave a Reply

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

%d bloggers like this: