在Perl中制作PDF报告? [英] Make PDF reports in Perl?
问题描述
Perl的所有PDF库似乎有些野蛮 - 停留在20世纪80年代。您必须指定要进行布局的PostScript点。 Java有JasperReports,Ruby有Prawn,Python有ReportLab。有没有一个非灭绝的库/模块,将让我在不到一个星期的编码做一个漂亮的PDF? (我有点沮丧的PDF :: API2,PDF :: Table,等)我不想生成HTML和转换它。 Perl是报告的理想选择,但主报告文件格式不可用。人们使用哪些图书馆?
我需要:
- >
- 图表(图片)
- 颜色
- 格式化b $ b
- 标题/页脚
我稍微打开了外部(非Perl)开源工具,if绝对需要。但不是真正感兴趣的一个主要的Java服务器方法。对于赏金,我想要一个纯Perl方法,因为我想在一个服务器上运行,我不能添加多个模块。
如果LaTeX太大,或许有人可以使用 Inline :: Python
封装ReportLab ,所有人似乎都喜欢这么多(我没有使用它,我不太熟练Python)。
编辑3:
这里是编辑2,除了分为模块化样式,如果人们喜欢它(如果它是一种鲁棒)可以发布到CPAN。现在将 .pm
文件放置在 Inline / Python / ReportLab.pm
文件结构中 @INC
(脚本自己的基目录通常在 @INC
)。
#Inline / Python / ReportLab.pm
package Inline :: Python :: ReportLab;
use strict;
使用警告;
使用Carp;
使用Inline :: Python qw / py_eval /;
我们的@ISA ='Inline :: Python :: Object';
sub import {
py_eval('from reportlab.pdfgen.canvas import Canvas');
}
sub new {
my $ class = shift;
my $ filename = shift || croak必须指定文件名为contructor;
return bless(Inline :: Python :: Object-> new('__ main__','Canvas',$ filename),$ class);
}
1;然后脚本可以是:
#!/ usr / bin / env perl
use strict;
使用警告;
使用Inline :: Python :: ReportLab;
my $ c = Inline :: Python :: ReportLab-> new('hello.pdf');
$ c-> drawString(100,100,Hello World);
$ c-> showPage();
$ c-> save();
编辑2:
虽然编辑1仍然感兴趣,我错了!),我已经想出了如何创建一个'Canvas'的实例并直接公开其方法:
!/ usr / bin / env perl
使用strict;
使用警告;
使用Inline :: Python qw / py_eval /;
py_eval('from reportlab.pdfgen.canvas import Canvas');
my $ c = Inline :: Python :: Object-> new('__ main__','Canvas','hello.pdf');
$ c-> drawString(100,100,Hello World);
$ c-> showPage();
$ c-> save();
编辑2/3:作为更手动界面的示例。我认为Edits 2/3提供了一个更好的接口,这使得原来的Python类没有(太多)包装的重担。
编辑1:的功能通过手动黑客的方法。这意味着对于每个想要使用的方法,都必须添加一个包装器方法。虽然这已经是一个可行的解决方案,我想知道是否没有一些更简单的方法来暴露整个python'canvas'类,但现在这是我在哪里:
#!/ usr / bin / env perl
use strict;
使用警告;
使用Inline Python => << END_PYTHON;
from reportlab.pdfgen import canvas
类Canvas:
def __init __(self,filename):
self.canvas = canvas.Canvas(filename)
def drawString(self,x,y,text):
self.canvas.drawString(x,y,text)
def save(self):
self.canvas.showPage
self.canvas.save()
END_PYTHON
my $ c = Canvas-> new('hello.pdf');
$ c-> drawString(100,100,Hello World);
$ c-> save();
All the PDF libraries for Perl seem a bit barbaric -- stuck in the 1980's. You have to specify PostScript points to do layout. Java has JasperReports, Ruby has Prawn, and Python has ReportLab. Is there a non-extinct library/module that will let me make a nice looking PDF in less than a week of coding? (I'm a little frustrated by PDF::API2, PDF::Table, etc.) I don't want to generate HTML and convert it. Perl is ideal for reporting, but the main report file format is not available in a usable way. What libraries do people use?
I need:
- tables
- charts (Images)
- color
- formatting (ideally automatic, not pixel by pixel)
- headers / footers
I'm slightly open to wrapping external (non-Perl) open source tools, if absolutely needed. But not really interested in a major Java server approach. For the bounty, I want a pure Perl approach, since I want to run this on a server that I can't add more than modules to. If you have a public example that works well, please point me to it.
If LaTeX is too big, perhaps one could use Inline::Python
to wrap ReportLab, that everyone seems to like so much (I haven't used it and am not too proficient at Python).
Edit 3:
Here is Edit 2, except split into a modular style, if people like it (and if it is an kind of robust) perhaps I can publish to CPAN. For now place the .pm
file in a file structure like Inline/Python/ReportLab.pm
somewhere in your @INC
(the script's own base directory is usually in @INC
).
# Inline/Python/ReportLab.pm
package Inline::Python::ReportLab;
use strict;
use warnings;
use Carp;
use Inline::Python qw/py_eval/;
our @ISA = 'Inline::Python::Object';
sub import {
py_eval('from reportlab.pdfgen.canvas import Canvas');
}
sub new {
my $class = shift;
my $filename = shift || croak "Must specify file name to contructor";
return bless(Inline::Python::Object->new('__main__', 'Canvas', $filename), $class);
}
1;
Then a script could be something like:
#!/usr/bin/env perl
use strict;
use warnings;
use Inline::Python::ReportLab;
my $c = Inline::Python::ReportLab->new('hello.pdf');
$c->drawString(100,100,"Hello World");
$c->showPage();
$c->save();
Edit 2: While Edit 1 is still of interest, it seems (tell me if I am incorrect!) that I have figured out how to create an instance of 'Canvas' and expose its methods directly:
#!/usr/bin/env perl
use strict;
use warnings;
use Inline::Python qw/py_eval/;
py_eval('from reportlab.pdfgen.canvas import Canvas');
my $c = Inline::Python::Object->new('__main__', 'Canvas', 'hello.pdf');
$c->drawString(100,100,"Hello World");
$c->showPage();
$c->save();
Edit 2/3: This portion is left as an example of a more manual interface. I think Edits 2/3 give a better interface which leaves the heavy lifting to the original Python class without (too much) wrapping.
Edit 1: I have now exposed some of the functionality by manually hacking in the methods. This means that for every method one wants to use, a wrapper method must be added. While this is already a feasible solution, I wonder if there isn't some easier way to expose the entire python 'canvas' class, but for now this is where I am:
#!/usr/bin/env perl
use strict;
use warnings;
use Inline Python => <<END_PYTHON;
from reportlab.pdfgen import canvas
class Canvas:
def __init__(self,filename):
self.canvas = canvas.Canvas(filename)
def drawString(self,x,y,text):
self.canvas.drawString(x,y,text)
def save(self):
self.canvas.showPage()
self.canvas.save()
END_PYTHON
my $c = Canvas->new('hello.pdf');
$c->drawString(100,100,"Hello World");
$c->save();
这篇关于在Perl中制作PDF报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!