Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Printing reports in web apps

I've got an idea for an .Net application that would be perfect as a web app - simple UI's, data centric, widely used in an enterprise, etc.  The only drawback is that I have no experience with creating printable reports in web apps.  It's a simple app that I'm doing mainly to teach myself, so I don't want to have to use a business intelligence tool like Crystal Reports ($$). Even though I'm doing it as a learning avenue, it still would be a viable application - but only if I could print reports.

What are my options?


Friday, March 28, 2003

Do you mean printing on the client side or on the web server?

Using HTML to produce output for a printer is a frustrating experience if your doing reports that are more sophisticated than a simple header/body/footer report. 

If your doing simple reports and you can guarantee that your clients are using IE, checkout the tbody attribute style="display: table-header-group" to add column headers to the top of each page.

Another option would be to use a PDF writer.  There are a number of them that exist.  I'm using one for classic ASP at the moment and I would recommend it. 

Crystal Reports would probably work, but you would require that your target audience have to download the Crystal Reports web report viewer (or whatever its called).

Guy Incognito
Friday, March 28, 2003

I never considered printing on the server.  I assume you would need to create an separate document to print in addition to the HTML page. How would you enable the user to route the print job to a nearby printer of their choice? Would all the possible printer drivers in the company need to be loaded on the web server?

If I chose to print the HTML, can the IE header and footer be manipulated programatically?  I have developed a printable HTML form and printed it with Javascript before, but I could never figure out how to manipulate these.

I suppose the PDF generated option would be the easiest, but I've never liked the quality of the output that much.


Friday, March 28, 2003

Yeah, in order to print using the web server, you'd have to configure the printers on the web server, which might only be an option if your developing this for an intranet.  You'd also have some fun monkeying around with registry/user access permissions, depending on the user your web server process runs under.  This is probably only a viable option if your goal is to limit printing to specific devices (such as high-speed multi-part form printers).

You can manipulate IE's page header/footer with script embedded in a web page, but it would involve making registry changes on the users computer.  By default IE does not allow this access, but you could mess around with security under IE to accomplish the task.  I think the correct setting is to enable "Initialize and Script ActiveX Controls not marked as safe."  I think this setting can be configured on a "zone" basis, so you'd only want enable it for trusted sites such as an intranet.

Here is some sample code, I dug up from an old project:

Const REPORT_HEADER = "&w" ' The page title.
Const REPORT_FOOTER = "Page &p of &P&b&d"

Sub PrintCurrentPage(ByVal NewHeader, ByVal NewFooter)
    Const IE_PAGESETUP_KEY = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup"

    Const HEADER = "\header"
    Const FOOTER = "\footer"
    Dim OldHeader
    Dim OldFooter
    Dim OldMarginLeft
    Dim OldMarginRight
    With CreateObject("WScript.Shell")

          ' Read the old values.
          OldHeader = .RegRead(IE_PAGESETUP_KEY & HEADER)
          OldFooter = .RegRead(IE_PAGESETUP_KEY & FOOTER)
          ' Set the current temporary values.
          Call .RegWrite(IE_PAGESETUP_KEY & HEADER, NewHeader, "REG_SZ")
          Call .RegWrite(IE_PAGESETUP_KEY & FOOTER, NewFooter, "REG_SZ")
          Call Window.Print()
          ' Reset the old values.
          Call .RegWrite(IE_PAGESETUP_KEY & HEADER, OldHeader, "REG_SZ")
          Call .RegWrite(IE_PAGESETUP_KEY & FOOTER, OldFooter, "REG_SZ")

    End With
End Sub

This seems more like a hack to me, and I still don't know why IE (or for that matter HTML) doesn't allow you more control over printing a document.  Maybe this will be addressed in HTML.NEXT_VERSION?

You might want to also search the regular JoelOnSoftware forum, because I think I remember this topic from a while back...?

Guy Incognito
Saturday, March 29, 2003

Personally prefer to PDFs. Thus you can tune pages or whatever.. Printing HTML forms/tables give you some overhead, and if you work with printing complex HTML-matrix-reports is definitely a nightmare. You cannot tune pagination perfectly. Also can use @media (in CSS or style defs)  to make a printable versions smaller or different than screen versions..  Other options are using client side ActiveXs or Java applets, which are related with download some code, which are not very good and pragmatic way.
The best way is genrating a PDF file than give user options for downloading / browsing-printing.

Hakan Kazaz
Tuesday, April 01, 2003

*  Recent Topics

*  Fog Creek Home