86

I want to print HTML from a C# web service. The web browser control is overkill, and does not function well in a service environment, nor does it function well on a system with very tight security constraints. Is there any sort of free .NET library that will support the printing of a basic HTML page? Here is the code I have so far, which does not run properly.

public void PrintThing(string document)
{
    if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
    {
        Thread thread =
            new Thread((ThreadStart) delegate { PrintDocument(document); });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }
    else
    {
        PrintDocument(document);
    }
}

protected void PrintDocument(string document)
{
    WebBrowser browser = new WebBrowser();
    browser.DocumentText = document;
    while (browser.ReadyState != WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }
    browser.Print();
}

This works fine when called from UI-type threads, but nothing happens when called from a service-type thread. Changing Print() to ShowPrintPreviewDialog() yields the following IE script error:

Error: dialogArguments.___IE_PrintType is null or not an object.

URL: res://ieframe.dll/preview.dlg

And a small empty print preview dialog appears.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137

6 Answers6

36

You can print from the command line using the following:

rundll32.exe %WINDIR%\System32\mshtml.dll,PrintHTML "%1"

Where %1 is the file path of the HTML file to be printed.

If you don't need to print from memory (or can afford to write to the disk in a temp file) you can use:

using (Process printProcess = new Process())
{
    string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
    printProcess.StartInfo.FileName = systemPath + @"\rundll32.exe";
    printProcess.StartInfo.Arguments = systemPath + @"\mshtml.dll,PrintHTML """ + fileToPrint + @"""";
    printProcess.Start();
}

N.B. This only works on Windows 2000 and above I think.

Fred
  • 3,365
  • 4
  • 36
  • 57
ICR
  • 13,896
  • 4
  • 50
  • 78
  • Great answer - was going to mark it accepted, and ran my test. Seems that dll still pops up the Print dialog, and as this is to run as an unattended Windows Service, we need to bypass that somehow. I've pretty much exhausted Google, and I'm at my wits end. – Chris Marasti-Georg Aug 24 '09 at 16:25
  • Unfortunately printer dialog appears. I tested on Windows 7 x64. – LukaszTaraszka Nov 02 '16 at 13:45
8

I know that Visual Studio itself (at least in 2003 version) references the IE dll directly to render the "Design View".

It may be worth looking into that.

Otherwise, I can't think of anything beyond the Web Browser control.

EndangeredMassa
  • 17,208
  • 8
  • 55
  • 79
6

Easy! Split your problem into two simpler parts:

  1. render the HTML to PDF
  2. print the PDF (SumatraPDF)
  • -print-to-default $file.pdf prints a PDF file on a default printer
  • -print-to $printer_name $file.pdf prints a PDF on a given printer
Community
  • 1
  • 1
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • Last I checked, part 2 (printing via command line with Adobe) does not work in a service - it requires user interaction. – Chris Marasti-Georg Aug 16 '12 at 14:37
  • 1
    Try Sumatra PDF it's much faster http://blog.kowalczyk.info/software/sumatrapdf/manual.html#manual-cmd-line – Colonel Panic Aug 16 '12 at 14:41
  • @ColonelPanic Wow. Great hint. Took me ages to find one that works even from a service, and it seems SumatraPdf is actually usable in commercial packages. – Nyerguds Jul 12 '18 at 13:36
4

If you've got it in the budget (~$3000), check out PrinceXML.

It will render HTML into a PDF, functions well in a service environment, and supports advanced features such as not breaking a page in the middle of a table cell (which a lot of browsers don't currently support).

Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137
user314783
  • 179
  • 3
2

I tool that works very well for me is HiQPdf. https://www.hiqpdf.com/

The price is reasonable (starts at $245) and it can render HTML to a PDF and also manage the printing of the PDF files directly.

AnthonyVO
  • 3,821
  • 1
  • 36
  • 41
1

Maybe this will help. http://www.codeproject.com/KB/printing/printhml.aspx Also not sure what thread you are trying to access the browser control from, but it needs to be STA

Note - The project referred to in the link does allow you to navigate to a page and perform a print without showing the print dialog.

NastyNateDoggy
  • 292
  • 1
  • 2
  • 14