<p>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 <code>.NET</code> library that will support the printing of a basic HTML page?  Here is the code I have so far, which does not run properly.</p>

<pre><code>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();
}
</code></pre>

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

<blockquote>
  <p><strong>Error:</strong> <code>dialogArguments.___IE_PrintType</code> is null or not an object. </p>
  
  <p>URL: <code>res://ieframe.dll/preview.dlg</code></p>
</blockquote>

<p>And a small empty print preview dialog appears.</p>

[{'answer_id': 290, 'author': 'EndangeredMassa', 'author_id': 106, 'author_profile': 'https://Stackoverflow.com/users/106', 'pm_score': 3, 'selected': False, 'text': '<p>I know that Visual Studio itself (at least in 2003 version) references the IE dll directly to render the "Design View".</p>\n\n<p>It may be worth looking into that.</p>\n\n<p>Otherwise, I can\'t think of anything beyond the Web Browser control.</p>\n'}, {'answer_id': 768, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 5, 'selected': False, 'text': '<p>You can print from the command line using the following:</p>\n\n<blockquote>\n  <p>rundll32.exe\n  %WINDIR%\\System32\\mshtml.dll,PrintHTML\n  "%1"</p>\n</blockquote>\n\n<p>Where %1 is the file path of the HTML file to be printed.</p>\n\n<p>If you don\'t need to print from memory (or can afford to write to the disk in a temp file) you can use:</p>\n\n<pre><code>using (Process printProcess = new Process())\n{\n    string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);\n    printProcess.StartInfo.FileName = systemPath + @"\\rundll32.exe";\n    printProcess.StartInfo.Arguments = systemPath + @"\\mshtml.dll,PrintHTML """ + fileToPrint + @"""";\n    printProcess.Start();\n}\n</code></pre>\n\n<p>N.B. This only works on Windows 2000 and above I think.</p>\n'}, {'answer_id': 108827, 'author': 'oglester', 'author_id': 2017, 'author_profile': 'https://Stackoverflow.com/users/2017', 'pm_score': -1, 'selected': False, 'text': '<p>I don\'t know the specific tools, but there are some utilities that record / replay clicks. In other words, you could automate the "click" on the print dialog.  (I know this is a hack, but when all else fails...)</p>\n'}, {'answer_id': 1008517, 'author': 'NastyNateDoggy', 'author_id': 124479, 'author_profile': 'https://Stackoverflow.com/users/124479', 'pm_score': 1, 'selected': False, 'text': '<p>Maybe this will help. <a href="http://www.codeproject.com/KB/printing/printhml.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/printing/printhml.aspx</a>\nAlso not sure what thread you are trying to access the browser control from, but it needs to be STA</p>\n\n<p>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.</p>\n'}, {'answer_id': 2624191, 'author': 'user314783', 'author_id': 314783, 'author_profile': 'https://Stackoverflow.com/users/314783', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'ve got it in the budget (~$3000), check out <a href="http://www.princexml.com" rel="nofollow noreferrer">PrinceXML</a>.</p>\n\n<p>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). </p>\n'}, {'answer_id': 11970128, 'author': 'Colonel Panic', 'author_id': 284795, 'author_profile': 'https://Stackoverflow.com/users/284795', 'pm_score': 3, 'selected': False, 'text': '<p>Easy! Split your problem into two simpler parts:</p>\n\n<ol>\n<li><a href="https://stackoverflow.com/a/5635643/284795">render the HTML to PDF</a></li>\n<li>print the PDF (<a href="http://blog.kowalczyk.info/software/sumatrapdf/manual.html#manual-cmd-line" rel="noreferrer">SumatraPDF</a>)</li>\n</ol>\n\n<blockquote>\n  <ul>\n  <li><code>-print-to-default $file.pdf</code>    prints a PDF file on a default printer  </li>\n  <li><code>-print-to $printer_name $file.pdf</code>  prints a PDF on a given printer</li>\n  </ul>\n</blockquote>\n'}, {'answer_id': 57838787, 'author': 'AnthonyVO', 'author_id': 438458, 'author_profile': 'https://Stackoverflow.com/users/438458', 'pm_score': 2, 'selected': False, 'text': '<p>I tool that works very well for me is HiQPdf.  <a href="https://www.hiqpdf.com/" rel="nofollow noreferrer">https://www.hiqpdf.com/</a></p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/174', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/96/']