Skip to content

Commit cb10d8f

Browse files
authored
Merge pull request #14 from szabgab/test-with-image
Add test that adds a logo
2 parents 9c18edf + 44e6491 commit cb10d8f

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

sample/004_image.pdf

62.2 KB
Binary file not shown.

t/004_image.t

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use strict;
2+
use warnings;
3+
4+
use Test::More;
5+
6+
use CtrlO::PDF;
7+
8+
use lib '.';
9+
use t::lib::Tools qw(compare_pdf);
10+
11+
subtest image => sub {
12+
my $pdf = CtrlO::PDF->new(
13+
logo => "sample/logo.png",
14+
footer => "My PDF document footer",
15+
);
16+
17+
$pdf->add_page;
18+
19+
# Add headings
20+
$pdf->heading('This is the main heading');
21+
$pdf->heading('This is a sub-heading', size => 12);
22+
23+
# Add paragraph text
24+
$pdf->text("Foobar");
25+
26+
compare_pdf($pdf, 'sample/004_image.pdf');
27+
};
28+
29+
done_testing();
30+
31+

t/lib/Tools.pm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package t::lib::Tools;
2+
use strict;
3+
use warnings;
4+
5+
use File::Temp qw(tempdir);
6+
use File::Spec::Functions qw(catfile);
7+
use File::Compare qw(compare);
8+
use Test::Builder::Module;
9+
use Exporter qw(import);
10+
11+
our @EXPORT_OK = qw(compare_pdf);
12+
13+
sub compare_pdf {
14+
my ($pdf, $expected_file) = @_;
15+
16+
my $Test = Test::Builder::Module->builder;
17+
18+
my $content = $pdf->content;
19+
$Test->ok($content, "Some PDF content produced");
20+
21+
# For debugging one can set CLEANUP to 0 and enable the diag to see
22+
# the name of the temporary folder.
23+
my $dir = tempdir( CLEANUP => 1 );
24+
# $Test->diag($dir);
25+
26+
my $file = catfile($dir, 'out.pdf');
27+
28+
open my $out, '>', $file;
29+
binmode $out;
30+
print $out $content;
31+
close $out;
32+
33+
$Test->is_num(compare($file, $expected_file), 0, 'File is as expected');
34+
}
35+
36+
1;

0 commit comments

Comments
 (0)