Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

PDF::API2 : re-sizing the mediabox once drawn/ crop pdf to actual content

by aegidel (Initiate)
on Dec 14, 2011 at 12:43 UTC ( [id://943541]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all, i'm trying to create a label-sized pdf printout (brother ql 580n, 62mm media endless paper) under debian 2.6.39-bpo.2-686-pae with perl v5.10.1 (*) built for i486-linux-gnu-thread-multi that dynamically adjusts the page height based on content. So far i have
#!/usr/bin/perl use strict; use warnings; use PDF::API2; use PDF::TextBlock; use constant mm => 25.4 / 72; use constant in => 1 / 72; use constant pt => 1; my ( $paragraph1, $paragraph2) = ('Headertext', 'Testtext zur Ausgabe +in einem selbstgebauten PDF mit den richtigen Maßen. Mal sehen, was d +as gibt! ÄÜÖüäößéèàá'); my $pdf = PDF::API2->new( -file => "./test.pdf" ); my $page = $pdf->page; $page->mediabox (62/mm, 100/mm); #$page->cropbox (2/mm, 4/mm, 60/mm, 92/mm); # printable area my %font = ( Helvetica => { Bold => $pdf->corefont( 'Helvetica-Bold', -encoding => 'latin +1' ), }, Times => { Roman => $pdf->corefont( 'Times', -encoding => 'latin1' ), }, ); # do some umlaut conversions here .... (snipped) my $tb = PDF::TextBlock->new({ pdf => $pdf, page => $page, fonts => { b => PDF::TextBlock::Font->new({ pdf => $pdf, font => $font{'Helvetica'}{'Bold'}, size => 16/pt, fillcolor => '#000000', }), p => PDF::TextBlock::Font->new({ pdf => $pdf, font => $font{'Times'}{'Roman'}, size => 14/pt, fillcolor => '#000000', }), }, }); $tb->text( "<b>$paragraph1</b>\n\n<p>$paragraph2</p>" ); # set the start of the paragraph and maxwidth/ height $tb->x(7/mm); $tb->y(82/mm); $tb->w(50/mm); $tb->h(95/mm); my ($endw, $ypos) = $tb->apply; $pdf->save; $pdf->end;
All my tries (recalling mediabox, trimbox etc after $tb->apply etc) so far have ended up cropping the top part of the pdf (due to 0.0 origin at bottom left i suppose) or resizing to letter.. However, calculating a cut-off region by doing
my $cutAt = ($ypos + 20) / 2.45;
i would like to resize the PDF to the width=62 mm (as defined, doesn't change) and height=$cutAt (e.g. 60mm) FROM THE TOP. I've been trying without success, any ideas on how this can be achieved? Thanks!

Replies are listed 'Best First'.
Re: PDF::API2 : re-sizing the mediabox once drawn/ crop pdf to actual content
by Eliya (Vicar) on Dec 14, 2011 at 17:51 UTC

    A PDF MediaBox is represented by four parameters: x,y of the lower left corner, and x,y of the upper right corner (with 0,0 being located at the bottom left of the page).  And $page->mediabox(62/mm, 100/mm) is a short form of saying $page->mediabox(0, 0, 62/mm, 100/mm).

    If you need to reduce the MediaBox at its bottom end, you don't want the lower left corner to be 0,0, so you have to use the four argument version of $page->mediabox().

    62mm,100mm --------------------- | | | This is your | | | | textblock | | | | foo bar baz | | | | end. | | | 0,$ypos|.....................| | | | | | | | | --------------------- 0,0

    In other words, if I'm understanding your requirements correctly, all you need is

    ... my ($endw, $ypos) = $tb->apply; $page->mediabox(0, $ypos, 62/mm, 100/mm); # <--- $pdf->save; $pdf->end;

    (AFAICT, $ypos - as returned by $tb->apply() - is the current y-position (cursor) after the block has been rendered, i.e. your y-coordinate of the lower left corner.)

    Update: just tested it, and it works fine for me.

      Elyia,

      thanks so much. Exactly what i was looking for and well explained to a PDF newb.

      Now i just have to figure out why the brother ql-580n won't honour the custom media sizes i define with brpapertool/lpoptions and keeps printing out 100mm of paper every time though pdfinfo shows the pdf is correctly defined. Oh well..

      Again: thanks for your input!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-18 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found