Killing an external process from NAV

Hi everyone,

I am generating a PDF with this code, using the PDFSharp library, but the adobe keeps open when it finishes

  Printer := Printer.PdfFilePrinter(FileNameServer);
  Printer.DefaultPrinterName := 'Etiquetas Expediciones 2';
  Printer.AdobeReaderPath('C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe');
  Printer.Print();
  CLEAR(Printer);
  //SHELL('C:\WINDOWS\system32\cmd.exe', '/c', 'TASKKILL /F /IM AcroRd32.exe /T');

As you can see, I’ve tried whit the “SHELL” function, but it is deprecates for NAV2018. How should I close and end the task for the Adobe Reader??

Thank you very much

This is the second time you ask the question of how to print a PDF-document. On the first time you got a suggestion to your solution to save the documents in a folder and then have a program that looks into the folder and if files exist then print them.

If you have not found a program for it then try to google: “automatic printing from folder”

The problem is once again, you cannot control what the server is doing from the client side. The codeunit that executes the above code is not the user that start the code, it is entirely the servicetier. (For that very reason the SHELL command has been omitted).

Running AcroRd32.exe from command-prompt is not the solution as adobe is a gui-application and not a command-line-application.

I’m not asking about how to print.

The issue about the servicetier and the architecture of the instalalition is quite peculiar, due to network issues in the customer. I thought that using a library as PDFSharp will be an option, i’ve read about it in differenter sites.

Sory for my lack of understanding, I’ll find another way to do this

I am sorry if you find me being rude!. It was definately not the intension!

But your problem is related to printing. You said in another question that you want to print a PDF-file. You found what appeared to be a good solution with the start of Adobe PDF and print with the application. Unfortunately the Adobe does not exit the app after your printing (because it is a GUI-application, and not a commandline-application).

The big “problem” is that you (and many others as well) keep on believing that it is the client that does the actual execution of the code - but it is not. Everything is done on the serverside (the NAV servicetier) and therefore you have to think in a much different way, comparing to if you did it in NAV 2009 R2 or below.

I myself have also struggled with this for a while. What we as NAV developers need to learn is that not everything can be fixed using AL-CODE - other solutions has to be found. And that is what I have trying to give you with the goggle suggestion.

No worries Palle, every tip that comes from you is really appreciated, and more for a rookie like me. Every new development or integration is a battle for me, and it wouldn’t work without your help or others in this forum…

I found in google different 3rd party software, that works really fine, but must be licensed for not generating extra pages with their own watermark. I’ve asked a few of them about licensing.

The other option I found is to create a script with powershell, and add it to the task scheduler…

Thank you again

You ask to kill Adobe Reader, but instead of killing it, why don’t you use a function that do not relay on opening Adobe reader?

What are the intension of this code? What are you printing to PDF?

To be fair. Printing a PDF-file was the original intension. This is what the above solution is doing, the drawback is the running Adobe Reader application that needs to be killed. My suggestion was a powershell script or a program that looks for PDF documents in a specific folder.

You are more than welcome to see if you can come up with a solution that prints a PDF-file (That was not generated by NAV) .

It it has to be done from a commandline I would suggest using the free GhostScript

https://www.ghostscript.com/doc/current/Use.htm

That’s it. I receive a PDf fro ma webService, and I need to print it to a certain printer. That is solved, id it was not for the Adobe stlls open…

I’m debuging other parts of the code, and now I’m gonan try to develop a script… And I’ll also take a look to the ghostScript idea

Thank you!

about “I am generating a PDF with this code, using the PDFSharp library” - are you sure that this library does not have method which close connection or similar?

Did you check all methods?

I’m on it, doesn’t seem to exist… I’ve read that it’s one of the “bad things” of this libray…

Well then you can also have a look at:

Process DotNet System.Diagnostics.Process.System,

FOREACH Process IN Process.GetProcessesByName(‘AcroRd32’) DO BEGIN
Process.Kill;
END;

It will kill Adobe reader

Thanks Lars,

Seem to work perfectly, but when the adobe is close, I get the next error, and reading in different sites, seems to be something about admin rights…

kill.png

Give your service account need right to Terminate process throw windows

Hi lars,

You are talking about the user that starts sesion of the service, am I right?

As far as I understand, that should be done in the server. out from NAv, no? How should be the right way?

THank you very much

Yes, and the create a user special right and only give the user right “Terminate”

Really appreciate Lars, I’ll work on it! I don’t know realy how to do it, I’ll suggest our customer to talk with the server provider

Cool, remember to mark a answer as solution to help other.

Hi Lars,

Instead of changing the user rights, I’ve solved this issue with:

   FOREACH Process IN Process.GetProcessesByName('AcroRd32') DO BEGIN
    Process.CloseMainWindow;
   END;

Instead of KILL.

Thank you!!!