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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 10:08:26  来源:igfitidea点击:

How do I print an HTML document from a web service?

提问by Chris Marasti-Georg

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

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

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:

这在从 UI 类型线程调用时工作正常,但从服务类型线程调用时没有任何反应。更改Print()为会ShowPrintPreviewDialog()产生以下 IE 脚本错误:

Error:dialogArguments.___IE_PrintTypeis null or not an object.

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

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

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

And a small empty print preview dialog appears.

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

回答by EndangeredMassa

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

我知道 Visual Studio 本身(至少在 2003 版本中)直接引用 IE dll 来呈现“设计视图”。

It may be worth looking into that.

可能值得研究一下。

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

否则,我想不出 Web 浏览器控制之外的任何东西。

回答by ICR

You can print from the command line using the following:

您可以使用以下命令从命令行打印:

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

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

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

其中 %1 是要打印的 HTML 文件的文件路径。

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.

注意,我认为这仅适用于 Windows 2000 及更高版本。

回答by Greg Ogle

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

我不知道具体的工具,但有一些实用程序可以记录/重放点击次数。换句话说,您可以自动化打印对话框上的“点击”。(我知道这是一个黑客,但是当所有其他方法都失败时......)

回答by NastyNateDoggy

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

也许这会有所帮助。http://www.codeproject.com/KB/printing/printhml.aspx也不确定您试图从哪个线程访问浏览器控件,但它需要是 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.

注意 - 链接中引用的项目允许您导航到页面并执行打印而不显示打印对话框。

回答by user314783

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

如果您的预算充足(约 3000 美元),请查看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).

它将 HTML 呈现为 PDF,在服务环境中运行良好,并支持高级功能,例如不会在表格单元格中间破坏页面(许多浏览器目前不支持)。

回答by Colonel Panic

Easy! Split your problem into two simpler parts:

简单!将您的问题分成两个更简单的部分:

  1. render the HTML to PDF
  2. print the PDF (SumatraPDF)
  1. 将 HTML 呈现为 PDF
  2. 打印 PDF ( SumatraPDF)
  • -print-to-default $file.pdfprints a PDF file on a default printer
  • -print-to $printer_name $file.pdfprints a PDF on a given printer
  • -print-to-default $file.pdf在默认打印机上打印 PDF 文件
  • -print-to $printer_name $file.pdf在给定的打印机上打印 PDF

回答by AnthonyVO

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

对我来说效果很好的工具是 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.

价格合理(起价 245 美元),它可以将 HTML 呈现为 PDF,还可以直接管理 PDF 文件的打印。