Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

How to print a report from a web application

Here's what I'd like to be able to do:

Using PHP/JavaScript, I'd like to create a <div> that's 8 1/2 by 11 inches in dimensions. It's contents will be details of a report of whatever kind. I then would like to be able to have the user click a "Print Report" button to print out this <div> to an 8 1/2 by 11 inch paper. I don't want any extraneous details like the URL link at the top or bottom. In short, I want the <div> to print out exactly the way it is, no more, no less.

Is there any way I can do this? Any advice that points me to the right path would be greatly appreciated.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

This is a bit of an open ended question

You can style your div with unit cm, mm, inches etc (refer spec here). There are some caveats with this
1) This may work on some but not other monitors - depends on the age of the monitor and if they support EDI or not which gives back a dpi reading which then can be translated into a width based on screen resolution. In older monitors where your could have your display configured to different resolutions without EDI it is not possible to get the width.

With respect to the URL at the bottom - this is variable in what you can do with it - in some cases you can and others not refer to the @page spec here - browser dependent.

My thoughts are that you don't use HTML for printing reports. I would look at a PDF library and use output to PDF as the printed version of the report. This way you are guaranteed of getting the same output all the time and you loose the URL's at the bottom by default.

Requires extra coding to output the PDF but there are a lot of good libraries out there that can assist with this.
Avatar of elepil
elepil

ASKER

JulianH, thank you for responding.

My goal is simplicity. I am actually rewriting a standalone desktop application as a PHP application. In the desktop version, the user could just click print and a pop window comes up to give them a print preview, after which they can just click the Print button and get a hardcopy.

Prior to making this post in this forum, my plan was to open a popup with no URL address field, and then just use window.print() to print the entire page which would've already been preformatted and prepopulated with the report data; I haven't tested it yet, but I'm assuming if I size the report content properly, it should print out neatly within 8 1/2 x 11 sheets of paper. Do you think this is viable?

My purpose for making this post was to get ideas from other people like yourself on even more streamlined ways than what I was intending to do. Your suggestion to output to PDF is a good one, but it would take more steps than my plan, both for the user and programmatically for myself. Do you know of any printing frameworks out there that would facilitate what I'm trying to do? I was hoping there'd be something like just adding a <script> tag the way we do in jQuery, and voila, we have a whole host of features available. Anything like that for printing?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil

ASKER

Thanks for your help, Julian.