1

I'm programming a web page for internal use at my company.

It's supposed to dynamically create PDF documents, and send them to the office printer.

The code I'm using is this:

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

ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "open";
info.FileName = processFilename;
info.Arguments = String.Format(@"/t ""{0}"" ""{1}""", pdfFilePath, PrinterSettings.InstalledPrinters[0]);
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;

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

p.WaitForInputIdle();

System.Threading.Thread.Sleep(6000);
if (false == p.CloseMainWindow())
{

    p.Kill();

}

It finds wherever adobe acrobat reader is located through the registry, and opens it with the argument of /t which sends the next argument pdfFilePath directly to the printer. After 6 seconds it closes adobe acrobat reader.

This runs just fine on my local computer when debugging it in visual studio 2010.

But when I deploy it on the server using IIS 7.5 it doesn't open adobe acrobat reader at all.

I've checked that it has the correct file path for AcroRd32.exe and the PDF I'm trying to print.

I've also tried a version of this program which opens the actual PDF file with the verb "Print", but adobe acrobat reader still won't open.

I think it might be a permissions issue.

Edit: I've tested this code using Foxit Reader instead of Adobe Acrobat Reader.

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

The remaining code is unchanged. It opens Foxit Reader and prints on my local debugging environment. But nothing happens when the code is run on the server.

G3n0c1de
  • 139
  • 2
  • 14
  • If it's a perm issue, simply check the account running the app pool has read access to that registry key and the executable. You'd know, though, because this code would throw. Are you getting any exceptions? Uncaught exceptions would be logged to the Event Log. It might also be because acrobat cannot run without an active user session. When you're debugging, you're running it. When it's deployed on a remote system, nobody is logged in. –  Feb 08 '17 at 19:14
  • https://forums.adobe.com/thread/1042132 From this convo, it appears Adobe doesn't print without opening the application. That would seem to require an active user session to do, which means you probably can't do this. There are other ways to print PDFs, you should research them. –  Feb 08 '17 at 19:18
  • Adobe Reader just wasn't engineered for your use case. Even if you did manage to get it working for your current version, a security update might prevent it from working in the future. Take a look at the linked article for a solution using the Adobe PDF Library. http://blogs.datalogics.com/2015/11/11/printing-pdf-from-the-command-line-using-the-adobe-pdf-library-with-datalogics-extensions/ – joelgeraci Feb 08 '17 at 19:26
  • @Will, I've tried a third party solution, Foxit Reader, and that exe doesn't open either. I've replaced `.OpenSubKey("AcroRd32.exe")` with `.OpenSubKey("foxitreader.exe")`. It prints in my debugging environment, and doesn't open on the server, even though I've read about it not requiring an active user session. I've checked the event log and I haven't found any exceptions. The page simply posts back as if the code ran without issue. – G3n0c1de Feb 09 '17 at 00:10
  • "even though I've read about it not requiring an active user session" really? That kind of info is scarce and rarely looked for. Huh. Anyhow, there are other ways to do it. You can find pure code methods of printing a pdf. All you have to do is find a library that will print the PDF for you. You might have to buy it, but considering it *renders the PDF in a format acceptable to printers* the cost will be worth it. –  Feb 09 '17 at 14:54

0 Answers0