0

I am trying to print a pdf created from code in an asp.net webforms app, but before the actual print I want to show the print preview popup that appears for example when I use javascript's window.print()

Basically, I need the exact same popup to appear, without changing the page I am curently in, but instead of showing in the popup the page I curently am in, I want to show the PDF created.

The problem is I can't find anything that would get me to this result. Maybe I don't know what to look for, so thanks in advance for any advice.

next_user
  • 293
  • 3
  • 11

2 Answers2

0

window.open("path to pdf"); or window.location.href = "path to pdf";

this will open your pdf in a new window. I don't know about a print preview but it will allow them to view the generated pdf.

jaredlee.exe
  • 81
  • 1
  • 9
0

Short answer, no, Print Preview is a common but optional client-side feature and preference that in an uncontrolled environment (the Internet) you do not have programmatic access to.

If you do have a controlled environment then you can just install custom software that you can program against. But since you're asking in the first place I'm assuming this won't be an option for you.

As @jaredlee.exe said, your best best is to pop open a new window but instead of linking directly to the PDF you could try linking to a simple page that has a full-page iframe or object (or possibly embed) that points to your PDF. Then you could bind an onload (or onreadystatechange or domcontentloaded or whatever else) event that fires the print() method for that specific object.

That all said, there's a really big point to understand and that's that web browsers having the ability to render PDFs natively is a relatively new thing. Adobe shipped a plugin for IE (and maybe Netscape) in the 90's and over the years newer browsers like Chrome and Firefox were added. Overtime, however, these programs started to add their own PDF renderers and once they did that they actually disabled Adobe's. On top of that, operating system vendors (who often happened to also be browser vendors) started to add native PDF renderers directly to their operating system. Some people (myself included) believe that all of these renderers pale in comparison to Adobe's reference renderer so we then disable the built-in ones wherever we find them. So for me (and I know I'm weird) all of these options would still, at best, result in an empty window that's trying to print a blank page and a downloaded PDF.

To restate the above, a web browser is most commonly used to view web pages. The moment you switch to PDFs you are no longer in the "web world" but the "PDF world" and you aren't controlling a "web browser" but instead a "PDF renderer". Unfortunately there's no specification for talking to "PDF renderers" out there because the field is still too new.

To restate my restatement, this all might work some or most of the time, but also don't be surprised if there are edge cases that completely fail.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274