line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PDFLib::PPS; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
24630
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
4
|
use base 'PDFLib'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
486
|
|
5
|
1
|
|
|
1
|
|
355
|
use pdflib_pl 7.0; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use PDFLib::ZapfChars; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use constant { ERROR => -1 }; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
if ($ARGV[0]) { |
13
|
|
|
|
|
|
|
my $pdf = PDFLib::PPS->new(BlockContainer => $ARGV[0]); |
14
|
|
|
|
|
|
|
$pdf->print_block_info; |
15
|
|
|
|
|
|
|
exit; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
|
|
|
|
|
|
my $class = shift; |
20
|
|
|
|
|
|
|
my $pdf = $class->SUPER::new(@_); |
21
|
|
|
|
|
|
|
return $pdf; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub flowhandle { |
25
|
|
|
|
|
|
|
my ($pdf, $field_name, $handle) = @_; |
26
|
|
|
|
|
|
|
if (defined $handle) { |
27
|
|
|
|
|
|
|
$pdf->{FlowHandles}{$field_name} = $handle; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
return defined $pdf->{FlowHandles}{$field_name} |
30
|
|
|
|
|
|
|
? $pdf->{FlowHandles}{$field_name} : -1; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub zapfchar { |
35
|
|
|
|
|
|
|
my ($pdf, $name) = @_; |
36
|
|
|
|
|
|
|
return sprintf('%c', $PDFLib::ZapfChars{$name}); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub search_path { |
40
|
|
|
|
|
|
|
my ($pdf, $search_path) = @_; |
41
|
|
|
|
|
|
|
if (defined $search_path) { |
42
|
|
|
|
|
|
|
$pdf->{SearchPath} = $search_path; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
$pdf->{SearchPath}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub block_data { |
48
|
|
|
|
|
|
|
my $pdf = shift; |
49
|
|
|
|
|
|
|
if ($_[0]) { |
50
|
|
|
|
|
|
|
$pdf->{BlockData} = ref $_[0] ? $_[0] : \@_; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
return $pdf->{BlockData}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub block_datum { |
56
|
|
|
|
|
|
|
my ($pdf, $block_name, $value) = @_; |
57
|
|
|
|
|
|
|
if (defined $value) { |
58
|
|
|
|
|
|
|
$pdf->{BlockData}->{$block_name} = $value; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
return $pdf->{BlockData}->{$block_name}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub block_container { # this is the "template" filename |
64
|
|
|
|
|
|
|
my ($pdf, $block_container) = @_; |
65
|
|
|
|
|
|
|
if (defined $block_container) { |
66
|
|
|
|
|
|
|
$pdf->{BlockContainer} = $block_container; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
$pdf->{BlockContainer}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub container { # i.e., the block container object |
72
|
|
|
|
|
|
|
my ($pdf, $container) = @_; |
73
|
|
|
|
|
|
|
if (defined $container) { |
74
|
|
|
|
|
|
|
$pdf->{container} = $container; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
return $pdf->{container}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub current_page { |
80
|
|
|
|
|
|
|
my ($pdf, $page) = @_; |
81
|
|
|
|
|
|
|
if (defined $page) { |
82
|
|
|
|
|
|
|
$pdf->{page} = $page; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
return $pdf->{page}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub parse_datum { |
88
|
|
|
|
|
|
|
my ($pdf, $datum) = @_; |
89
|
|
|
|
|
|
|
return $datum unless ref $datum eq 'HASH'; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $text = $datum->{text}; |
92
|
|
|
|
|
|
|
my $override_encoding = exists $datum->{encoding} ? "Yes" : undef; |
93
|
|
|
|
|
|
|
my $options = join " " => |
94
|
|
|
|
|
|
|
map { sprintf "%s=%s", $_, $datum->{$_} } grep { not /text/ } keys %$datum; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
return ($text, $options, $override_encoding); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub fill_in { |
100
|
|
|
|
|
|
|
my ($pdf, $verbose) = @_; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $container = $pdf->open_pdi_document; |
103
|
|
|
|
|
|
|
defined $container ? $pdf->container($container) : return undef; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
for my $page_no (1 .. $pdf->number_of_pages) { |
106
|
|
|
|
|
|
|
print STDERR "[PPS] processing page number $page_no\n" if $verbose; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# open the template page. |
109
|
|
|
|
|
|
|
my $page = $pdf->open_pdi_page($page_no); |
110
|
|
|
|
|
|
|
defined $page ? $pdf->current_page($page) : return undef; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# create a new document page. |
113
|
|
|
|
|
|
|
$pdf->begin_page_ext; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
for my $block_name (sort $pdf->block_names) { |
116
|
|
|
|
|
|
|
my $encoding = $pdf->encoding_for_block($block_name); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
my ($text, $font_spec, $override_encoding) = |
119
|
|
|
|
|
|
|
$pdf->parse_datum($pdf->block_datum($block_name)); |
120
|
|
|
|
|
|
|
$encoding = $override_encoding ? $font_spec : "$encoding $font_spec"; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
if ($pdf->is_textflow($block_name)) { |
123
|
|
|
|
|
|
|
$encoding .= sprintf " textflowhandle=%s textflow=true $font_spec ", |
124
|
|
|
|
|
|
|
$pdf->flowhandle($block_name); |
125
|
|
|
|
|
|
|
print STDERR "$block_name encoding: ($font_spec) $encoding\n" |
126
|
|
|
|
|
|
|
if $verbose; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $fill = |
130
|
|
|
|
|
|
|
PDF_fill_textblock($pdf->_pdf, $pdf->current_page, |
131
|
|
|
|
|
|
|
$block_name, $text, "$encoding"); |
132
|
|
|
|
|
|
|
if ($fill == ERROR) { return undef } |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
if ($pdf->is_textflow($block_name)) { |
135
|
|
|
|
|
|
|
$pdf->flowhandle($block_name, $fill); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
print STDERR |
139
|
|
|
|
|
|
|
"[page $page_no]\tfilled in $block_name with $text ($encoding)\n" |
140
|
|
|
|
|
|
|
if $verbose; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$pdf->end_page_ext; |
144
|
|
|
|
|
|
|
$pdf->close_pdi_page; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$pdf->close_pdi_document; |
148
|
|
|
|
|
|
|
return "OK"; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub block_info { |
152
|
|
|
|
|
|
|
my $pdf = shift; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $container = $pdf->open_pdi_document; |
155
|
|
|
|
|
|
|
defined $container ? $pdf->container($container) : return undef; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my %info = (); |
158
|
|
|
|
|
|
|
for my $page_no (1 .. $pdf->number_of_pages) { |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# open the template page. |
161
|
|
|
|
|
|
|
my $page = $pdf->open_pdi_page($page_no); |
162
|
|
|
|
|
|
|
defined $page ? $pdf->current_page($page) : return undef; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
for my $block_name ($pdf->block_names) { |
165
|
|
|
|
|
|
|
my $font = $pdf->font_for_block($block_name); |
166
|
|
|
|
|
|
|
push @{ $info{$page_no} } => [$block_name, $font]; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
$pdf->close_pdi_page; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
return wantarray ? %info : \%info; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub print_block_info { |
175
|
|
|
|
|
|
|
my $pdf = shift; |
176
|
|
|
|
|
|
|
my %info = $pdf->block_info; |
177
|
|
|
|
|
|
|
for my $page (sort keys %info) { |
178
|
|
|
|
|
|
|
print "Page $page ", '-' x 70, "\n"; |
179
|
|
|
|
|
|
|
for my $block (@{ $info{$page} }) { |
180
|
|
|
|
|
|
|
my ($name, $font) = @$block; |
181
|
|
|
|
|
|
|
my $spacer = " " x (40 - length $name); |
182
|
|
|
|
|
|
|
print "\t$name $spacer $font\n"; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub end_page_ext { |
188
|
|
|
|
|
|
|
my $pdf = shift; |
189
|
|
|
|
|
|
|
PDF_end_page_ext($pdf->_pdf, ""); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub begin_page_ext { |
193
|
|
|
|
|
|
|
my $pdf = shift; |
194
|
|
|
|
|
|
|
PDF_begin_page_ext($pdf->_pdf, 20, 20, ""); |
195
|
|
|
|
|
|
|
PDF_fit_pdi_page($pdf->_pdf, $pdf->current_page, 0, 0, "adjustpage"); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub encoding_for_block { |
199
|
|
|
|
|
|
|
my ($pdf, $block_name) = @_; |
200
|
|
|
|
|
|
|
my $font = $pdf->font_for_block($block_name); |
201
|
|
|
|
|
|
|
if ($font eq 'ZapfDingbats') { |
202
|
|
|
|
|
|
|
return "encoding=builtin"; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
else { |
205
|
|
|
|
|
|
|
return "encoding=host"; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
sub get_pdi_parameter { |
210
|
|
|
|
|
|
|
my ($pdf, $path) = @_; |
211
|
|
|
|
|
|
|
return PDF_get_pdi_parameter($pdf->_pdf, $path, |
212
|
|
|
|
|
|
|
$pdf->container, $pdf->current_page, 0); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub is_textflow { |
216
|
|
|
|
|
|
|
my ($pdf, $block_name) = @_; |
217
|
|
|
|
|
|
|
return lc $pdf->field_type($block_name) eq 'textflow' ? 1 : undef; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub field_type { |
221
|
|
|
|
|
|
|
my ($pdf, $block_name) = @_; |
222
|
|
|
|
|
|
|
return |
223
|
|
|
|
|
|
|
$pdf->get_pdi_parameter("vdp/Blocks/$block_name/Custom/PDFlib:field:type"); |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
sub font_for_block { |
227
|
|
|
|
|
|
|
my ($pdf, $block_name) = @_; |
228
|
|
|
|
|
|
|
return $pdf->get_pdi_parameter("vdp/Blocks/$block_name/fontname"); |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
sub block_names { |
232
|
|
|
|
|
|
|
my $pdf = shift; |
233
|
|
|
|
|
|
|
my $count = $pdf->block_count; |
234
|
|
|
|
|
|
|
return map { $pdf->get_pdi_parameter("vdp/Blocks[$_]/Name") } |
235
|
|
|
|
|
|
|
(0 .. ($pdf->block_count - 1)); |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub block_count { |
241
|
|
|
|
|
|
|
my $pdf = shift; |
242
|
|
|
|
|
|
|
return PDF_get_pdi_value($pdf->_pdf, 'vdp/blockcount', |
243
|
|
|
|
|
|
|
$pdf->container, $pdf->current_page, 0); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub number_of_pages { |
247
|
|
|
|
|
|
|
my $pdf = shift; |
248
|
|
|
|
|
|
|
return PDF_get_pdi_value($pdf->_pdf, '/Root/Pages/Count', |
249
|
|
|
|
|
|
|
$pdf->container, -1, 0); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
sub open_pdi_document { |
253
|
|
|
|
|
|
|
my ($pdf) = @_; |
254
|
|
|
|
|
|
|
$pdf->set_parameter(SearchPath => $pdf->search_path); |
255
|
|
|
|
|
|
|
$pdf->set_parameter(pdiwarning => 'true'); |
256
|
|
|
|
|
|
|
my $container = PDF_open_pdi_document($pdf->_pdf, $pdf->block_container, ""); |
257
|
|
|
|
|
|
|
if ($container == ERROR) { |
258
|
|
|
|
|
|
|
warn ("Error opening PDI document: ", PDF_get_errmsg($pdf->_pdf)); |
259
|
|
|
|
|
|
|
return undef; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
return $container; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
sub close_pdi_document { |
265
|
|
|
|
|
|
|
my $pdf = shift; |
266
|
|
|
|
|
|
|
PDF_close_pdi($pdf->_pdf, $pdf->container); |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
sub open_pdi_page { |
270
|
|
|
|
|
|
|
my ($pdf, $page_no) = @_; |
271
|
|
|
|
|
|
|
my $page = PDF_open_pdi_page($pdf->_pdf, $pdf->container, $page_no, ""); |
272
|
|
|
|
|
|
|
if ($page == ERROR) { |
273
|
|
|
|
|
|
|
warn ("Error opening PDI page: ", PDF_get_errmsg($pdf->_pdf)); |
274
|
|
|
|
|
|
|
return undef; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
return $page; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
sub close_pdi_page { |
280
|
|
|
|
|
|
|
my $pdf = shift; |
281
|
|
|
|
|
|
|
PDF_close_pdi_page($pdf->_pdf, $pdf->current_page); |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
1; |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
__END__ |