2

This is my code for printing an already generated PDF on website. The page allows the user to print that PDF file by selecting a printer from a list.

My code works fine when I run it from my local machine and the PDF is printed out from the selected printer, but when I deploy it to production web server (IIS 7) nothing happens when clicking on Print button and no exceptions or errors were raised.

I've checked that the file exists in the server as well as the executable C:\\Program Files\\Adobe\\Reader 10.0\\Reader\\AcroRd32.exe, so I thought the issue could be related to permissions.

Checks performed in server:

  1. All Users, System and IIS_IUSRS users has R/W/E permissions on ~/Content/Pdfs/ folder
  2. All Users, System and IIS_IUSRS users has R/W/E permissions on C:\Program Files\Adobe\Reader 10.0\Reader folder.
  3. Identity of my Application Pool on IIS was changed from ApplicationPoolIdentity to LocalSystem.

    private void Print(string file, string printerName)
    {                      
        string FilePath = Server.MapPath("~/Content/Pdfs/" + file);
    
        try
        {
            Process p = new Process();
            p.StartInfo.FileName = @"C:/Program Files/Adobe/Reader 10.0/Reader/AcroRd32.exe";
            p.StartInfo.Arguments = "/h /t \"" + FilePath + "\" \"" + printerName + "\"";
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.UseShellExecute = false;                
            p.Start();
            string stdout = p.StandardError.ReadToEnd();
            p.WaitForExit();
    
            const string fic = @"C:\Output.txt";
    
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fic);
            sw.WriteLine(stdout);
            sw.Close();
        }
    
        catch (Exception e) {
    
            const string fic = @"C:\Error.txt";
    
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fic);
            sw.WriteLine(e.Message);
            sw.Close();
        }
        }
    

What could be the problem?

UPDATE:

I've done more logging as suggested so i used Try...Catch to log both output and exceptions (updated code). Once executed on server web Output.txt file was created empty and Error.txt file was not created. No application errors/warnings were recorded on Event Viewer.

I opened a command prompt window on server and executing AcroRd32.exe /h /t "C:\inetpub\wwwroot\otweb\Content\Pdfs\orden20140627112547.pdf" "Bullzip Printer" the file was successfully printed out.

ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
  • as you said this code working fine on your local machine but not works at your production server. Try to call this code from server or open your website from server and execute code. may be it will works. – Rachit Patel Jun 26 '14 at 11:14
  • That's what I'm doing. The website is deployed on a server. I'm accessing to the website both from a client and from the server itself and the code is called when clicking on Print button inside the website. – ɐsɹǝʌ ǝɔıʌ Jun 26 '14 at 11:21
  • does code working if you click print button from server? – Rachit Patel Jun 26 '14 at 12:20
  • Yes when debugging, but not when the website is hosted in IIS. – ɐsɹǝʌ ǝɔıʌ Jun 26 '14 at 14:19
  • Can I propose to just add more logging? Save the command line and current user to file, then try to execute it normal server environment, then under the user using runas, review the system log, check whether this code in the `try { ... } catch ...`. – Alex Netkachov Jun 26 '14 at 23:30
  • @equisde It looks like a quite tricky issue... Could you please try run the command under the same user that IIS runs it? It should be something like "IIS_IUSRS" I guess but you may check it to be sure. Another option can be just try runas in the application, in other words try to elevate privileges. Another workaround may be implementing a service that creates the PDFs and communicate with this service from ASPX app. – Alex Netkachov Jun 27 '14 at 10:51
  • You are trying to print on the users machine, right? – usr Jun 27 '14 at 10:57
  • @AlexAtNet On my IIS server `IIS_IUSR` is a group not an user. Under Users folder I have only Administrator user. I can imagine that when the user access via website and click on `Print` button, the code is executed in the server with any IIS user or group privileges. How can i execute the command under the same user that IIS runs? – ɐsɹǝʌ ǝɔıʌ Jun 27 '14 at 11:41
  • @usr No. The users access to the website which is hosted on a IIS 7 server. Then they can select a PDF stored on the server from a list and select a printer from a combo. This combo is populated with the printers mapped on the server (this is because it will be an intranet application and all printers in the company are mapped on the server). Then when they click on Print button the above code is executed on server and it will send the document to the selected printer. – ɐsɹǝʌ ǝɔıʌ Jun 27 '14 at 11:44
  • @equisde - it may be naive approach but I would just check the `USER` environment variable in this case. And then use `RUNAS` command to run the exe under this user. – Alex Netkachov Jun 27 '14 at 11:56
  • Hi @AlexAtNet. USER environment variable is set to SYSTEM. I tried to execute `RUNAS /user:IUSR "AcroRd32.exe /h /t C:\inetpub\wwwroot\otweb\Content\Pdfs\orden20140627112547.pdf` under command prompt window but it asks for a password for IUSR user. I don't know what pwd is and I cannot reset the password for that user because there are another web services hosted on this IIS. Thanks for your time. – ɐsɹǝʌ ǝɔıʌ Jun 30 '14 at 08:46

1 Answers1

1

I finally found the solution. The only thing what helped me was set Application Pool Identity to Administrator account on IIS

ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
  • If this is a public facing web server, this is a very, very bad idea. – Kev Jun 30 '14 at 16:18
  • @Kev. It will be a private server. Anyway, this is the only solution i could find. Do you have any suggestion? – ɐsɹǝʌ ǝɔıʌ Jun 30 '14 at 17:48
  • @Kev I've been reading [**your answer**](http://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permissions/5439658#5439658) about IIS AppPoolIdentity and file system permissions and also [**your answer**](http://stackoverflow.com/questions/5729264/what-are-all-the-user-accounts-for-iis-asp-net-and-how-do-they-differ/5731360#5731360) regarding accounts. I followed the [**documentation**](http://www.iis.net/learn/manage/configuring-security/application-pool-identities) but it is not working when setting ApplicationPoolIdentity. What could be the reason? – ɐsɹǝʌ ǝɔıʌ Jul 01 '14 at 11:22
  • This is what helped us as well. We also had to replace Adobe Reader with Foxit Reader. – Kai Hartmann Jul 28 '15 at 07:36