如何从 Web 服务打印 HTML 文档?

我想从 C # Web 服务打印 HTML。Web 浏览器控制是过度的,在服务环境中不能很好地工作,在安全约束非常严格的系统中也不能很好地工作。是否有任何类型的免费 .NET库,将支持打印一个基本的 HTML 页面?下面是我到目前为止的代码,它不能正常运行。

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

这在从 UI 类型的线程调用时可以很好地工作,但是在从服务类型的线程调用时什么也不会发生。将 Print()更改为 ShowPrintPreviewDialog()会产生以下 IE 脚本错误:

错误: dialogArguments.___IE_PrintType是否为空对象。

网址: res://ieframe.dll/preview.dlg

并出现一个小的空打印预览对话框。

17167 次浏览

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.

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.

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...)

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.

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).

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

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.