How to trigger windows Print dialog from server side (asp.net)?

旧城冷巷雨未停 提交于 2020-01-01 19:53:11

问题


I want to generate a PDF file on server side, and then in response want to send that file (buffer,fileName- whatever may work) and show a print dialog to ask user to print the generated PDF file.

I tried something like below. But it does not trigger window.print() dialog.

   public static void ForcedPrint(HttpResponse response, byte[] buffer, string fileName, string fileExtension) {
      response.Clear();
      response.Buffer=true;
      response.Write("<script>window.print();</script>");
      response.Charset="";
      response.Cache.SetCacheability(HttpCacheability.NoCache);
      response.ContentType="application/pdf";
      response.BinaryWrite(buffer);
      response.Flush();
      response.End();
    }

Can someone please help me with this? The feature i am looking for is that i should be able to create PDF file on server, and in response user should get a dialog to print the generated file.

Thanks in advance.


回答1:


You need to embed the PDF in the HTML document, say in a div called thePDF and in JavaScript code in the document, you need to invoke

thePDF.printWithDialog()

The dialog that appears will be the Adobe Reader plugin's print dialog rather than the browser's print dialog; this will allow selection of pages etc. before printing.




回答2:


As far as I'm aware you cannot generate a print command to the browser on the server. The most you can do is generate the javascript which will make it pop up with a print dialog (window.print()) but that wouldnt help you with what you're trying to do.

Just speculating but you may try generating a page with an iframe that points to the PDF file, and in the base page have the javascript that tells the iframe to print?

Hope this helps,

Darko



来源:https://stackoverflow.com/questions/1112483/how-to-trigger-windows-print-dialog-from-server-side-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!