IT박스

웹 서비스에서 HTML 문서를 어떻게 인쇄합니까?

itboxs 2020. 10. 25. 11:50
반응형

웹 서비스에서 HTML 문서를 어떻게 인쇄합니까?


C # 웹 서비스에서 HTML을 인쇄하고 싶습니다. 웹 브라우저 제어는 과도하고 서비스 환경에서 제대로 작동하지 않으며 보안 제약이 매우 엄격한 시스템에서도 제대로 작동하지 않습니다. .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 null이거나 개체가 아닙니다.

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

그리고 작은 빈 인쇄 미리보기 대화 상자가 나타납니다.


다음을 사용하여 명령 줄에서 인쇄 할 수 있습니다.

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

여기서 % 1은 인쇄 할 html 파일의 파일 경로입니다.

메모리에서 인쇄 할 필요가 없거나 임시 파일의 디스크에 쓸 여유가있는 경우 다음을 사용할 수 있습니다.

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 이것은 Windows 2000 이상에서만 작동한다고 생각합니다.


Visual Studio 자체 (최소한 2003 버전에서는)가 IE dll을 직접 참조하여 "디자인 뷰"를 렌더링한다는 것을 알고 있습니다.

그것을 살펴볼 가치가 있습니다.

그렇지 않으면 웹 브라우저 제어 외에는 아무것도 생각할 수 없습니다.


쉬운! 문제를 더 간단한 두 부분으로 나누십시오.

  1. HTML을 PDF로 렌더링
  2. PDF 인쇄 ( SumatraPDF )
  • -print-to-default $file.pdf 기본 프린터에서 PDF 파일을 인쇄합니다.
  • -print-to $printer_name $file.pdf 주어진 프린터에서 PDF를 인쇄합니다.

예산 (~ $ 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).


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.


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.


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

참고URL : https://stackoverflow.com/questions/174/how-do-i-print-an-html-document-from-a-web-service

반응형