2013-03-26 98 views
1

我想使用perl + PDF :: API2在我的网站上生成一些pdf文档。
使用perl生成pdf

1)阅读PDF模板
2)一些数据添加到模板
3)保存文件到硬盘
4)读取新的文件
5)打印到用户的浏览器

#!/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; 

如何在不将临时PDF存储在/ way/to/out /文件夹(r/w可访问)的情况下执行此操作?

回答

8

参见the documentation for PDF::API2;在它描述了您正在使用的saveas方法后,它立即显示如何使用stringify方法,该方法按您的要求进行。

$text->text('Hello world!'); 
binmode STDOUT; 
print "Content-type: application/pdf\n\n"; 
print $p->stringify(); 
exit; 
+0

谢谢,解决了。版主可以关闭这个问题 – Suic 2013-03-26 14:03:11