简体   繁体   中英

How can I create a page based on image size in PDF::API2?

I am creating a pdf from a list of image files and I was wondering if it was possible to create each page of my pdf to be the size of whatever image I am currently adding - so they all fit and none of the larger ones get cropped or whatever.

Currently I'm creating pages like this: my $page = $pdf->page();

I have an object of the specific image as well. And if someone could tag this as PDF::API2 that'd be great.

I think you want to look at $pdf->mediabox() , $pdf->cropbox() , $pdf->bleedbox() , and $pdf->trimbox() .

You probably want to find the PDF spec to determine how these work, though.

Are you thinking of this purely for on-screen viewing? If print-size doesn't matter, you can do something like this:

use PDF::API2;
my $pdf = PDF::API2->new();

foreach my $filename (@list_of_jpeg_locations) {
    my $image = $pdf->image_jpeg($filename);

    my $width  = $image->width();
    my $height = $image->height();

    # Set the page size to equal the image size
    my $page = $pdf->page();
    $page->mediabox($width, $height);

    # Place the image in the bottom corner of the page
    my $gfx = $page->gfx();
    $gfx->image($image, 0, 0);
}

$pdf->saveas('/path/to/file.pdf');

You can tweak this code to scale the images to fit a particular printed page size, if need be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM