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

发布于 2024-09-03 15:47:08 字数 608 浏览 3 评论 0原文

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

Is it possible to add header(with text and one image) and footer (with page number) with images. I wrote below code to create a PDF document which shows png images.

If this can be done easily with any other module, please suggest.Really appreciate response with sample code.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一笑 2024-09-10 15:47:08

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

Waiting one day on SO saved you 10 minutes reading the module documentation. It's not difficult, 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;
}
深空失忆 2024-09-10 15:47:08

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

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

作为一般解决方案,我逐页创建一个新的 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::API2 is my workhorse for this sort of thing.

And I'll almost always use the importPageIntoForm method, as soon as I need to do any layup or reprocessing of an existing PDF document.

As a general solution, I create a new PDF, page by page, import the elements that I want to lay up, then add additional text or graphics.

#!/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);
那小子欠揍 2024-09-10 15:47:08

看看 PDF::API2::Simple< /a> 相反。此 CPAN 模块围绕 PDF::API2< /a> 包括页眉和页脚。

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

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/

Have a look at PDF::API2::Simple instead. This CPAN modules provides some handy helper methods around PDF::API2 including headers and footers.

Here is a simple working header/footer example:

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文