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

I know that PDF::API2 has the "rotate" and "translate", but am I right in thinking this applies to the whole page content?

What I am TRYING to do is arrange 16x A4 prints into an A0 PDF. I THOUGHT I could load an image, rotate it, and move it to X/Y co-ordinates, using a loop to move across and up. The images are created at 300DPI, so the inches dimensions when divided by 72 come out at around 595 x 842 (portrait mode when rotated)

I also thought that the bottom left corner once rotated would be 595 to the right of the true PDF 0,0 co-ordintates of bottom left of PDF.

Alas, at present, it appears to be adding a photo, rotating page, adding another, rotating page and so on. So its not moving up the page as expected either.

Have I got to rotate my original images, and then just use co-ordinates to place? Or is there a potential workaround?

my $dir='../../wordboards/standard'; opendir my $dh, $dir or die "Could not open '$dir' for reading '$!'\n" +; my @pics = readdir $dh; closedir $dh; shift @pics; shift @pics; my $pdf = PDF::API2->new(); # Set the page size my $page = $pdf->page(); $page->mediabox('A0'); #Each A4 is 841 x 595 my $content = $page->gfx; for (my $row=0; $row<4; $row++){ for (my $col=0; $col<4; $col++){ my $count=($row*4)+$col; my $photoFile = $pdf->image_jpeg($dir."/".$pics[$count]); $content->image($photoFile, 0, 0); $content->transform( -translate => [my $_x = 595+($row*595), my $_y = ($col*8 +42)], # Top left is 0,0 when rotated (ie bottom left is 842 points + to right) -rotate => 90, ); } }

Since an image is usually placed bottom left to top left, once rotated 90 degrees, my theory is that it would be printed in columns of data running right to left, hence moving start co-ordinates to the right

Replies are listed 'Best First'.
Re: Making a 4 x 4 of rotated images (PDF::API2)
by vr (Curate) on Dec 08, 2019 at 12:30 UTC

    Of course you don't have to rotate original files. Also, the correct POV is that you don't move/rotate/scale images or any other graphic objects. It's coordinate system that's being transformed, and changes are cumulative. Instead of keeping track of transforms applied so far, use push/pop stack frame, returning each time to unscaled/unrotated LL page corner as 0,0.

    In fact, the "image" method does save/transform(translate to X,Y and scale to WxH)/restore, because image is 1x1 box "filled" with all its pixels. So there are now several transforms and stack frames for each image. That may seem wasteful but it isn't. Be generous with "save"/"restore" calls if required. (What's wasteful is importing the same image 16 times, but it's done only for sake of illustration.)

    The "translate" and "image" calls could be combined in obvious way, and you can omit centering images inside A4 boxes altogether, if they are A4 as you say.

    use strict; use warnings; use LWP::Simple; use PDF::API2; use constant { JPG => 'jpg.jpg', PDF => 'pdf.pdf', URL => 'https://jpeg.org/images/jpeg-home.jpg', A4W => 595, A4H => 842, RES => 72, # image above is at 72 dpi }; -f JPG or is_success getstore URL, JPG or die; my $pdf = PDF::API2-> new; my $page = $pdf-> page; $page-> mediabox( 'A0' ); my $content = $page-> gfx; for my $row ( 0 .. 3 ) { for my $col ( 0 .. 3 ) { $content-> save; $content-> transform( -translate => [ A4W + $row * A4W, $col * A4H ], -rotate => 90, ); my $jpeg = $pdf-> image_jpeg( JPG ); my $w = 72 / RES * $jpeg-> width; my $h = 72 / RES * $jpeg-> height; $content-> translate(( A4H - $w )/2, ( A4W - $h )/2 ); $content-> image( $jpeg, 0, 0, 72 / RES ); $content-> restore; } } $pdf-> saveas( 'pdf.pdf' );
Re: Making a 4 x 4 of rotated images (PDF::API2)
by jcb (Parson) on Dec 07, 2019 at 23:11 UTC

    Try placing the images in a grid and then rotating the page after all images are placed. Think about laying out the pictures working from the side instead of from the bottom.