Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
"# does not rotate correctly because I don't quite understand the translate part"

When you rotate the page ($page->rotate(90)) to change from portrait to landscape, the X-Y axes change from horizontal-vertical to vertical-horizontal. The -translate values reference the axes; you need to adjust them for the page rotation change. I suspect this is maybe causing you problems understanding what's going on here: it's neither immediately obvious nor intuitive, so your confusion is unsurprising. I suggest you spend some time playing around with the values to get a feel for this.

The following code generates a landscape-oriented page which roughly looks like this:

| Left text    Right text |
|                         |
| +------+     +------+   |
| |  ^^  |     |  ^^  |   |
| |  ||  |     |  ||  |   |
| | img1 |     | img2 |   |
| +------+     +------+   |

I used different text and images purely for testing purposes; obviously, you can use the same text and images on both sides. The images were 64x64 pixel PNGs which had a clear UP direction (to check the rotation).

You'll no doubt need to change certain values to suit your layout requirements; however, I hope this gives you a solid starting point.

#!/usr/bin/env perl use strict; use warnings; use autodie; use PDF::API2; my $pdf_file = 'test.pdf'; my $left_text = 'This text on the left.'; my $right_text = 'This text on the right.'; my ($left_img, $right_img) = qw{left.png right.png}; my $pdf = PDF::API2::->new(-file => $pdf_file); my $page = $pdf->page(); $page->mediabox('A4'); $page->rotate(90); my $font = $pdf->corefont('Helvetica'); my @text_cfg = ( { text => $left_text, pos => [50, 40] }, { text => $right_text, pos => [50, 450] }, ); for my $cfg (@text_cfg) { my $text = $page->text(); $text->font($font, 12); $text->transform( -translate => $cfg->{pos}, -rotate => 90, ); $text->text($cfg->{text}); } my @gfx_cfg = ( { img => $pdf->image_png($left_img), pos => [150, 40] }, { img => $pdf->image_png($right_img), pos => [150, 450] }, ); my $gfx = $page->gfx; for my $cfg (@gfx_cfg) { $gfx->save(); $gfx->transform( -translate => $cfg->{pos}, -rotate => 90, ); $gfx->image($cfg->{img}, 0, 0); $gfx->restore(); } $pdf->save();

— Ken

In reply to Re: PDF API2 - landscape and rotating text and image by kcott
in thread PDF API2 - landscape and rotating text and image by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2021-02-26 16:10 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found