【问题标题】:How to add header, footer with images using PDF::API2::Lite?如何使用 PDF::API2::Lite 添加带有图像的页眉、页脚?
【发布时间】:2011-02-26 05:40:25
【问题描述】:

是否可以在图像中添加页眉(带有文本和一张图像)和页脚(带有页码)。我编写了下面的代码来创建一个显示 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 );

【问题讨论】:

    标签: perl pdf perl-module


    【解决方案1】:

    在 SO 上等待一天为您节省了 10 分钟阅读模块文档的时间。不难,Space。

    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;
    }
    

    【讨论】:

    • 感谢 Daxim:此代码用于编辑现有文档。你能建议我如何使用它来创建带有图像的新文档
    • 使用new constructor。请务必阅读文档。
    • 谢谢 daxim:你能不能给我建议一下如何向 pdf doc 添加属性。
    • 为您的新问题打开一个新问题:stackoverflow.com/questions/ask
    【解决方案2】:

    PDF::API2 是我处理这类事情的主力。

    我几乎总是会使用importPageIntoForm 方法,只要我需要对现有 PDF 文档进行任何布局或重新处理。

    作为一般解决方案,我逐页创建一个新 PDF,导入我想要放置的元素,然后添加其他文本或图形。

    #!/usr/bin/perl
    use warnings; use strict;
    
    use PDF::API2;
    
    my $infile = shift (@ARGV);
    my $outfile = shift (@ARGV);
    
    die "usage $0: infile outfile"
    unless $infile && $outfile;
    
    my $pdf_in = PDF::API2->open($infile);
    my $pdf_out = PDF::API2->new;
    
    foreach my $pagenum (1 .. $pdf_in->pages) {
    
      my $page_in = $pdf_in->openpage($pagenum);
      #
      # create a new page
      #
      my $page_out = $pdf_out->page(0);
    
      my @mbox = $page_in->get_mediabox;
      $page_out->mediabox(@mbox);
    
      my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum);
    
      #
      # lay up the input page in the output page
      # note that you can adjust the position and scale, if required
      #
      my $gfx = $page_out->gfx;
    
      $gfx->formimage($xo,
              0, 0, # x y
              1);   # scale
    
      #
      # add page number text
      #
      my $txt = $page_out->text;
    
      $txt->strokecolor('#000000');
    
      $txt->translate(
              my $_x = 200,
              my $_y = 50
      );
    
      my $font = $pdf_out->corefont('Courier');
      $txt->font($font, 12);
      $txt->text( 'Page: '.$pagenum );
    
      #
      # add header image
      #
    
      my $header_img = $pdf_out->image_png('SomeHeader.png');
      $gfx->image($header_img, 0, 400);
    }
    
    $pdf_out->saveas($outfile);
    

    【讨论】:

    • 这是一个非常棒的方法——而不是在原地修改 pdf,而是使用它作为模板来创建新的 pdf。感谢您告诉我们有关 importPageIntoForm() 的信息!
    【解决方案3】:

    改为查看PDF::API2::Simple。这个 CPAN 模块围绕 PDF::API2 提供了一些方便的辅助方法,包括页眉和页脚。

    这是一个简单的页眉/页脚示例:

    use 5.012;
    use warnings;
    use PDF::API2::Simple;
    
    our $PageNo;
    
    my $pdf = PDF::API2::Simple->new(
        file   => 'file.pdf',
        header => \&header,
        footer => \&footer,
    );
    
    $pdf->add_font('Verdana');
    
    for my $page (1..3) {
        $pdf->add_page;
        $pdf->image( 'image.png', x => 300, y => 300 );
    }
    $pdf->save;  
    
    
    sub header { shift->text( 'Header text here' ) }
    sub footer { shift->text( 'page:  ' . ++$PageNo, x => 10, y => 10 ) }
    

    /I3az/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 2015-09-02
      • 2020-07-23
      • 2013-09-30
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      相关资源
      最近更新 更多