1

I'm trying to print a PDF manually through Process.Start, but it isn't working in IIS. I copied the same code in a windows form application and that worked. I already tried giving the rights to 'Network Service' user (my application pool has Network Service permission). I've also followed the steps here: IIS7 does not start my Exe file by Process Start

 string file = @"C:\test.pdf";
            string printer = "TestPrinter";

            string processFilename = Microsoft.Win32.Registry.LocalMachine
                .OpenSubKey("Software")
                .OpenSubKey("Microsoft")
                .OpenSubKey("Windows")
                .OpenSubKey("CurrentVersion")
                .OpenSubKey("App Paths")
                .OpenSubKey("AcroRd32.exe")
                .GetValue(String.Empty).ToString();

            var info = new ProcessStartInfo();
            info.FileName = processFilename;
            info.Arguments = string.Format("/h /t \"{0}\" \"{1}\"", file, printer);
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.UseShellExecute = false;

            Process p = Process.Start(info);
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            int counter = 0;
            while (!p.HasExited)
            {
                System.Threading.Thread.Sleep(1000);
                counter += 1;
                if (counter == 5) break;
            }
            if (!p.HasExited)
            {
                p.CloseMainWindow();
                p.Kill();
            }
Nic
  • 217
  • 2
  • 16
  • 1
    Permissions, or maybe architecture (32 vs 64 bit). See [this question](https://stackoverflow.com/questions/8414514/iis7-does-not-start-my-exe-file-by-process-start) – stuartd Jun 28 '17 at 09:05
  • Possible duplicate of [IIS7 does not start my Exe file by Process Start](https://stackoverflow.com/questions/8414514/iis7-does-not-start-my-exe-file-by-process-start) – Owen Pauling Jun 28 '17 at 09:05
  • Hi Stuart, I checked that link already and have given all the permissions to Network Service user (my application pool's identity), but it still isn't working. I'm able to see the adobe exe in task manager though, but it doesn't fire the print command or open the print dialog. – Nic Jun 28 '17 at 09:07
  • 1
    Did you try to see what will happen when you allow it to show window? – Marcin Ciesla Jun 28 '17 at 09:16
  • Yes, just tried that again and still no good. – Nic Jun 28 '17 at 09:19
  • Define 'isn't working'. What **specifically** occurs? – mjwills Jun 28 '17 at 09:57
  • @mjwills Adobe exe opens in Task Manager and then gets killed after 5 seconds based on the code I've written, but I don't get any prints. I've opened Printer's 'What's printing' dialog, but nothing gets queued. – Nic Jun 28 '17 at 10:04
  • Does `Network Service` have access to your printer? – mjwills Jun 28 '17 at 10:06
  • It has the access, but I think the problem is with the Adobe dialog which opens during printing. There is probably some setting in the code which I've written above. – Nic Jun 28 '17 at 10:24
  • Possible duplicate of [How to print PDF document from Windows Service](https://stackoverflow.com/questions/5596394/how-to-print-pdf-document-from-windows-service) – mjwills Jun 28 '17 at 10:41

1 Answers1

1

Well, it took me three days and multiple ways and tools to test printing only to confirm that it's not a programming problem. It is a permission problem. When I was searching for tips, this particular problem seemed to be ongoing for more than a decade and it always happens to certain web server setups. So, here is the solution for anyone who stumbled upon the same situation as I had and save the three days of headache.

The setup:

  1. Have a web application or service running on IIS server and you need to print some documents.
  2. Have a network printer set up for this purpose, such as printing to a particular departmental printer within the office network.
  3. The printing works when you are testing on localhost with your development machine.
  4. The printing silently drops and nothing happens when deployed to the actual web server.

The reason is that a network printer is accessed in the form of \networkserver\printername and the web application account, IIS_IUSER, is not a domain account and, therefore, doesn’t have access to any network server.

Solutions:

  1. Add the IIS_IUSER account to the domain BUT this is a very bad idea because you would need to give Internet users of your web app to have access to some network drive. Therefore, this is a no-no.
  2. Add the printer using TCP/IP instead of setting it up as a network drive. By IP address, all local users, including the IIS_IUSER account will have access to the printer by default.

With an IP printer, no matter you use ProcessStartInfo or a third-party tool to print, it will work. Happy programming!

Jack Ceramic
  • 212
  • 2
  • 6