Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Add Border to a PDF file

by artist (Parson)
on Jul 23, 2007 at 20:55 UTC ( #628332=perlquestion: print w/replies, xml ) Need Help??

artist has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,
How I can add border to a PDF file?
Thanks
(Updated ). Need to know, which modules does that? I am looking at PDF::ReportWriter or PDF::Reuse, PDF::Template etc and haven't found something worthwhile.
--Artist

Replies are listed 'Best First'.
Re: Add Border to a PDF file
by snoopy (Curate) on Jul 24, 2007 at 03:23 UTC
    Do you mean adding a border around each page?

    The following uses PDF::API2 directly to draw a 1 pt border 10pt (~3.5mm) in from the edge of the page

    #!/usr/bin/perl use warnings; use strict; use PDF::API2; my $pdf = PDF::API2->open('/tmp/test.pdf'); foreach my $pagenum (1 .. $pdf->pages) { my $page = $pdf->openpage($pagenum); # # Get the page size # my @mbox = $page->get_mediabox; my $gfx = $page->gfx; # # Draw a box 10pt in from the edges of the page # $gfx->linewidth(1); $gfx->strokecolor('#000000'); $gfx->rect($mbox[0]+10, $mbox[1]+10, $mbox[2]-20, $mbox[3]-20); $gfx->stroke; } $pdf->savas('/tmp/test1.pdf');
      Thanks, your code is a really a good beginning. Problem here that the border overwrites the content of the PDF file. In that cause, I would rather draw the border outside the content, or shrink the page first and then draw the border.
      --Artist
        Here's a second cut. This preserves the page size and shrinks the graphics by 10%:
        #!/usr/bin/perl use warnings; use strict; use PDF::API2; my $pdf_in = PDF::API2->open('/tmp/test.pdf'); my $pdf_out = PDF::API2->new; foreach my $pagenum (1 .. $pdf_in->pages) { my $page_in = $pdf_in->openpage($pagenum); # # create new page # my $page_out = $pdf_out->page(0); # # Get the page size # my @mbox = $page_in->get_mediabox; # # Inherit mediabox $page_out->mediabox(@mbox); # # # copy page as a form. reduce slightly. # my $xo = $pdf_out->importPageIntoForm($pdf_in, $pagenum); my $gfx = $page_out->gfx; $gfx->formimage($xo, my $_x = 20, my $_y = 20, my $_scale = .9); # # Draw a box 10pt in from the edges of the page # $gfx->linewidth(1); $gfx->strokecolor('#000000'); $gfx->rect($mbox[0]+10, $mbox[1]+10, $mbox[2]-20, $mbox[3]-20); $gfx->stroke; } $pdf_out->saveas('/tmp/test2.pdf');
Re: Add Border to a PDF file
by Khen1950fx (Canon) on Jul 23, 2007 at 23:23 UTC
    I think that your best bet is PDF::Table. You can set the border with border => 1 and you can choose a color such as border_color => 'red'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://628332]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-11-26 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?