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 139 140 141 142 143 144 145 146 147
|
#!/usr/bin/perl
use warnings;
use strict;
use Test::More tests => 16;
use File::Temp qw(tempfile);
use PDF::Builder;
my $pdf = PDF::Builder->new();
$pdf->info(Producer => 'PDF::Builder Test Suite');
my %info = $pdf->info();
is($info{'Producer'}, 'PDF::Builder Test Suite', 'Check info string');
my $gfx = $pdf->page()->gfx();
$gfx->fillcolor('blue');
$gfx->_Gpending(); # flush buffered output
my $new = PDF::Builder->from_string($pdf->to_string());
%info = $new->info();
is($info{'Producer'}, 'PDF::Builder Test Suite', 'Check info string after save and reload');
##
## import_page
##
$pdf = $new;
$new = PDF::Builder->new();
my $form = $new->importPageIntoForm($pdf, 1);
#$form->{'-docompress'} = 0; # not in API2 tests
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::Builder->from_string($string, 'compress' => 'none');
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::Builder->from_string($pdf->to_string());
$form = $new->importPageIntoForm($pdf, 2);
#$form->{'-docompress'} = 0; # not in API2 tests
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::Builder->new('compress' => 'none');
$pdf->pageLabel(0, { 'style' => 'Roman' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /R /St 1 >> \] >>},
q{Page Numbering: Upper-case Roman Numerals});
$pdf = PDF::Builder->new('compress' => 'none');
$pdf->pageLabel(0, { 'style' => 'roman' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /r /St 1 >> \] >>},
q{Page Numbering: Upper-case Roman Numerals});
$pdf = PDF::Builder->new('compress' => 'none');
$pdf->pageLabel(0, { 'style' => 'Alpha' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /A /St 1 >> \] >>},
q{Page Numbering: Upper-case Alphabet Characters});
$pdf = PDF::Builder->new('compress' => 'none');
$pdf->pageLabel(0, { 'style' => 'alpha' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /a /St 1 >> \] >>},
q{Page Numbering: Lower-case Alphabet Characters});
$pdf = PDF::Builder->new('compress' => 'none');
$pdf->pageLabel(0, { 'style' => 'decimal' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /S /D /St 1 >> \] >>},
q{Page Numbering: Decimal Characters});
$pdf = PDF::Builder->new('compress' => 'none');
$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::Builder->new('compress' => 'none');
$pdf->pageLabel(0, { 'prefix' => 'Test' });
like($pdf->to_string(), qr{/PageLabels << /Nums \[ 0 << /P \(Test\) /S /D /St 1 >> \] >>},
q{Page Numbering: Decimal Characters (implicit), with prefix});
##
## to_string
##
$pdf = PDF::Builder->new('compress' => 'none');
$gfx = $pdf->page()->gfx();
$gfx->fillcolor('blue');
$gfx->_Gpending(); # flush buffered output
$string = $pdf->to_string();
like($string, qr/0 0 1 rg/,
q{Stringify of newly-created PDF contains expected content});
my ($fh, $filename) = tempfile();
print $fh $string;
close $fh;
$pdf = PDF::Builder->open($filename);
$string = $pdf->to_string();
like($string, qr/0 0 1 rg/,
q{Stringify of newly-opened PDF contains expected content});
##
## saveas with same filename
## (in response to bug 134993, introduced by 113516, not yet in PDF::Builder)
##
$pdf = PDF::Builder->new('compress' => 'none');
$gfx = $pdf->page()->gfx();
$gfx->fillcolor('blue');
$gfx->_Gpending(); # flush buffered output
($fh, $filename) = tempfile();
print $fh $pdf->to_string();
close $fh;
$pdf = PDF::Builder->open($filename, 'compress' => 'none');
$gfx = $pdf->page()->gfx();
$gfx->fillcolor('red');
$gfx->_Gpending(); # flush buffered output
$pdf->saveas($filename);
$pdf = PDF::Builder->open($filename, 'compress' => 'none');
$gfx->_Gpending(); # flush buffered output
$string = $pdf->to_string();
like($string, qr/0 0 1 rg/,
q{saveas($opened_filename) contains original content});
like($string, qr/1 0 0 rg/,
q{saveas($opened_filename) contains new content});
1;
|