Hi perlers,

This is a rather specialised query about the module PDF::API2.

I am trying to create PDFs on the fly. The first attempt worked fine 
and I was delighted with the results. Then I was asked to make 
something similar with landscape orientation. It started well but I 
have hit a problem and I can't see what is going wrong.  

To make the page landscape I have used the 'rotate' method. Similarly 
I used the rotate method on the graphics handle, otherwise the images 
appears in portrait while the page is in landscape. It is this rotate 
that is causing the problem.  

Normally I maintain the spacing of the images by incrementing the Y 
axis value ($y) by the width of the last image, for test purposes I 
am simply incrementing $y by 162 pixels. Once the graphics is rotated 
all hell breaks loose with the lay-out and I can not see where the 
image is laid on the page. I have tried a number of values (fixed and 
incrementing) yet I can not see how I can rotate the image and 
maintain control over $y.   

I have attached a skeleton of script I am working on. The image file 
can be anything. If it is run unmodified you should see 4 jpegs along 
the top and 4 along the bottom, unrotated. Once line 99 is 
uncommented:
#       $photo->rotate($rotation); 

the images start to disappear, presumably off the page.  

$x is pretty constant either 18 or 270. 

I know this is a bit specialised but if anyone has any ideas they'd 
be much appreciated. Thanx.
Dermot.


#!/bin/perl


use XML::Simple;
use PDF::API2;
use Image::Magick;
use strict;
use warnings;

use constant pt => 1;


# === The Rotation ====
# need to apply this to all import elements;

my $rotation = 90;

# Create a new PDF object;
my $pdf = PDF::API2->new(-file => "mypdf.pdf");

$pdf->preferences(
                -singlepage => 1,
                -thumbs => 1,
                -fith => 1, 
        );


# ========== Create the first page.=====
# Acceptable values for media include A4 but it is more useful to 
# know the pixel size.
# Start ny creating a pdf page object

my $page = $pdf->page;

# and rotate is 90 degrees to make its orientation landscape
$page->rotate(90);

# === specify media (page) size
# Crop size needs to specified as slightly less then media 
# or your'll get ink on the rollers.

$page->mediabox(595, 842);
$page->cropbox(590.5, 840.5);

# Start the X & Y coordinates for the page.
# coordinates are given from the bottom left corner.

my $y = 12;
my $x = 18;

# ===== Scale =====
# specify the scaling here as it is dotted around the program.

my $scale = .72;

# my $no = @ref;
my $no = 12;


print "No of record is $no\n";

my $newpage = 0;
my $newrow = 0;
my $newpagestarted;


for (my $i = 0; $i < $no; ++$i ) {
        
        if ($newpage == 8) {
                $newpage = 0;
                $page = $pdf->page;
                $page->rotate($rotation);
        }
        else {
                ++$newpage;
        }

        if ($newrow == 4) { 
                $newrow = 0;
                $x = 280;
                $y = 18;
        }
        else {
                ++$newrow;
        }


# ==== Graphic part =========
# First create a new grpahics handle for the thumbnail
 
        my $photo = $page->gfx;


# Rotate the Graphic handle the same as the page.
# This is where the problems begin. If the image is rotated
# it becomes impossible to maintain the sequence. 

 
#       $photo->rotate($rotation);
        
# Get the path for actual file from the splnum subroutine
        my $img_file = &makepath();

# We die if I can't find the image file.
# It will probably be desirable to warn instead 
        die("Unable to find image $img_file: $!\n") unless -e $img_file;

# Create a Image::Magick object to interigate the image
# file. We nede to knwo the dimension of the image to align 
# the text correctly.

        my $image = Image::Magick->new;

# Use the ping method to get the height and width as ping 
# doesn't open the file, it is the fastest way.
        my ($img_width, $img_height, $size, $format) = $image->Ping($img_file);

        
# Define the image as a jpeg.
        my $jpeg = $pdf->image_jpeg("$img_file");

# Insert the image

        $photo->image($jpeg,$x,$y,$scale);
        print "inserting $img_file at x=$x, y=$y\n";

# Set $y to the base of the image so we can keep a 
# consistant margin.
        $y += 162;

}

$pdf->save;


#========== SUBS ===============

sub makepath {


#my $root = "/data/images/";
my $root = "$ENV{USERPROFILE}"."\\desktop\\pdf\\test\\";
my $num = shift;

my $fqn = "$root"."img.jpg";
return ($fqn);
 
}





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Reply via email to