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.

11
  • 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. Commented Jun 26, 2014 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. Commented Jun 26, 2014 at 11:21
  • does code working if you click print button from server? Commented Jun 26, 2014 at 12:20
  • Yes when debugging, but not when the website is hosted in IIS. Commented Jun 26, 2014 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 .... Commented Jun 26, 2014 at 23:30

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

If this is a public facing web server, this is a very, very bad idea.
@Kev. It will be a private server. Anyway, this is the only solution i could find. Do you have any suggestion?
@Kev I've been reading your answer about IIS AppPoolIdentity and file system permissions and also your answer regarding accounts. I followed the documentation but it is not working when setting ApplicationPoolIdentity. What could be the reason?
This is what helped us as well. We also had to replace Adobe Reader with Foxit Reader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.