Friday, September 22, 2017

How to print a remote file from NAV


I have seen couple of times the requirement where we need to print a document from NAV which is not a report, it could be a marketing campaign letter, sales sheets or any other document.

Most of the time we upload these kind of documents on a website/remote site, so if we need print those documents with every invoice or any other statement, below is the function you can use from NAV.

In the below example I just took a random PDF URL from the web and used it for testing.

The key in this function is to download the document from the web using WebClient locally and then use the Process to print the document to the printer.

LOCAL PROCEDURE PrintRemoteFile@1240060002();
       FileURL@1240060000 : Text;
       ProcessStartInfo@1240060001 : DotNet "'System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.ProcessStartInfo";
       Process@1240060002 : DotNet "'System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process";
       WebClient@1240060003 : DotNet "'System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebClient";
       LocalFileName@1240060004 : Text;
       FileURL := '';
       ProcessStartInfo := ProcessStartInfo.ProcessStartInfo();
       WebClient := WebClient.WebClient();
       LocalFileName := 'C:\Temp\TempFile.pdf';
       ProcessStartInfo.FileName := LocalFileName;
       ProcessStartInfo.Verb := 'Print';
       ProcessStartInfo.CreateNoWindow := FALSE;
       Process := Process.Process;
       Process := Process.Start(ProcessStartInfo);
       MESSAGE('Document Printed');

If you have any other tips or suggestions to resolve this error, please do share them in the comments below.



Kuljeet Singh said...

Hi Suresh
I tried this code but this is not working for me, do you have any idea why this is not working for me.
The only difference I made in my code is I did not download any file from web, instead I am already having a file in some specified location which I am trying to print.
Here is my code

ProcessStartInfo := ProcessStartInfo.ProcessStartInfo();
WITH ProcessStartInfo DO BEGIN
FileName := 'c:\temp\S076511.pdf';
Verb := 'Print';
CreateNoWindow := FALSE;
WindowStyle := ProcessWindowStyle.Hidden;
Process := Process.Process;

I am getting this error:

A call to system.diagnostics.process.start failed with this message. The system cannot find the file specified

Kuljeet Singh said...

Thanks for your help

Suresh Kulla said...

Hello Kuljeet,

Did you set the RunOnClient for ProcessStartInfo, Process.. variables to true, if not it will be looking at that file on the NAV server, either you have to download the file to the client machine and print or set the RunOnClient property for the variables to true.

Let me know if that fixes the issue.


Kuljeet Singh said...

Hi Suresh, Thanks for the your expert advice, it works for me now :)

Alberto Costantin said...

Hi Suresh, this solution it's very interesting but i have a question.
If i want to set another Printer and not print with a default printer?
What should I do?

Suresh Kulla said...


I working on a modification with that specific requirement and I will post the solutions in a new blog in next couple of days. One Solution suggest online is to use the Arguments property for Process Start Info but that did not work for me.
So please check my blog in next couple of days for the solution.


Gunaseelan said...

I'm get error from nav2016 please let me know the is there any hotfix or any recommendation my build 42815

Microsoft Dynamics NAV

Break On Error Message:

The operation cannot complete because it uses a feature that is not supported (.NET Framework Interoperability.)