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

后端 未结 7 1438
夕颜
夕颜 2020-12-13 22:46

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

相关标签:
7条回答
  • 2020-12-13 23:26

    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.

    0 讨论(0)
  • 2020-12-13 23:26

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

    0 讨论(0)
  • 2020-12-13 23:28

    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.

    0 讨论(0)
  • 2020-12-13 23:30

    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
    0 讨论(0)
  • 2020-12-13 23:33

    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.

    0 讨论(0)
  • 2020-12-13 23:49

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

    0 讨论(0)
提交回复
热议问题