Friday, September 22, 2017

How to print a remote file from NAV

fax-1889061_1920

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();
     VAR
       FileURL@1240060000 : Text;
       ProcessStartInfo@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.ProcessStartInfo";
       Process@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Diagnostics.Process";
       WebClient@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebClient";
       LocalFileName@1240060004 : Text;
     BEGIN
       FileURL := 'http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf';
       ProcessStartInfo := ProcessStartInfo.ProcessStartInfo();
       WebClient := WebClient.WebClient();
       LocalFileName := 'C:\Temp\TempFile.pdf';
       WebClient.DownloadFile(FileURL,LocalFileName);
       ProcessStartInfo.FileName := LocalFileName;
       ProcessStartInfo.Verb := 'Print';
       ProcessStartInfo.CreateNoWindow := FALSE;
       Process := Process.Process;
       Process := Process.Start(ProcessStartInfo);
       MESSAGE('Document Printed');
     END;

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

Share:

15 comments:

Kuljeet 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;
END;
Process := Process.Process;
Process.Start(ProcessStartInfo);

I am getting this error:

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

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

Thanks
Suresh

Kuljeet said...

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

Unknown 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?
Thanks.
Alberto

Suresh Kulla said...

Alberto,

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.

Thanks
Suresh

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.)
---------------------------
OK
---------------------------

Unknown said...

Hi Suresh

I came across your blog on the printing of external documents attached to NAV and see it has been very helpful

my question is, what if the document is attached to a PO in NAV.
How can this document automatically print when printing the PO

Thanks for you help

Sherry said...

Hi Suresh,

I am trying to download file from URL, I tried different methods (NAV 2015) but I am getting below error:-

Microsoft Dynamics NAV
---------------------------

A call to System.Net.WebClient.DownloadFile failed with this message: The request was aborted: Could not create SSL/TLS secure channel.
---------------------------
OK
---------------------------

I was looking for solutions then I find your code and used it as below but I am getting same error. It is working in BC 365.

WebClient := WebClient.WebClient();
Credential := Credential.NetworkCredential;
Credential.UserName := ''; //Specified UserName
Credential.Password := ''; //Specified password
WebClient.Credentials := Credential;

WebClient.DownloadFile('URL Address','Local Address');

Can you please suggest what can be issue.

Thanks!!

magictradingco said...

A very delightful article that you have shared here. Your blog is a valuable and engaging article for us, and also I will share it with my companions who need this info. Thankful to you for sharing an article like this.4 In 1 Mug Printing Machine

Astropack Gulf LLC said...

Astropack Gulf LLC provides the best food inspection system in Dubai, UAE, Kuwait, Oman, KSA, Bahrain, Qatar.it has been established with a mission to continue the legacy of our team in the market by providing unmatched support to the processing and food inspection system industries. These products we deal with will achieve your task in secured perfection and detail. Service Engineers under the technical head who has over 15 years of experience in the food inspection industry with all leading brands.

Astropackgulf LLC said...

hi, it's a very informative blog, ger more important information. and our company Astropack Gulf LLC is an approved distributor dealing with a range of high-performance Portable inkjet printers in Dubai, UAE, Saudi Arabia, and other GCC nations. Along with Portable inkjet printers we also proffer a range of continuous inkjet printers that assist you to get the best printing solutions for your printing demands. The products we deal with will finish your task in the guaranteed result and detail. The clients are assisted by a team of factory-trained technicians for installation, commissioning, and post-sales comfort. Apart from UAE, we have many happy clients in Oman, Kuwait, Saudi Arabia, Bahrain, and Qatar as well.

Pawprint Reminders said...

The information in the post you posted here is useful because it contains some of the best information available. Veterinary Appointment Reminder Cards. Thanks for sharing it. Keep up the good work.

Optimal3d said...

I just wanted to say this is an elegantly composed article as we have seen here. I got some knowledge from your article and also it is a significant article for us. Thanks for sharing an article like this.3d Printing Expert Auckland

Anonymous said...

THis does not work in NAV 2017 webclient as file is saved in server directory.