PDF::FromImage - Create PDF slide from images


PDF-FromImage documentation Contained in the PDF-FromImage distribution.

Index


Code Index:

NAME

Top

PDF::FromImage - Create PDF slide from images

SYNOPSIS

Top

    use PDF::FromImage;

    my $pdf = PDF::FromImage->new;

    $pdf->load_images(
        'page1.png',
        'page2.png',
        :
    );

    $pdf->write_file('output.pdf');

DESCRIPTION

Top

This module create simple pdf image slide from multiple images.

METHODS

Top

load_image($filename)

Load a image file.

Supported format are jpeg, tiff, pnm, png, and gif.

load_images(@filenames)

Load multiple images.

write_file($filename)

Generate pdf from loaded images, and write it to file.

AUTHOR

Top

Daisuke Murase <typester@cpan.org>

COPYRIGHT

Top


PDF-FromImage documentation Contained in the PDF-FromImage distribution.
package PDF::FromImage;
use 5.008001;
use Moose;

our $VERSION = '0.000003';

use PDF::API2;
use Imager;

use PDF::FromImage::Image;

has images => (
    is      => 'rw',
    isa     => 'ArrayRef',
    lazy    => 1,
    default => sub { [] },
);

sub load_image {
    my ($self, $image) = @_;

    my $imager = Imager->new;
    $imager->read( file => $image )
        or confess $imager->errstr;

    my $format    = $imager->tags( name => 'i_format' );
    my $supported = grep { $_ eq $format } qw/jpeg tiff pnm png gif/;

    confess qq{This module doen't support "$format"} unless $supported;

    my $image_object = PDF::FromImage::Image->new(
        src    => $image,
        format => $format,
        width  => $imager->getwidth,
        height => $imager->getheight,
    );

    push @{ $self->images }, $image_object;
}

sub load_images {
    my $self = shift;
    $self->load_image($_) for @_;
}

sub write_file {
    my ($self, $filename) = @_;
    confess 'no image is loaded' unless @{ $self->images };

    my $pdf = PDF::API2->new;

    for my $image (@{ $self->images }) {
        my $page = $pdf->page;
        $page->mediabox( $image->width, $image->height );

        my $loader = 'image_' . $image->format;
        my $img = $pdf->$loader($image->src);

        my $gfx = $page->gfx;
        $gfx->image( $img, 0, 0 );
    }

    $pdf->saveas($filename);
    $pdf->end;
}

1;