How to print an HTML document from a web service?

I want to print HTML from a C # web service. Management through a web browser is unnecessary, it does not work properly in a service environment and does not work properly in a system with very severe security restrictions. Is there any free .NET library that will support printing a basic HTML page? Here is the code that I have that does not work 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 great when called from threads like a user interface, but nothing happens when called from a thread like a service. Changing Print() to ShowPrintPreviewDialog() results in 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 preview dialog appears.

+73
html c # web-services printing
Aug 01 '08 at 18:33
source share
6 answers

You can print from the command line using the following:

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

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

If you do not need to print from memory (or you can afford to write to disk in a temporary 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(); } 

NB This only works on Windows 2000 and above.

+31
Aug 03 '08 at 18:06
source share

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

Maybe you should think about it.

Otherwise, I can’t think of anything other than a web browser control.

+4
Aug 02 '08 at 0:42
source share

Easily! Divide your problem into two simpler parts:

  • -print-to-default $file.pdf prints the PDF file to the default printer
  • -print-to $printer_name $file.pdf prints PDF on this printer
+4
Aug 15 '12 at 13:27
source share

If you have a budget (~ $ 3000), select PrinceXML .

It turns HTML into PDF, works well in a service environment, and supports advanced features such as not splitting a page in the middle of a table cell (which is not currently supported by many browsers).

+3
Apr 12 '10 at 17:57
source share

Perhaps this will help. http://www.codeproject.com/KB/printing/printhml.aspx Also not sure which thread you are trying to access browser control, but it should be STA

Note. The project mentioned in the link allows you to go to the page and print without displaying the print dialog box. A.

0
Jun 17 '09 at 17:36
source share

I do not know specific tools, but there are some utilities that record / play clicks. In other words, you can automate the "click" in the print dialog. (I know this is hacking, but when all else fails ...)

-one
Sep 20 '08 at 17:37
source share



All Articles