File: pdf.t

package info (click to toggle)
libpdf-api2-perl 2.047-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,228 kB
  • sloc: perl: 42,227; makefile: 11
file content (138 lines) | stat: -rw-r--r-- 3,802 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use Test::More tests => 16;

use warnings;
use strict;

use File::Temp qw(tempfile);
use PDF::API2;

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

$pdf->info(Producer => 'PDF::API2 Test Suite');
my %info = $pdf->info();
is($info{'Producer'}, 'PDF::API2 Test Suite', 'Check info string');

my $gfx = $pdf->page->gfx();
$gfx->fillcolor('blue');

my $new = PDF::API2->from_string($pdf->to_string());
%info = $new->info();
is($info{'Producer'}, 'PDF::API2 Test Suite', 'Check info string after save and reload');

##
## import_page
##

$pdf = $new;
$new = PDF::API2->new();
my $form = $new->importPageIntoForm($pdf, 1);
delete $form->{'Filter'};
my $string = $new->to_string();
like($string, qr/0 0 1 rg/,
     q{Page imported by import_page contains content from original});

# Add a second page with a different page size

$new = PDF::API2->from_string($string);
my $page = $pdf->page();
my $font = $pdf->corefont('Helvetica');
$page->mediabox(0, 0, 72, 144);
my $text = $page->text();
$text->font($font, 12);
$text->text('This is a test');
$pdf = PDF::API2->from_string($pdf->to_string());
$form = $new->importPageIntoForm($pdf, 2);
delete $form->{'Filter'};

is(($form->bbox())[2], 72,
   q{Form bounding box is set from imported page});

$string = $new->to_string();

like($string, qr/\(This is a test\)/,
     q{Second imported page contains text});


# Page Numbering

$pdf = PDF::API2->new();
$pdf->pageLabel(0, { -style => 'Roman' });

like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /R >> \] >>},
     q{Page Numbering: Upper-case Roman Numerals});

$pdf = PDF::API2->new();
$pdf->pageLabel(0, { -style => 'roman' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /r >> \] >>},
     q{Page Numbering: Upper-case Roman Numerals});

$pdf = PDF::API2->new();
$pdf->pageLabel(0, { -style => 'Alpha' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /A >> \] >>},
     q{Page Numbering: Upper-case Alphabet Characters});

$pdf = PDF::API2->new();
$pdf->pageLabel(0, { -style => 'alpha' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /a >> \] >>},
     q{Page Numbering: Lower-case Alphabet Characters});

$pdf = PDF::API2->new();
$pdf->pageLabel(0, { -style => 'decimal' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /D >> \] >>},
     q{Page Numbering: Decimal Characters});

$pdf = PDF::API2->new();
$pdf->pageLabel(0, { -start => 11 });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /D /St 11 >> \] >>},
     q{Page Numbering: Decimal Characters (implicit), starting at 11});

$pdf = PDF::API2->new();
$pdf->pageLabel(0, { -prefix => 'Test' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /P \(Test\) /S /D >> \] >>},
     q{Page Numbering: Decimal Characters (implicit), with prefix});


##
## to_string
##

$pdf = PDF::API2->new(-compress => 0);
$gfx = $pdf->page->gfx();
$gfx->fillcolor('blue');

$string = $pdf->to_string();
like($string, qr/0 0 1 rg/,
     q{To_String of newly-created PDF contains expected content});

my ($fh, $filename) = tempfile();
print $fh $string;
close $fh;

$pdf = PDF::API2->open($filename);
$string = $pdf->to_string();
like($string, qr/0 0 1 rg/,
     q{To_String of newly-opened PDF contains expected content});

##
## save with same filename
##

$pdf = PDF::API2->new(-compress => 0);
$gfx = $pdf->page->gfx();
$gfx->fillcolor('blue');

($fh, $filename) = tempfile();
print $fh $pdf->to_string();
close $fh;

$pdf = PDF::API2->open($filename, -compress => 0);
$gfx = $pdf->page->gfx();
$gfx->fillcolor('red');
$pdf->save($filename);

$pdf = PDF::API2->open($filename, -compress => 0);
$string = $pdf->to_string();
like($string, qr/0 0 1 rg/,
     q{save($opened_filename) contains original content});
like($string, qr/1 0 0 rg/,
     q{save($opened_filename) contains new content});