perl - 如何使用 PDF::API2::Lite 添加带有图像的页眉、页脚?

标签 perl pdf perl-module

是否可以添加带有图像的页眉(带有文本和一张图像)和页脚(带有页码)。我编写了下面的代码来创建一个显示 png 图像的 PDF 文档。

如果可以使用任何其他模块轻松完成此操作,请提出建议。非常感谢示例代码的回复。

use strict;
use PDF::API2::Lite;
use Getopt::Long;

my $outfile;
my $path;

my $options = GetOptions( "outfile=s" => \$outfile,
                          "images=s" => \$path,);

my @images = sort glob("$path") or die "No Files\n";

my $pdf = PDF::API2::Lite->new();
for my $png ( sort @images ) {
        my $image = $pdf->image_png( "$png" );
        $pdf->page(1150,450);
        $pdf->image($image, 10, 10);
}

$pdf->saveas( $outfile );

最佳答案

在 SO 上等待一天可以节省您 10 分钟阅读模块文档的时间。这并不难,太空。

use PDF::API2 qw();

{
    my $pdf = PDF::API2->open('input.pdf');

    for my $index (1 .. $pdf->pages) {
        my $page = $pdf->openpage($index);
        my $txt  = $page->text;
        $txt->textlabel(300, 700, $pdf->corefont('Helvetica Bold'), 12, 'some Header text');

        my $gfx = $page->gfx;
        $gfx->image($pdf->image_png('Header_image.png'), 150, 700);

        $txt->textlabel(300, 100, $pdf->corefont('Helvetica Bold'), 12, "Page: $index");
    }

    $pdf->saveas('output.pdf');
    $pdf->end;
}

关于perl - 如何使用 PDF::API2::Lite 添加带有图像的页眉、页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2947925/

相关文章:

perl - 验证 DBIx::Class 中的行数据的首选方法是什么?

c# - 如何使用 C# 将 Word 文档(基于 XML)转换为 PDF?

c# - 如何从按钮中获取文件

pdf - 由Pandoc从.md转换的投影仪幻灯片中的图非常大

windows - Perl 模块 - dist.ini 和特定于平台的先决条件

perl - 设置Perl模块参数并同时使用Exporter

perl - 寻找一种将观看次数最多的 youtube 视频输出到搜索词的文本文件的方法

regex - 理解 Perl 正则表达式修饰符/m 和/s

perl - 配置 OpenDDS

perl - CentOS8下如何安装perl DateTime模块。 yum install "perl(DateTime)"在 CentOS7 中工作,但在 8 中没有