How to setup server side printing in ASP.NET 4.0 using Crystal Reports

by Jim Cho 26. September 2011 07:32

You are trying to print in ASP.NET on a printer setup on the server using Crystal Reports. This could be a network or IP printer. The code you use might look like this simplified version:

using (ReportDocument rptDoc = new ReportDocument())
{
    rptDoc.Load(myReportPath);
    rptDoc.SetDataSource(myDataSource);
    rptDoc.PrintToPrinter(1, true, 0, 0);
}

Note that if a printer name is not specified (with PrintOptions.PrinterName), it should still print to the default printer. When you explicitly setting the printer name with PrinterOptions.PrinterName results in the same printer not found errors.

This seems to work fine when you run it from your development machine, but after you deploy your application to the server, it no longer works. The server Event Log shows an exception like the following:

Exception information:

Exception type: InvalidPrinterException

Exception message: No printers are installed.
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
at System.Drawing.Printing.PrinterSettings.GetHdevmode()
at System.Drawing.Printing.PageSettings.GetPaperSize(IntPtr modeHandle)
at System.Drawing.Printing.PageSettings.get_PaperSize()
at CrystalDecisions.CrystalReports.Engine.PrintOptions.FindPaperSize(PrinterSettings printer, PaperSize size)

The reason it works while on your development machine is that the development web service is running in the context of your windows login, which probably has the correct printers set up. On the server, it’s probably running as the Network Service or as the Local System account.

The solution is to setup printers for the system account using the instructions below. You will also need to change the identity of the application pool that your app is using to use the LocalSystem identity.

clip_image002

Even though the article below was written for COM and ASP, it works in Windows 2008 R2 with ASP.NET 4.0. It will allow you to specify any printer setup on your network.

Reference: http://support.microsoft.com/kb/184291

You can set up printers for the SYSTEM account to resolve this problem. To set up printers for the SYSTEM account, perform the following:
This method requires you to modify the registry using the Registry Editor.
WARNING: Using Registry Editor incorrectly can cause serious, system-wide problems that may require you to reinstall Windows to correct them. Microsoft cannot guarantee that any problems resulting from the use of Registry Editor can be solved. Use this tool at your own risk.

  1. Ensure that the user you are currently logged into on the server has the desired printers installed.
  2. Launch the Registry Editor (Regedit.exe).
  3. Select the following key:

HKEY_CURRENT_USER

\Software\Microsoft\Windows NT\Current Version\Devices

  1. From the Registry menu, click Export Registry File.
  2. In the File Name text box, type c:\Devices.reg.
  3. Select the following key:

HKEY_CURRENT_USER

\Software\Microsoft\Windows NT\Current Version\PrinterPorts

  1. From the Registry menu, click Export Registry File.
  2. In the File Name text box, type c:\PrinterPorts.reg.
  3. Select the following key:

HKEY_CURRENT_USER

\Software\Microsoft\Windows NT\Current Version\Windows

  1. From the Registry menu, click Export Registry File.
  2. In the File Name text box, type c:\Windows.reg.
  3. From the Start button, select Run. Open Devices.reg in Notepad by typing Notepad Devices.reg in Run dialog box.
  4. Replace the text HKEY_CURRENT_USER with HKEY_USERS\.DEFAULT
  5. Save the file. Then import it into the registry by double-clicking the file in Windows Explorer.
  6. Repeat steps 13 through 15 for PrinterPorts.reg and Windows.reg.

These steps only work for local printers.
To enable IIS to enumerate the network printers by using the SYSTEM account, follow these steps.
Note If the process is running under the Network Service account, explicit permissions to the newly created registry are required.

  1. Make sure that you are currently logged in to the server as a user who has the desired network printers installed.
  2. Start Registry Editor.
  3. Click the following key:

HKEY_CURRENT_USER\Printers\Connections

  1. On the Registry menu, click Export Registry File.
  2. In the File Name box, type c:\printconns.reg.
  3. To open the printconns.reg file in Notepad, click Start, click Run, type Notepad printconns.reg in the Open box, and then click OK.
  4. Replace the text HKEY_CURRENT_USER with the text HKEY_USERS\.DEFAULT.
  5. Save the file.
  6. To import the file into the registry, double-click the file in Windows Explorer.
  7. Restart the Print Spooler service.

 

blog comments powered by Disqus