MVC print PDF without displaying in view with print dialog
MVC print PDF without displaying in view with print dialog
I have an MVC app where the intention is for the the user to click Print, and in the subsequent print dialog, verify the target printer, number of copies, etc, and click Print. I'm rendering the PDF document and returning the document thusly:
return File(printJob.Bytes, "application/pdf");
This displays the document quite nicely in the browser. In both Chrome and Edge, when the user clicks Print, they get a preview of the page in the print dialog. So the user experience is they click Print, see a preview, click print, see a preview again and then click print. Does anyone know if it's possible to render the PDF document without displaying it in the browser and instead just present the user with the print dialog box?
Thx in advance!
1 Answer
1
When you return the PDF from your website, the browser is then acting as a PDF viewer instead of a web browser, so you need to get the PDF to request the print dialog on open, not the website.
You can achieve this by adding the document javascript this.print(true)
into the PDF that you are generating.
this.print(true)
You can test this in Adobe Acrobat by going to Javascript -> Edit All Javascripts and just pasting this.print(true)
at the top outside of any named function.
this.print(true)
Tested and working on Chrome, as soon as you open the PDF it displays the print dialog.
Acrobat Javascript Reference is here, the reference to the print function is on page 179.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.