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

时间:2022-06-01 22:17:48

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.

我想从c# web服务中打印HTML。web浏览器控件的作用过大,在服务环境中不能很好地工作,在安全约束非常严格的系统上也不能很好地工作。是否有任何可以支持基本HTML页面打印的免费。net库?这是我到目前为止的代码,它不能正常运行。

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_PrintType' is null or not an object
URL: res://ieframe.dll/preview.dlg

错误:“dialogArguments。___IE_PrintType'是null或不是对象URL: res://ieframe.dll/preview.dlg

And a small empty print preview dialog appears.

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

6 个解决方案

#1


29  

You can print from the command line using the following:

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

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

rundll32。exe %列出% \ System32系统\[。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和以上我认为。

#2


3  

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来呈现“Design View”。

It may be worth looking into that.

这可能是值得研究的。

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

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

#3


3  

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格式,在服务环境中功能良好,并支持高级功能,如不在表单元格中破坏页面(许多浏览器目前不支持这种功能)。

#4


3  

Easy! Split your problem into two simpler parts:

简单!把你的问题分成两个简单的部分:

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

#5


0  

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

也许这将帮助。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.

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

#6


-1  

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

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

#1


29  

You can print from the command line using the following:

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

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

rundll32。exe %列出% \ System32系统\[。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和以上我认为。

#2


3  

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来呈现“Design View”。

It may be worth looking into that.

这可能是值得研究的。

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

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

#3


3  

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格式,在服务环境中功能良好,并支持高级功能,如不在表单元格中破坏页面(许多浏览器目前不支持这种功能)。

#4


3  

Easy! Split your problem into two simpler parts:

简单!把你的问题分成两个简单的部分:

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

#5


0  

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

也许这将帮助。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.

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

#6


-1  

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

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