Never underestimate Apache subrequests

So, our “whole application”: is written in HTML::Mason, running under mod_perl, etc. Everyone seems quite happy; it’s performing really well–we’re handling nearly 2M hits/day (with about a 10:1 graphics to HTML ratio) on a dual PIII/1Ghz app server and a similar DB server–and it’s pretty easy to get it to do whatever you want it to.

However, for reporting purposes, we have to produce something in a reasonable printable form. The only really portable print-oriented format that gets you good display control is .PDF. We’ve gone down the using-HTML-to-generate-print rathole for one report, and it’s too horrific to contemplate doing more.

There’s no good–by which I guess I really mean high-level–free way to produce PDFs in perl that I have found, and boy, have I looked. And the commercial tools that do what we need all seem to want multi-thousand dollar licenses for “server versions”. This is unattractively expensive.

But we still need pdf, and until xmlroff is in better shape, and its libfo is hooked into a perl module (boy, I wish I had time and energy to help with it, because it’s a cool system), there really aren’t any options that don’t involve using external processes.

So, we’ve started retrieving our report data in XML form, using Matt Sergeant’s XML::Generator::DBI, and we’ve got some stylesheets that do our conversions to HTML and CSV, and we’ll eventually do some to get the data in a form to feed to Spreadsheet::WriteExcel::FromXML so that we can get XLS files.

And we can write stylesheets to go to fo, and then we can run FOP on them, and, here’s the beauty part, our HTML::Mason component that does this can then just make an Apache subrequest to FOP’s output file, and Apache will take care of sending the PDF back for us.

It’s a simple as:

bc. system qq{/usr/bin/fop -q -fo $fo -pdf $pdf};
$m->auto_send_headers (0);
my $subrequest = $r->lookup_file ($pdf);
if ($subrequest->status == 200) {
$subrequest->run (1) ;
} else {
$m->abort (404);

We do similar things for doing access control on static graphics–we keep them absolutely outside of our document root, and then have a Mason dhandler that decides whether you’re allowed access, and if you are, let’s Apache take care of you.

I don’t think enough people use Apache subrequests.

Published by

Michael Alan Dorman

Yogi, brigand, programmer, thief, musician, Republican, cook. I leave it to you figure out which ones are accurate.