Starting work with the server through PHP

This is most likely not easy, but here is the situation:

I wrote a C # command line application that:

  • creates a PDF file using ITextSharp
  • writes it to disk
  • uses Acrord32.exe (this is Acrobat Reader) via System.Diagnostics.Process to quietly print the generated PDF

If I create my solution and double-click pdfGen.exe , it will work as expected. PDF is created and printed.

Now my application should be deployed to an internal server with Windows Vista with IIS 7. PHP webapp is running on this server. And it will be called through PHP using shell_exec() so that the resulting PDF is printed on a printer connected to the server.

So my PHP page looks basically like this:

 shell_exec('/path/to/pdfGen.exe'); 

But here everything goes wrong. What happens in accordance with the task manager, etc .:

  • pdfGen.exe starting
  • PDF file is created.
  • Acrord32.exe starting
  • pdfGen.exe freezes forever (and also runs a PHP script) and nothing prints

I am sure this is a resolution issue. I already gave IIS_IUSRS access to the default printer and the directory where Acrord32.exe is located. But still, no print. However, if I run the pdfGen.exe file manually, it works.

Any idea what I am missing?

EDIT:

I do not have to use Acrobat Reader to print PDF. If there is another way to quietly print the created PDF server, I would not mind at all.

+4
source share
5 answers

Thank you all for your comments. Unfortunately, this "php start printjob" was part of a larger project that was canceled today due to, well ... I don’t know ... political reasons. Guess that the project is pretty much dead.

In any case, I tried myself several more times in recent days and could not get it to work with IIS. My solution that I implemented and tested already: uninstall IIS, install the XAMPP or WAMPP package with local apache and PHP, which works with administrator access rights.

It did the trick. I used pclose(popen('...command...', 'r')); in PHP to run .exe , and so PHP does not wait for the completion of the PDF file. Everything worked perfectly.

Here is my C # code that runs a print job using Acrobat Reader

 public void Print(string pathname, string acrobatDirectory) { var proc = new Process { StartInfo = { Arguments = String.Format("/t \"{0}\"", pathname), FileName = acrobatDirectory, UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = false, RedirectStandardError = false, } }; proc.Start(); } 

The first argument is the path to the PDF to be printed, the second parameter is the absolute path to AcroRd32.exe .

The only problem was that AcroRd32.exe was started, printed, and was not closed again. Therefore, each printjob started a new instance of AcroRd32.exe (I use Acrobat Reader 9.0). Therefore, if you printed 10 times, 10 copies of the acrobat reader were created.

I started to run the print job, and then waited X seconds, hoping the printer was finished, and then killed all instances of AcroRd32.exe :

 public void Print(string pathname, string acrobatDirectory) { Debug.WriteLine("Printing..."); Printer.Print(pathname, acrobatDirectory); Thread.Sleep(30000); try { Debug.WriteLine("Trying to kill runnung AcroRd32.exe "); FindAndKillProcess("AcroRd32"); } catch (Exception) { Debug.WriteLine("AcroRd32.exe could not be killed..."); } } private bool FindAndKillProcess(string name) { foreach (Process clsProcess in Process.GetProcesses()) { if (clsProcess.ProcessName.StartsWith(name)) { clsProcess.Kill(); return true; } } return false; } 

It worked out quite well.


Please note that the above (killing all AcroRd32.exe and running PHP with administrator privileges) was only possible because: All this is used by only one user at a time and has a very limited scope .

It should be used in a touch-screen application deployed on a client PIC. The seller must use the PHP application to configure the product, and then PHP will call my .exe, which would create and print the PDF in the background. The printed document is then transmitted to the client. Therefore, security, etc. in this case was not a problem.


If anyone has a solution to use it with IIS, I still agree to accept it as an answer.

0
source

To check what is happening, try running a process tracker from Sysinternals and filtering events in an adobe acrobat process. You will see acrobat system calls, and this will let you know more or less about what is going wrong.

+1
source

I know a slight improvement to your solution: SumatraPDF has a nice command line interface that you can use to automatically close Sumatra after printing. A.

I used the PHP functions "system" or "exec" to execute a batch file to open SumatraPDF:

 sumatrapdf.exe -print-to-default -exit-on-print <path_to_PDF_file> 

(you can also specify the name of the printer to print)

+1
source

what an interesting program.

IIS_IUSRS does not seem to have the right to print, try adding IIS_IUSRS to the group of print statements / grant print permission to the user.

0
source

Shell_exec () is almost intended for shell commands (ls / dir, cp, etc.). Have you tried using exec () instead of shell_exec ()?

0
source

All Articles