Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print PDF document from Windows Service

I have windows service that must periodically print PDF document from server. My function is

private void PrintFormPdfData(byte[] formPdfData)
{
    string tempFile;

    tempFile = Path.GetTempFileName();

    using (FileStream fs = new FileStream(tempFile, FileMode.Create))
    {
        fs.Write(formPdfData, 0, formPdfData.Length);
        fs.Flush();
    }

    string pdfArguments = string.Format("/p /h\"{0}\"", tempFile);

    string pdfPrinterLocation = @"C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe";


    ProcessStartInfo newProcess = new ProcessStartInfo(pdfPrinterLocation, pdfArguments);
    newProcess.CreateNoWindow = true;
    newProcess.RedirectStandardOutput = true;
    newProcess.UseShellExecute = false;
    newProcess.RedirectStandardError = true;

    Process pdfProcess = new Process();
    pdfProcess.StartInfo = newProcess;
    pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    pdfProcess.Start();
    pdfProcess.WaitForExit();
}

When I implement this in Windows Application it works, but when I implement in Windows service it does not work.

Can you help me?

like image 467
cashmere Avatar asked Apr 08 '11 14:04

cashmere


People also ask

Can a Windows service print?

The print document class provided by . NET is capable of printing inside a Windows Service. The benefit of using the print document class is that it gives some control of printing and settings. It exposes an event which can be consumed to change printing settings.

How do I get a PDF File to print?

To print a PDF document: Set up the page settings as recommended. Click the Print button on the toolbar or select File from the menu bar followed by Print from the dropdown list. Click OK to print.


1 Answers

I solved this problem with the registry edits found in this article: https://support.microsoft.com/en-us/kb/184291.

Printing from Windows services (and also IIS) uses the SYSTEM account as I understand it. This account does not have access to the printer but these registry edits gives that SYSTEM account printing privileges.

Here is a summary:

  1. export 3 keys ("folders within regedit.exe") to .reg files:
    • HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Current Version\Devices
    • HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Current Version\PrinterPorts
    • HKEY_CURRENT_USER\Software\Microsoft\Windows NT\Current Version\Windows
  2. edit your 3 newly created reg files and change "HKEY_CURRENT_USER" to "HKEY_USERS\.DEFAULT"
  3. run your 3 reg files by double clicking them in explorer (this adds them to the registry)

That's all it takes. This helped me to print pdf files to a printer from PHP in IIS 7. My php script would work just fine so long as the server happened to have the Administrator logged in when the script was run. Now no one needs to be logged in and the script still works :)

I hope this saves someone as much searching and tinkering as I put in to find it.

like image 121
HyperActive Avatar answered Sep 30 '22 03:09

HyperActive