'Process.Start doesn't work in IIS

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();
            }


Solution 1:[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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jack Ceramic