aboutsummaryrefslogtreecommitdiffstats
path: root/qpdf/qtest/split-pages.test
blob: 455a4ed32b85fa60cfdc4213e1f8f6ab1c6bd655 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
use Digest::MD5;
use File::Copy;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('split-pages');

my $n_tests = 42;
my $n_compare_pdfs = 2;

# sp = split-pages
my @sp_cases = (
    [11, '%d at beginning', '', '%d_split-out.zdf'],
    [11, '%d at end', '--qdf', 'split-out.zdf_%d'],
    [11, '%d in middle', '--allow-weak-crypto --encrypt u o 128 --',
     'a-%d-split-out.zdf'],
    [11, 'pdf extension', '', 'split-out.Pdf'],
    [4, 'fallback', '--pages 11-pages.pdf 1-3 minimal.pdf --', 'split-out'],
    [1, 'broken data', '--pages broken-lzw.pdf --', 'split-out.pdf',
     {$td->FILE => "broken-lzw.out", $td->EXIT_STATUS => 3}],
    );
for (@sp_cases)
{
    $n_tests += 1 + $_->[0];
}

$td->runtest("split page group > 1",
             {$td->COMMAND => "qpdf --static-id --split-pages=5 11-pages.pdf" .
                  " --verbose split-out-group.pdf"},
             {$td->FILE => "split-pages-group.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
foreach my $f ('01-05', '06-10', '11-11')
{
    $td->runtest("check out group $f",
                 {$td->FILE => "split-out-group-$f.pdf"},
                 {$td->FILE => "split-exp-group-$f.pdf"});
}

$td->runtest("no split-pages to stdout",
             {$td->COMMAND => "qpdf --split-pages 11-pages.pdf -"},
             {$td->FILE => "split-pages-stdout.out", $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

$td->runtest("split page with shared resources",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages=4".
                  " shared-images.pdf split-out-shared.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(01-04 05-08 09-10))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-$i.pdf"},
                 {$td->FILE => "shared-split-$i.pdf"});
}

$td->runtest("split page with labels",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages=6".
                  " 11-pages-with-labels.pdf split-out-labels.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(01-06 07-11))
{
    $td->runtest("check output ($i)",
                 {$td->COMMAND => "qpdf-test-compare split-out-labels-$i.pdf labels-split-$i.pdf"},
                 {$td->FILE => "labels-split-$i.pdf", $td->EXIT_STATUS => 0});
}

# See comments in TODO about these expected failures. Search for
# "split page with outlines".
$td->runtest("split page with outlines",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages=10".
                  " outlines-with-actions.pdf split-out-outlines.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(01-10 11-20 21-30))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-outlines-$i.pdf"},
                 {$td->FILE => "outlines-split-$i.pdf"},
                 $td->EXPECT_FAILURE)
}

foreach my $d (@sp_cases)
{
    my ($n, $description, $xargs, $out, $exp) = @$d;
    if (! defined $exp)
    {
        $exp = {$td->STRING => "", $td->EXIT_STATUS => 0};
    }
    $td->runtest("split pages " . $description,
                 {$td->COMMAND =>
                      "qpdf --static-id --split-pages 11-pages.pdf" .
                      " $xargs $out"},
                 $exp,
                 $td->NORMALIZE_NEWLINES);
    my $pattern = $out;
    my $nlen = length($n);
    if ($pattern =~ m/\%d/)
    {
        $pattern =~ s/\%d/\%0${nlen}d/;
    }
    elsif ($pattern =~ m/\.pdf$/i)
    {
        $pattern =~ s/(\.pdf$)/-%0${nlen}d$1/i;
    }
    else
    {
        $pattern .= "-%0${nlen}d";
    }
    for (my $i = 1; $i <= $n; ++$i)
    {
        my $actual = sprintf($pattern, $i);
        my $expected = $actual;
        $expected =~ s/split-out/split-exp/;
        $td->runtest("check output page $i ($description)",
                     {$td->COMMAND => "qpdf-test-compare $actual $expected u"},
                     {$td->FILE => $expected, $td->EXIT_STATUS => 0});
    }
}

$td->runtest("split shared font, xobject",
             {$td->COMMAND =>
                  "qpdf --static-id --qdf --no-original-object-ids" .
                  " --split-pages shared-font-xobject.pdf" .
                  " split-out-shared-font-xobject.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(1 2 3 4))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-font-xobject-$i.pdf"},
                 {$td->FILE => "shared-font-xobject-split-$i.pdf"});
}

$td->runtest("unreferenced resources with bad token",
             {$td->COMMAND =>
                  "qpdf --qdf --static-id --split-pages=2" .
                  " --remove-unreferenced-resources=yes" .
                  " split-tokens.pdf split-out-bad-token.pdf"},
             {$td->FILE => "split-tokens-split.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "split-out-bad-token-1-2.pdf"},
             {$td->FILE => "split-tokens-split-1-2.pdf"});
$td->runtest("--no-warn with proxied warnings during split",
             {$td->COMMAND =>
                  "qpdf --qdf --static-id --split-pages=2" .
                  " --no-warn --remove-unreferenced-resources=yes" .
                  " split-tokens.pdf split-out-bad-token.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);

$td->runtest("shared images in form xobject",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages".
                  " shared-form-images.pdf split-out-shared-form.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(1 2 3 4 5 6))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-form-$i.pdf"},
                 {$td->FILE => "shared-form-split-$i.pdf"});
}
$td->runtest("merge for compare",
             {$td->COMMAND => "qpdf --static-id --empty --pages" .
                  " split-out-shared-form*.pdf -- a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "shared-form-images-merged.pdf"});
compare_pdfs($td, "shared-form-images.pdf", "a.pdf");

$td->runtest("shared form xobject subkey",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages".
                  " shared-form-images-xobject.pdf" .
                  " split-out-shared-form-xobject.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(1 2))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-form-xobject-$i.pdf"},
                 {$td->FILE => "shared-form-xobject-split-$i.pdf"});
}

my @fo_resources = (['form-xobjects-no-resources', 1],
                    ['form-xobjects-some-resources1', 0],
                    ['form-xobjects-some-resources2', 0]);
foreach my $d (@fo_resources)
{
    my ($f, $compare) = @$d;
    $td->runtest("split $f",
                 {$td->COMMAND =>
                      "qpdf --empty --static-id --pages $f.pdf 1 --" .
                      " --remove-unreferenced-resources=yes a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("check output ($f)",
                 {$td->COMMAND => "qpdf-test-compare a.pdf $f-out.pdf"},
                 {$td->FILE => "$f-out.pdf", $td->EXIT_STATUS => 0});
    if ($compare)
    {
        compare_pdfs($td, "$f.pdf", "a.pdf");
    }
}

cleanup();
$td->report(calc_ntests($n_tests, $n_compare_pdfs));