Reputation: 2501
i want to generate some pdf documents on my website using perl + PDF::API2.
1) read pdf template
2) add some data to template
3) save file to hdd
4) read new file
5) print it to user's browser
#!/usr/bin/perl
use strict;
use PDF::API2;
my $p = PDF::API2->open('/way/to/input/pdf.pdf');
my $font = $p->ttfont('/way/to/font.ttf', -encode => 'utf8');
my $page = $p->openpage(1);
my $text = $page->text();
$text->font($font,12);
$text->translate(150,150);
$text->text('Hello world!');
$p->saveas('/way/to/out/pdf.pdf'); #ex: '/usr/local/www/apache22/data/docs'
my $fileContent;
open(my $file, '<', '/way/to/out/pdf.pdf') or die $!;
binmode($file);
{
local $/;
$fileContent = <$file>;
}
close($file);
binmode STDOUT;
print "Content-type: application/pdf\n\n";
print $fileContent;
exit;
How can i do this without storing temporary PDFs in /way/to/out/ folder (r/w accessable)?
Upvotes: 1
Views: 778
Reputation: 943220
See the documentation for PDF::API2; immediately after it describes the saveas
method you are using, it shows how to use the stringify
method, which does what you want.
$text->text('Hello world!');
binmode STDOUT;
print "Content-type: application/pdf\n\n";
print $p->stringify();
exit;
Upvotes: 8