File Coverage

blib/lib/PDF/FromImage.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package PDF::FromImage;
2 1     1   20 use 5.008001;
  1         3  
  1         38  
3 1     1   424 use Moose;
  0            
  0            
4              
5             our $VERSION = '0.000003';
6              
7             use PDF::API2;
8             use Imager;
9              
10             use PDF::FromImage::Image;
11              
12             has images => (
13             is => 'rw',
14             isa => 'ArrayRef',
15             lazy => 1,
16             default => sub { [] },
17             );
18              
19             =head1 NAME
20              
21             PDF::FromImage - Create PDF slide from images
22              
23             =head1 SYNOPSIS
24            
25             use PDF::FromImage;
26            
27             my $pdf = PDF::FromImage->new;
28            
29             $pdf->load_images(
30             'page1.png',
31             'page2.png',
32             :
33             );
34            
35             $pdf->write_file('output.pdf');
36              
37             =head1 DESCRIPTION
38              
39             This module create simple pdf image slide from multiple images.
40              
41             =head1 METHODS
42              
43             =head2 load_image($filename)
44              
45             Load a image file.
46              
47             Supported format are jpeg, tiff, pnm, png, and gif.
48              
49             =cut
50              
51             sub load_image {
52             my ($self, $image) = @_;
53              
54             my $imager = Imager->new;
55             $imager->read( file => $image )
56             or confess $imager->errstr;
57              
58             my $format = $imager->tags( name => 'i_format' );
59             my $supported = grep { $_ eq $format } qw/jpeg tiff pnm png gif/;
60              
61             confess qq{This module doen't support "$format"} unless $supported;
62              
63             my $image_object = PDF::FromImage::Image->new(
64             src => $image,
65             format => $format,
66             width => $imager->getwidth,
67             height => $imager->getheight,
68             );
69              
70             push @{ $self->images }, $image_object;
71             }
72              
73             =head2 load_images(@filenames)
74              
75             Load multiple images.
76              
77             =cut
78              
79             sub load_images {
80             my $self = shift;
81             $self->load_image($_) for @_;
82             }
83              
84             =head2 write_file($filename)
85              
86             Generate pdf from loaded images, and write it to file.
87              
88             =cut
89              
90             sub write_file {
91             my ($self, $filename) = @_;
92             confess 'no image is loaded' unless @{ $self->images };
93              
94             my $pdf = PDF::API2->new;
95              
96             for my $image (@{ $self->images }) {
97             my $page = $pdf->page;
98             $page->mediabox( $image->width, $image->height );
99              
100             my $loader = 'image_' . $image->format;
101             my $img = $pdf->$loader($image->src);
102              
103             my $gfx = $page->gfx;
104             $gfx->image( $img, 0, 0 );
105             }
106              
107             $pdf->saveas($filename);
108             $pdf->end;
109             }
110              
111             =head1 AUTHOR
112              
113             Daisuke Murase <typester@cpan.org>
114              
115             =head1 COPYRIGHT
116              
117             This program is free software; you can redistribute
118             it and/or modify it under the same terms as Perl itself.
119              
120             The full text of the license can be found in the
121             LICENSE file included with this module.
122              
123             =cut
124              
125             1;