PhilterPaper / Perl-PDF-Builder

Extended version of the popular PDF::API2 Perl-based PDF library for creating, reading, and modifying PDF documents
https://www.catskilltech.com/FreeSW/product/PDF%2DBuilder/title/PDF%3A%3ABuilder/freeSW_full
Other
6 stars 7 forks source link

[RT 132403] Image rotation has a strange effect on its position #115

Closed PhilterPaper closed 4 years ago

PhilterPaper commented 4 years ago

Date: Thu, 23 Apr 2020 10:08:47 +0200 To: bug-PDF-API2 [...] rt.cpan.org From: Fernando Santagata <nando.santagata [...] gmail.com>

Hi,

When I first tried to rotate an image using $gfx->rotate(90); I found out that the image was not visible on the page anymore. After a series of attempts I found out that in order to make the image appear on the page I needed to place it outside the visible page, i.e. when I issue the $gfx->image call I have to pass a negative starting x position.

I tried this on several images, with the same result.

This is the program I'm running:

use PDF::API2;

my ($output, $file) = @ARGV;
my $pdf = PDF::API2->new(-file => $output);
my $page = $pdf->page;
$page->mediabox('A4');
my $gfx=$page->gfx;
$image = $pdf->image_jpeg($file);

my $ratio = $image->width / $image->height;
my $x1 = 50;
my $y1 = -500; # position out of page
my $xlen = int(400 * $ratio);
my $x2 = $x1 + $xlen;
my $y2 = 400;

$gfx->rotate(90);
$gfx->image($image, $x1, $y1, $x2, $y2);

$gfx->close;
$gfx->stroke;
$pdf->update;
$pdf->end;

The program works perfectly, it's just that it sounds wrong to have to position the image out of the page. Is there anything I'm missing?

I'm using PDF::API2 v2.037 perl v5.30.0 on a Debian sid.

Thank you -- Fernando Santagata

PhilterPaper commented 4 years ago

Thu Apr 23 21:21:01 2020 PMPERRY [...] cpan.org - Correspondence added

I think it's working correctly. You are rotating the page 90 degrees counterclockwise (anti-clockwise) around the current (0,0) origin. As you have not moved the origin, it's at the lower left of the original page, and it remains at the lower left of the rotated page. Therefore the entire original page (+Y) is offscreen to the left, X moves from 0 at the bottom upwards (to the original HEIGHT), and Y is negative over the visible page (0 at left, downwards by the original WIDTH). Therefore, a negative value of Y would be expected. Perhaps you are expecting the origin to shift to the lower RIGHT of the visible page? It doesn't.

Thu Apr 23 21:42:14 2020 PMPERRY [...] cpan.org - Correspondence added

A further thought: you may have confused the graphics context "rotate" with the page "rotate" ($page->rotate, done just after defining $page). That one seems to behave more the way you were expecting.

I have updated the documentation for both rotate() calls in PDF::Builder, to clarify this.

Fri Apr 24 01:58:22 2020 nando.santagata [...] gmail.com - Correspondence added

On Fri, Apr 24, 2020 at 3:42 AM Phil M. Perry via RT < bug-PDF-API2@rt.cpan.org> wrote:

<URL: https://rt.cpan.org/Ticket/Display.html?id=132403 >

A further thought: you may have confused the graphics context "rotate" with the page "rotate" ($page->rotate, done just after defining $page). That one seems to behave more the way you were expecting.

I use both $gfx->rotate and $page->rotate in my original program, because I want to show a landscape image as large as possible and I want to allow the user to see it as easily as possible. I thought that rotating the $gfx would reposition the image, but - as I reported originally - the program works fine so I'm OK.

I have updated the documentation for both rotate() calls in PDF::Builder, to clarify this.

Thank you!

-- Fernando Santagata