views:

281

answers:

4

I'm writing a server running as a Windows service that by request invokes Firefox to generate a pdf snapshot of a webpage.

I know it is a bad idea to run a GUI program in service mode, but the server nature of my program restricts from running it in the user mode. Running a user-level 'proxy' also is not an option, since there might be no interactive user logged-in on the machine with the server running.

In my experiments Firefox successfully produced pdf when the service was running under a user account that was already logged-in. Obviously it didn't work in other cases: for Local System and user accounts that weren't logged-in. Under LocalSystem with 'Allow service to interact with desktop' option enabled I could see the Firefox started that reports that it's unable to find a printer.

Since it wouldn't be practical to require an opened user session for the pdf server to run, is there any workaround for this except running the whole thing from a virtual machine?

UPDATE: I figured that the problem wasn't really with account permissions, but with an invisible modal dialog that FF was waiting on while running in the service mode.

However it's still unable to create a pdf when FF is running under the LocalSystem account. FF says it can't find a printer and I'm wondering if this is a permission that could be somehow enabled?

A: 

I've been through a similar conundrum with the MS Word running unattended. What you need to do is to login as the user you set up to be used to run firefox process and go through the process of setting up printers.

It is possible that just logging in as that user will be enough - there is some stuff performed during the first logon.

I apologize I was not clear enough - I mean you have to logon interactively using that account, configure the default printer, logout, and then run your service

mfeingold
Yes, in fact this already worked for me. GUI applications run just fine under logged in accounts as I mentioned in the 3rd paragraph. But I can't rely on an opened user session because the user might eventually decide to log off or just forget to log in.
Leonid
@Leonid: your service runs in its own login session, not the interactive session, so it doesn't matter if the user logs out.
Ben Voigt
Thank you, Ben. In fact, last week I figured my problem wasn't in FF inability to print when launched under an inactive account, but in a modal dialog window that I couldn't see as it was running in service mode.. Now it works under any account except LocalSystem. It still complains about inability to find any printer (even though I'm using a virtual one - see my other comment about cmdlnprint FF addon).
Leonid
+6  A: 

You might want to try a different approach where you'd include some .NET PDF library (PDFsharp is a good, open source, choice) in your project and than use that in conjuction with WebBrowser control you'd also include in your project to render the PDF.

Don't forget to use STAThread attribute if you try this.

kzen
+1 Seriously, as kzen mentions, you really should consider using a component that creates pdfs, not opening a GUI app to do it. If this can be invoked by multiple users, then you are begging for trouble. Most GUI apps just aren't build for this scenario and many people have gone down this road only to run into contention issues.Not to shill for them, but Aspose has a number of components built for this sort of scenario as another candidate to PDFSharp. I also always ask "Why PDF"? It is such a default answer for people that they often don't ask whether an easier format is ok.
Jim Leonardo
Without knowing what the original poster wants, this solution wouldn't work in a "browsershots" kind of situation. If the point of the project is to get an overview of what a page looks like, using a PDF-generator isn't quite the answer.
Christian W
You're right, Christian. The problem is that I need to make valid rendering of possibly a flash and js-enabled page first before saving the result to PDF. That means I should either stick with an existing engine like Gecko or Webkit or write it myself. Fortunately FF with a 'cmdlnprint' add-on solves both problems. But running this bundle as a service is the main challenge.
Leonid
A: 

A couple of year ago I had a related issue: Shared Network Printer on pseudo-device starting up Ghostscript for PS->PCL translation and printing to real printer. The print-spooler service ran as Local System and the pseudo-device driver hat troubles executing Ghostscript from the service-mode. I was able to solve the problem by copying a couple of registry keys from the HKCU-hive to HKLM.

edgar.holleis
thank you, Edgar. This seems like a good idea, but I'm trying to be as little invasive to the user's env as possible.
Leonid
A: 

Can you run the program as a Scheduled Task instead? The task can be assigned to a given user account which should work around the service limitations.

qor72
since I figured that the problem wasn't really with account permissions, but with an invisible modal dialog, this is not an issue anymore. However I believe this might work too.
Leonid