[cgiapp] Output PDF instead of HTML?
Richard Jones
ra.jones at dpw.clara.co.uk
Tue Sep 3 04:35:18 EDT 2013
On 03/09/2013 06:09, Jerry Kaidor wrote:
> It sometimes would be nice to be able to serve PDF data instead of
> HTML. These are not disk files: they are PDF reports that are
> algorithmically generated. ( Generally, I use perl to generate TeX and
> convert it to PDF with pdflatex ).
Hi Jerry,
I hope I haven't misunderstood your issue and am telling you something
you already know, but doesn't $self->header_add() work? Something like this:
sub print_record : Runmode {
my $self = shift;
my $pdf = _render_pdf();
# set header type to pdf (+ don't cache):
$self->header_add(-type => 'application/pdf', -expires => 'now');
return $pdf;
}
sub _render_pdf {
my @args = (
'--margin-top 7',
'--header-spacing 2',
[...],
);
# create temp file for input to wkhtmltopdf:
my $tmp_file = '/tmp/$filename.html; # $filename defined elsewhere
io($tmp_file)->print(${$content}); # $content is tt rendered elsewhere
my $pdf = `wkhtmltopdf -q @args $tmp_file -`;
io($tmp_file)->unlink; # delete temp file
return $pdf;
}
I have omitted some steps where $content is generated using Template
Toolkit (hence the deref step), and the filename is generated
dynamically. You will see I do use a temp file to hold the html while
the pdf is generated (using IO::All) but this is hidden from the user
and it generally works without issue. It just needs wkhtmltopdf
somewhere the web server can find it. You may not need the wkhtmltopdf
function, but the $self->header_add() function should still work if the
pdf generation is valid.
--
Richard Jones
More information about the cgiapp
mailing list