Builder/Docs.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 2 / 3 Podpod n/a totaltotal 91.6% 11 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
_docs
1.0
CRAP 1.0
« Builder/Content/Column_docs.pm ↑ Summary Builder/Resource/Colors.pm »
1 package PDF::Builder::Docs;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '3.028'; # VERSION
7 1 our $LAST_UPDATE = '3.028'; # manually update whenever code is changed
8  
9 # originally part of Builder.pm, it was split out due to its length
10 #
11 # WARNING: be sure to keep in synch with changes to POD elsewhere, especially
12 # for column() markup! (also list in #195 and in Content::Text)
13 #
14 # do not attempt to use Unicode entities, such as E<euro> -- the POD to
15 # HTML converter will barf on them!
16  
17 =head1 NAME
18  
19 PDF::Builder::Docs - Additional documentation for Builder module
20  
21 =head1 SOME SPECIAL NOTES
22  
23 =head2 Software Development Kit
24  
25 There are four levels of involvement with PDF::Builder. Depending on what you
26 want to do, different kinds of installs are recommended.
27  
28 =over
29  
30 =item 1.
31  
32 Simply installing PDF::Builder as a prerequisite for running some other
33 package. All you need to do is install the CPAN package for PDF::Builder, and
34 it will load the .pm files into your Perl library. If the other package prereqs
35 PDF::Builder, its installer may download and install PDF::Builder automatically.
36  
37 Note that PDF::Builder has a number of I<optional> prerequisites. It will
38 happily install and run without them, but advanced functionality (possibly
39 including that needed by another package) will B<not> be available unless you
40 manually install the desired packages. See L</Optional Libraries> or the
41 C<README> file for the list of optional packages.
42  
43 =item 2.
44  
45 You want to write a Perl program that uses PDF::Builder functions. In
46 addition to installing PDF::Builder from CPAN, you will want documentation on
47 it. Obtain a copy of the product from GitHub
48 (https://github.com/PhilterPaper/Perl-PDF-Builder) or as a gzipped tar file from CPAN.
49 This includes a utility to
50 build (from POD) a library of HTML documents, as well as examples (examples/
51 directory) and contributed sample programs (contrib/ directory).
52  
53 =item 3.
54  
55 You want to modify PDF::Builder files. In addition to the CPAN and GitHub
56 distributions, you I<may> choose to keep a local Git repository for tracking
57 your changes. Depending on whether or not your PDF::Builder copy is being used
58 for production purposes, you may want to do your editing and testing in the Perl
59 library installation (I<live>) or in a different place. The "t" tests (t/
60 directory) and examples provide good regression tests to ensure that you haven't
61 broken anything. If you do your editing on the live code, don't forget when done
62 to copy the changes back into the master version you keep!
63  
64 =item 4.
65  
66 You want to contribute to the development of PDF::Builder. You will need a
67 local Git repository (and a GitHub account), so that when you've got it all
68 done, you can issue a "Pull Request" to bring it to our attention. We can't
69 guarantee that your work will be incorporated into the project, but at least we
70 will look at it. From time to time, a new CPAN version will be issued.
71  
72 =back
73  
74 If you want to make substantial changes for public use, and can't come to a
75 meeting of minds with us, you can even start your own GitHub project and
76 register a new CPAN project (that's what we did, I<forking> PDF::API2). Please
77 don't just assume that we don't want your changes -- at least propose what you
78 want to do in writing, so we can consider it. We're always looking for people to
79 help out and expand PDF::Builder.
80  
81 =head2 Optional Libraries
82  
83 PDF::Builder can make use of some optional libraries, which are not I<required>
84 for a successful installation. If you want improved speed and capabilities for
85 certain functions, you may want to install and use these libraries.
86 PDF::Builder's C<README> file lists the minimum versions needed, and
87 C<INFO/Prereq_fixes> lists known updates needed for some.
88  
89 =over
90  
91 =item *
92  
93 Perl::Critic
94  
95 Needed if running C<tools/1_pc.pl> to check the library content.
96  
97 =item *
98  
99 Graphics::TIFF
100  
101 PDF::Builder inherited a rather slow, buggy, and limited
102 TIFF image library from PDF::API2. If Graphics::TIFF (available on CPAN, uses
103 libtiff.a) is installed, PDF::Builder will use that instead, unless you specify
104 that it is to use the old, pure Perl library. The only time you might want to
105 consider this is when you need to pass an open filehandle to C<image_tiff>
106 instead of a file name. See resolved bug reports RT 84665 and RT 118047, as well
107 as C<image_tiff>, for more information.
108  
109 =item *
110  
111 Image::PNG::Libpng
112  
113 PDF::Builder inherited a rather slow and buggy pure
114 Perl PNG image library from PDF::API2. If Image::PNG::Libpng (available on
115 CPAN, uses libpng.a) is installed, PDF::Builder will use that instead, unless
116 you specify that it is to use the old, pure Perl library. Using the new library
117 will give you improved speed, the ability to use 16 bit samples, and the
118 ability to read interlaced PNG files. See resolved bug report RT 124349, as well
119 as C<image_png>, for more information.
120  
121 =item *
122  
123 HarfBuzz::Shaper
124  
125 This library enables PDF::Builder to handle complex
126 scripts (Arabic, Devanagari, etc.) as well as non-LTR writing systems. It is
127 also useful for Latin and other simple scripts, for ligatures and improved
128 kerning. HarfBuzz::Shaper is based on a set of HarfBuzz libraries, which it
129 will attempt to build if they are not found. See C<textHS> for more
130 information.
131  
132 =item *
133  
134 Text::Markdown
135  
136 This library is used if you want to format "Markdown"
137 style code in PDF::Builder, via the C<column()> method. It translates a certain
138 dialect of Markdown into HTML, which is then further processed.
139  
140 =item *
141  
142 HTML::TreeBuilder
143  
144 This library is used to format HTML input into a
145 data structure which PDF::Builder can interpret, via the C<column()> method.
146 Note that if Markdown input is used, it will also need HTML::TreeBuilder to
147 handle the HTML the Markdown is translated to.
148  
149 =item *
150  
151 Pod::Simple::XHTML
152  
153 This library is used if you wish to generate the HTML documentation from the
154 POD and PM source, using C<docs/buildDoc.pl>. Note that the full set of
155 documentation can also be found online at
156 https://www.catskilltech.com/FreeSW/product/PDF-Builder/title/PDF%3A%3ABuilder/freeSW_full
157 under the "Documentation" link. This online documentation is updated at
158 every CPAN release, but not necessarily when the GitHub repository is updated.
159  
160 =item *
161  
162 SVGPDF
163  
164 This library is used to convert SVG image files into PDF primitives, so SVG
165 images may be included in a PDF.
166  
167 =back
168  
169 Note that the installation process will B<not> attempt to install these
170 libraries automatically. If you don't wish to use one or more of them, you are
171 free to not install the optional librarie(s). If you may want to make use of
172 one or more, consider installing them I<before> installing PDF::Builder, so
173 that any t-tests and/or examples that make use of these libraries may be run
174 during installation and checkout of PDF::Builder. Remember, you can I<always>
175 install an optional library later, if you want to make use of it.
176  
177 =head2 Strings (Character Text)
178  
179 Perl, and hence PDF::Builder, use strings that support the full range of
180 Unicode characters. When importing strings into a Perl program, for example
181 by reading text from a file, you must be aware of what their character encoding
182 is. Single-byte encodings (default is 'latin1'), represented as bytes of value
183 0x00 through 0xFF (0..255), will produce different results if you do something
184 that depends on the encoding, such as sorting, searching, or comparing any
185 two non-ASCII characters. This also applies to any characters (text) hard
186 coded into the Perl program.
187  
188 You can always decode the text from external encoding (ASCII, UTF-8, Latin-3,
189 etc.) into the Perl (internal) UTF-8 multibyte encoding. This uses one to four
190 bytes to represent each character. See pragma C<utf8> and module C<Encode> for
191 details about decoding text. Note that only TrueType fonts (C<ttfont>) can
192 make direct use of UTF-8-encoded text. Other font types (core, T1, etc.) can
193 only use single-byte encoded text. If your text is ASCII, Latin-1, or CP-1252,
194 you I<can> just leave the Perl strings as the default single-byte encoding.
195  
196 Then, there is the matter of encoding the I<output> to match up with available
197 font character sets. You're not actually I<translating> the text on output, but
198 are telling the output system (and Reader) what encoding the output byte stream
199 represents, and what character glyphs they should generate.
200  
201 If you confine your text to plain ASCII (0x00 .. 0x7F byte values) or even
202 Latin-1 or CP-1252 (0x00 .. 0xFF byte values), you can
203 use default (non-UTF-8) Perl strings and use the default output encoding
204 (WinAnsiEncoding), which is more-or-less Windows CP-1252 (a superset
205 in turn, of ISO-8859-1 Latin-1). If your text uses any other characters, you
206 will need to be aware of what encoding your text strings are (in the Perl string
207 and for declaring output glyph generation).
208 See L</Core Fonts>, L</PS Fonts> and L</TrueType Fonts> in L</FONT METHODS>
209 for additional information.
210  
211 =head2 Supported Perl Versions
212  
213 PDF::Builder intends to support all major Perl versions that were released in
214 the past six years, plus one, in order to continue working for the life of
215 most long-term-stable (LTS) server distributions.
216 See the L<https://www.cpan.org/src/> table
217 B<First release in each branch of Perl> x.xxxx0 "Major" release dates.
218  
219 For example, a version of PDF::Builder released on 2018-06-05 would support
220 the last major version of Perl released I<on or after> 2012-06-05 (5.18), and
221 then one before that, which would be 5.16. Alternatively, the last major
222 version of Perl released I<before> 2012-06-05 is 5.16.
223  
224 The intent is to avoid expending unnecessary effort in supporting very old
225 (obsolete) versions of Perl.
226  
227 =head3 Anticipated Support Cutoff Dates
228  
229 B<Note> that these are I<not> hard and fast dates. In particular, we develop
230 on Strawberry Perl, which sometimes falls a little behind the official Perl
231 release!
232  
233 Also, due to user requests to allow I<new> releases of PDF::Builder to run on
234 earlier versions of Perl, we will try to stretch the lifetime of earlier Perl
235 versions. Eventually, some older Perl versions will have to be dropped if and
236 when we start making use of newer Perl operators and constructs. Nevertheless,
237 it is still I<not> a good idea to continue to use very old Perl versions that
238 are beyond their support cutoff dates -- they become vulnerable to security
239 attacks, and bugs do not get fixed.
240  
241 =over
242  
243 =item *
244  
245 5.28 current minimum supported version. Out of support, so use at your own risk.
246  
247 =item *
248  
249 5.30 future minimum supported version, until next PDF::Builder release after 20 June, 2026.
250  
251 =item *
252  
253 5.32 future minimum supported version, until next PDF::Builder release after 20 May, 2027.
254  
255 =item *
256  
257 5.34 not released under Strawberry Perl, expected to be skipped
258  
259 =item *
260  
261 5.36 future minimum supported version, until next PDF::Builder release after 28 May, 2028. This is currently our primary development version.
262  
263 =item *
264  
265 5.38 future minimum supported version, until next PDF::Builder release after 02 Jul, 2029.
266  
267 =item *
268  
269 5.40 future minimum supported version, until next PDF::Builder release after 09 June, 2030. This is currently the maximum tested version.
270  
271 =item *
272  
273 5.42 future minimum supported version, until next PDF::Builder release after 03 July, 2031.
274  
275 =back
276  
277 If you need to use this module on a server with an extremely out-of-date version
278 of Perl, consider using either plenv or Perlbrew to run a newer version of Perl
279 without needing admin privileges.
280  
281 On the other hand, any feature in PDF::Builder should continue to work
282 unchanged for the life of most long-term-stable (LTS) server distributions.
283 Their lifetime is usually about six (6) years. Note that this does B<not>
284 constitute a statement of warranty, but that we I<intend> to try to keep any
285 particular release of PDF::Builder working for a period of years. Of course,
286 it helps if you periodically update your Perl installation to something
287 released in the recent past.
288  
289 =head3 Some Internal Details
290  
291 Some of the following may be a bit scary or confusing to beginners, so don't
292 be afraid to skip over it until you're ready for it...
293  
294 Perl (and PDF::Builder) internally use strings which are either single-byte
295 (ISO-8859-1/Latin-1) or multibyte UTF-8 encoded (there is an internal flag
296 marking the string as UTF-8 or not).
297 If you work I<strictly> in ASCII or Latin-1 or CP-1252 (each a superset of the
298 previous), you should be OK in not doing anything special about your string
299 encoding. You can just use the default Perl single byte strings (internally
300 marked as I<not> UTF-8) and the default output encoding (WinAnsiEncoding).
301  
302 If you intend to use input from a variety of sources, you should consider
303 decoding (converting) your text to UTF-8, which will provide an internally
304 consistent representation (and your Perl code itself should be saved in UTF-8,
305 in case you want to use any hard coded non-ASCII characters). In any string,
306 non-ASCII characters (0x80 or higher) would be converted to the Perl UTF-8
307 internal representation, via C<$string = Encode::decode(MY_ENCODING, $input);>.
308 C<MY_ENCODING> would be a string like 'latin1', 'cp-1252', 'utf8', etc. Similar
309 capabilities are available for declaring a I<file> to be in a certain encoding.
310  
311 Be aware that if you use UTF-8 encoding for your text, that only TrueType font
312 output (C<ttfont>) can handle it directly. Corefont and Type1 output will
313 require that the text will have to be converted back into a single-byte encoding
314 (using C<Encode::encode>), which may need to be declared with C<encode> (for
315 C<corefont> or C<psfont>). If you have any characters I<not> found in the
316 selected single-byte I<encoding> (but I<are> found in the font itself), you
317 will need to use C<automap> to break up the font glyphs into 256 character
318 planes, map such characters to 0x00 .. 0xFF in the appropriate plane, and
319 switch between font planes as necessary.
320  
321 Core and Type1 fonts (output) use the byte values in the string (single-byte
322 encoding only!) and provide a byte-to-glyph mapping record for each plane.
323 TrueType outputs a group of four hexadecimal digits representing the "CId"
324 (character ID) of each character. The CId does not correspond to either the
325 single-byte or UTF-8 internal representations of the characters.
326  
327 The bottom line is that you need to know what the internal representation of
328 your text is, so that the output routines can tell the PDF reader about it
329 (via the PDF file). The text will not be translated upon output, but the PDF
330 reader needs to know what the encoding in use is, so it knows what glyph to
331 associate with each byte (or byte sequence).
332  
333 Note that some operating systems and Perl flavors are reputed to be strict
334 about encoding names. For example, B<latin1> (an alias) may be rejected as
335 invalid, while B<iso-8859-1> (a canonical value) will work.
336  
337 By the way, it is recommended that you be using I<at least> Perl 5.10 if you
338 are going to be using any non-ASCII characters. Perl 5.8 may be a little
339 unpredictable in handling such text.
340  
341 =head2 Rendering Order
342  
343 For better or worse, for compatibility purposes, PDF::Builder continues the
344 same rendering model as used by PDF::API2 (and possibly its predecessors). That
345 is, all graphics I<for one graphics object> are put into one record, and all
346 text output I<for one text object> goes into another
347 record. Which one is output first, is whichever is declared first. This can
348 lead to unexpected results, where items are rendered in (apparently) the
349 wrong order. That is, text and graphics items are not necessarily output
350 (rendered) in the same order as they were created in code. Two items in the
351 same object (e.g., C<$text>) I<will> be rendered in the same order as they were
352 coded, but items from different objects may not be rendered in the expected
353 order. The following example (source code and annotated PDF excerpts) will
354 hopefully illustrate the issue:
355  
356 use strict;
357 use warnings;
358 use PDF::Builder;
359  
360 # demonstrate text and graphics object order
361 #
362 my $fname = "objorder";
363  
364 my $paper_size = "Letter";
365  
366 # see the text and graphics stream contents
367 my $pdf = PDF::Builder->new(compress => 'none');
368 $pdf->mediabox($paper_size);
369 my $page = $pdf->page();
370 # adjust path for your operating system
371 my $fontTR = $pdf->ttfont('C:\\Windows\\Fonts\\timesbd.ttf');
372 # or 'C:/Windows/Fonts/timesbd.ttf' will also work on Windows
373  
374 For the first group, you might expect the "under" line to be output, then the
375 filled circle (disc) partly covering it, then the "over" line covering the
376 disc, and finally a filled rectangle (bar) over both lines. What actually
377 happened is that the C<$grfx> graphics object was declared first, so everything
378 in that object (the disc and bar) is output first, and the text object C<$text>
379 (both lines) comes afterwards. The result is that the text lines are on I<top>
380 of the graphics drawings.
381
382 # ----------------------------
383 # 1. text, orange ball over, text over, bar over
384  
385 my $grfx1 = $page->gfx();
386 my $text1 = $page->text();
387 $text1->font($fontTR, 20); # 20 pt Times Roman bold
388  
389 $text1->fillcolor('black');
390 $grfx1->strokecolor('blue');
391 $grfx1->fillcolor('orange');
392  
393 $text1->translate(50,700);
394 $text1->text_left("This text should be under everything.");
395  
396 $grfx1->circle(100,690, 30);
397 $grfx1->fillstroke();
398  
399 $text1->translate(50,670);
400 $text1->text_left("This text should be over the ball and under the bar.");
401  
402 $grfx1->rect(160,660, 20,70);
403 $grfx1->fillstroke();
404  
405 % ---------------- group 1: define graphics object first, then text
406 11 0 obj << /Length 690 >> stream % obj 11 is graphics for (1)
407 0 0 1 RG % stroke blue
408 1 0.647059 0 rg % fill orange
409 130 690 m ... c h B % draw and fill circle
410 160 660 20 70 re B % draw and fill bar
411 endstream endobj
412  
413 12 0 obj << /Length 438 >> stream % obj 12 is text for (1)
414 BT
415 /TiCBA 20 Tf % Times Roman Bold 20pt
416 0 0 0 rg % fill black
417 1 0 0 1 50 700 Tm % position text
418 <0037 ... 0011> Tj % "under" line
419 1 0 0 1 50 670 Tm % position text
420 <0037 ... 0011> Tj % "over" line
421 ET
422 endstream endobj
423  
424 The second group is the same as the first, with the only difference being
425 that the text object was declared first, and then the graphics object. The
426 result is that the two text lines are rendered first, and then the disc and
427 bar are drawn I<over> them.
428  
429 # ----------------------------
430 # 2. (1) again, with graphics and text order reversed
431  
432 my $text2 = $page->text();
433 my $grfx2 = $page->gfx();
434 $text2->font($fontTR, 20); # 20 pt Times Roman bold
435  
436 $text2->fillcolor('black');
437 $grfx2->strokecolor('blue');
438 $grfx2->fillcolor('orange');
439  
440 $text2->translate(50,600);
441 $text2->text_left("This text should be under everything.");
442  
443 $grfx2->circle(100,590, 30);
444 $grfx2->fillstroke();
445  
446 $text2->translate(50,570);
447 $text2->text_left("This text should be over the ball and under the bar.");
448  
449 $grfx2->rect(160,560, 20,70);
450 $grfx2->fillstroke();
451  
452 % ---------------- group 2: define text object first, then graphics
453 13 0 obj << /Length 438 >> stream % obj 13 is text for (2)
454 BT
455 /TiCBA 20 Tf % Times Roman Bold 20pt
456 0 0 0 rg % fill black
457 1 0 0 1 50 600 Tm % position text
458 <0037 ... 0011> Tj % "under" line
459 1 0 0 1 50 570 Tm % position text
460 <0037 ... 0011> Tj % "over" line
461 ET
462 endstream endobj
463  
464 14 0 obj << /Length 690 >> stream % obj 14 is graphics for (2)
465 0 0 1 RG % stroke blue
466 1 0.647059 0 rg % fill orange
467 130 590 m ... h B % draw and fill circle
468 160 560 20 70 re B % draw and fill bar
469 endstream endobj
470  
471 The third group defines two text and two graphics objects, in the order that
472 they are expected in. The "under" text line is output first, then the orange
473 disc graphics is output, partly covering the text. The "over" text line is now
474 output -- it's actually I<over> the disc, but is orange because the previous
475 object stream (first graphics object) left the fill color (also used for text)
476 as orange, because we didn't explicitly set the fill color before outputting
477 the second text line. This is not "inheritance" so much as it is whatever the
478 graphics (drawing) state (used for both "graphics" and "text") is left in at
479 the end of one object, it's the state at the beginning of the next object.
480 If you wish to control this, consider surrounding the graphics or text calls
481 with C<save()> and C<restore()> calls to save and restore (push and pop) the
482 graphics state to what it was at the C<save()>. Finally, the bar is drawn over
483 everything.
484  
485 # ----------------------------
486 # 3. (2) again, with two graphics and two text objects
487  
488 my $text3 = $page->text();
489 my $grfx3 = $page->gfx();
490 $text3->font($fontTR, 20); # 20 pt Times Roman bold
491 my $text4 = $page->text();
492 my $grfx4 = $page->gfx();
493 $text4->font($fontTR, 20); # 20 pt Times Roman bold
494  
495 $text3->fillcolor('black');
496 $grfx3->strokecolor('blue');
497 $grfx3->fillcolor('orange');
498 # $text4->fillcolor('yellow');
499 # $grfx4->strokecolor('red');
500 # $grfx4->fillcolor('purple');
501  
502 $text3->translate(50,500);
503 $text3->text_left("This text should be under everything.");
504  
505 $grfx3->circle(100,490, 30);
506 $grfx3->fillstroke();
507  
508 $text4->translate(50,470);
509 $text4->text_left("This text should be over the ball and under the bar.");
510  
511 $grfx4->rect(160,460, 20,70);
512 $grfx4->fillstroke();
513  
514 % ---------------- group 3: define text1, graphics1, text2, graphics2
515 15 0 obj << /Length 206 >> stream % obj 15 is text1 for (3)
516 BT
517 /TiCBA 20 Tf % Times Roman Bold 20pt
518 0 0 0 rg % fill black
519 1 0 0 1 50 500 Tm % position text
520 <0037 ... 0011> Tj % "under" line
521 ET
522 endstream endobj
523  
524 16 0 obj << /Length 671 >> stream % obj 16 is graphics1 for (3) circle
525 0 0 1 RG % stroke blue
526 1 0.647059 0 rg % fill orange
527 130 490 m ... h B % draw and fill circle
528 endstream endobj
529  
530 17 0 obj << /Length 257 >> stream % obj 17 is text2 for (3)
531 BT
532 /TiCBA 20 Tf % Times Roman Bold 20pt
533 1 0 0 1 50 470 Tm % position text
534 <0037 ... 0011> Tj % "over" line
535 ET
536 endstream endobj
537  
538 18 0 obj << /Length 20 >> stream % obj 18 is graphics for (3) bar
539 160 460 20 70 re B % draw and fill bar
540 endstream endobj
541  
542 The fourth group is the same as the third, except that we define the fill color
543 for the text in the second line. This makes it clear that the "over" line (in
544 yellow) was written I<after> the orange disc, and still before the bar.
545  
546 # ----------------------------
547 # 4. (3) again, a new set of colors for second group
548  
549 my $text3 = $page->text();
550 my $grfx3 = $page->gfx();
551 $text3->font($fontTR, 20); # 20 pt Times Roman bold
552 my $text4 = $page->text();
553 my $grfx4 = $page->gfx();
554 $text4->font($fontTR, 20); # 20 pt Times Roman bold
555  
556 $text3->fillcolor('black');
557 $grfx3->strokecolor('blue');
558 $grfx3->fillcolor('orange');
559 $text4->fillcolor('yellow');
560 $grfx4->strokecolor('red');
561 $grfx4->fillcolor('purple');
562  
563 $text3->translate(50,400);
564 $text3->text_left("This text should be under everything.");
565  
566 $grfx3->circle(100,390, 30);
567 $grfx3->fillstroke();
568  
569 $text4->translate(50,370);
570 $text4->text_left("This text should be over the ball and under the bar.");
571  
572 $grfx4->rect(160,360, 20,70);
573 $grfx4->fillstroke();
574  
575 % ---------------- group 4: define text1, graphics1, text2, graphics2 with colors for 2
576 19 0 obj << /Length 206 >> stream % obj 19 is text1 for (4)
577 BT
578 /TiCBA 20 Tf % Times Roman Bold 20pt
579 0 0 0 rg % fill black
580 1 0 0 1 50 400 Tm % position text
581 <0037 ... 0011> Tj % "under" line
582 ET
583 endstream endobj
584  
585 20 0 obj << /Length 671 >> stream % obj 20 is graphics1 for (4) circle
586 0 0 1 RG % stroke blue
587 1 0.647059 0 rg % fill orange
588 130 390 m ... h B % draw and fill circle
589 endstream endobj
590  
591 21 0 obj << /Length 266 >> stream % obj 21 is text2 for (4)
592 BT
593 /TiCBA 20 Tf % Times Roman Bold 20pt
594 1 1 0 rg % fill yellow
595 1 0 0 1 50 370 Tm % position text
596 <0037 ... 0011> Tj % "over" line
597 ET
598 endstream endobj
599  
600 22 0 obj << /Length 52 >> stream % obj 22 is graphics for (4) bar
601 1 0 0 RG % stroke red
602 0.498039 0 0.498039 rg % fill purple
603 160 360 20 70 re B % draw and fill rectangle (bar)
604 endstream endobj
605  
606 # ----------------------------
607 $pdf->saveas("$fname.pdf");
608  
609 The separation of text and graphics means that only some text methods are
610 available in a graphics object, and only some graphics methods are available
611 in a text object. There is much overlap, but they differ. There's really no
612 reason the code couldn't have been written (in PDF::API2, or earlier) as
613 outputting to a single object, which would keep everything in the same order as
614 the method calls. An advantage would be less object and stream overhead in the
615 PDF file. The only drawback might be that an object might more easily
616 overflow and require splitting into multiple objects, but that should be rare.
617  
618 You should always be able to manually split an object by simply ending output
619 to the first object, and picking up with output to the second object, I<so long
620 as it was created immediately after the first object.> The graphics state at
621 the end of the first object should be the initial state at the beginning of the
622 second object. B<However,> use caution when dealing with text objects -- the
623 PDF specification states that the Text matrices are I<not> carried over from
624 one object to the next (B<BT> resets them), so you may need to reset some
625 settings.
626  
627 $grfx1 = $page->gfx();
628 $grfx2 = $page->gfx();
629 # write a huge amount of stuff to $grfx1
630 # write a huge amount of stuff to $grfx2, picking up where $grfx1 left off
631  
632 =head3 Rendering with a shared (mixed) object
633  
634 In any case, now that you understand the rendering order and how the order
635 of object declarations affects it, how text and graphics are drawn can now be
636 completely controlled as desired. There is really no need to add another "both"
637 type object that will handle all graphics and text objects, as that would
638 probably be a major code bloat for very little benefit. However, it could be
639 considered in the future if there is a demonstrated need for it, such as
640 serious PDF file size bloat due to the extra object overhead when interleaving
641 text and graphics output.
642  
643 There is not currently a general facility for mixed-use objects, but a limited
644 example is the current implementation of underline, line-through, and overline
645 text (within C<column()> markup); which are performed within the text object,
646 temporarily exiting (ET) to graphics mode to draw the lines, and then returning
647 (BT) to text mode. This was done so that baseline coordinate adjustments could
648 be easily made. Since "BT" resets some text settings, this needs to be done
649 with care!
650  
651 A number of PDF-producing products out there mix their text and graphics into a
652 single object. So long as PDF::Builder is I<not> expecting to pick apart a
653 "text" or "graphics" stream from an existing PDF file, in order to look inside
654 it, this should not be expected to cause any problems. However, if you add a
655 new text-only or graphics-only object to an existing PDF via PDF::Builder, you
656 need to be aware of the rendering order. Presumably the new object(s) will be
657 I<after> the original one, but you should be aware of what's going on. A text,
658 graphics, or mixed object is all the same to a PDF reader, which cares only
659 for who has the lower object number (to be rendered first).
660  
661 =head3 Graphics state left by existing PDFs
662  
663 There was a problem recently reported (ticket 231) which suggested that the
664 expected rendering order of output wasn't being respected. What it turned out
665 to be was that a utility ("Canva") used to generate a template PDF, was writing
666 graphics commands to a single stream (a mixed graphics and text stream, rather than
667 separate streams, but that's not important here). Apparently it was leaving
668 the PDF it created with a graphics state of B<transparent> "fill" mode. Further
669 text (as well as filled graphics draws) added by the user in PDF::Builder (open
670 PDF, open_page, add graphics and text in new streams) was still being drawn,
671 but was made invisible when rendered! Remember that text is by default (render
672 mode 0) drawn only with fill, not outlined. The solution was to first use the
673 C<egstate()> call to set transparency back to 0 (off), making "fill" opaque
674 again. See C<examples/060_transparency> for a code sample, or ticket 231.
675  
676 =head2 Notes on Reader support of features
677  
678 PDF Readers are complex pieces of software, written by different groups at
679 different times. Thus, they may differ in how they support features and handle
680 non-standard (i.e., not quite meeting standards) content! Most Readers out
681 there support all or most features up through PDF 1.7, and some support PDF 2.x
682 features. Note that PDF::Builder supports PDF 1.4 for the most part, with a few
683 PDF 1.5 features added. Most any Reader out there I<should> (in theory) support
684 any PDF produced with PDF::Builder.
685  
686 There is no official I<reference implementation> of a Reader, although Adobe's
687 Acrobat Reader (I<AAR>, a free download) is so prevalent that it is almost a
688 I<de facto> standard. At least, we I<try> to get PDF::Builder and its tests and
689 examples to run on AAR. Sometimes it can be difficult, as, for example, the
690 handling of save (B<q>) and restore (B<Q>) operators (commands) within a text
691 stream. The PDF standard sort of suggests that these apply only to the Graphics
692 Stream, and possibly shouldn't appear in a Text Stream. Most Readers appear to
693 just ignore q and Q within a text stream, and AAR usually seems to, but certain
694 combinations of stream size and compression seem to trigger a warning in AAR
695 upon load! This particular case is now a moot point, as C<save()> and
696 C<restore()> have been reverted to being no-ops (with a single warning message
697 given if found) in a Text Stream.
698  
699 We have been advised that certain stream operators may not be strictly allowed
700 within certain parts of a stream (particularly certain graphics state operators
701 after path construction has started). No Reader seems to give problems with
702 this at the moment, but users should be aware that the ordering of their
703 PDF::Builder calls I<may> need to be updated at some point, to get PDFs usable
704 on all Readers. If necessary, we will add code to enforce this (or at least,
705 warn of potential problems). Please feel free to report if you find such
706 restrictions are necessary.
707  
708 Also note that not all I<filters> (including compression methods) may be
709 supported on all Readers. For example, at this time, AAR (and a number of other
710 Readers) apparently do not support CCITT Group 4 Fax compression (for some TIFF
711 images). This remains under investigation.
712  
713 =head2 PDF Versions Supported
714  
715 When creating a PDF file using the functions in PDF::Builder, the output is
716 marked as PDF 1.4. This does not mean that all I<PDF> functionality up through
717 1.4 is supported! There are almost surely features missing as far back as the
718 PDF 1.0 standard.
719  
720 The big problem is when a PDF of version 1.5 or higher is imported or opened
721 in PDF::Builder. If it contains content that is actually unsupported by this
722 software, there is a chance that something will break. This does not guarantee
723 that a PDF marked as "1.7" will go down in flames when read by PDF::Builder,
724 or that a PDF written back out will break in a Reader, but the possibility is
725 there. Much PDF writer software simply marks its output as the highest version
726 of PDF at the time (usually 1.7), even if there is no content beyond, say, 1.2.
727 There is I<some> handling of PDF 1.5 items in PDF::Builder, such as cross
728 reference streams, but support beyond 1.4 is very limited. All we can say is to
729 be careful when handling PDFs whose version is above 1.4, and test thoroughly,
730 as they may break at some point.
731  
732 PDF::Builder includes a simple version control mechanism, where the initial
733 PDF version to be output (default 1.4) can be set by the programmer. Input
734 PDFs greater than 1.4 (current output level) will receive a warning (can be
735 suppressed) that the output level will be raised to that level. The use of PDF
736 features greater than the current output level will likewise trigger a warning
737 that the output level is to be raised to the necessary level. If this is not
738 desired, you should avoid using those PDF features which are higher than the
739 desired PDF output level.
740  
741 =head2 History
742  
743 PDF::API2 was originally written by Alfred Reibenschuh, derived from Martin
744 Hosken's Text::PDF via the Text::PDF::API wrapper.
745 In 2009, Otto Hirr started the PDF::API3 fork, but it never went anywhere.
746 In 2011, PDF::API2 maintenance was taken over by Steve Simms.
747 In 2017, PDF::Builder was forked by Phil M. Perry, who desired a more aggressive
748 schedule of new features and bug fixes than Simms was providing, although some
749 of Simms's work I<has> been ported from PDF::API2.
750  
751 According to Alfred Reibenschuh's 2005 presentation
752 "pdfapi2_for_fun_and_profit_APW2005.pdf" (on
753 http://pdfapi2.sourceforge.net, an unmaintained site), the history of PDF::API2
754 (the predecessor to PDF::Builder) goes as such:
755  
756 =over
757  
758 =item *
759  
760 First Code implemented based on PDFlib-0.6 (AFPL)
761  
762 =item *
763  
764 Changed to Text::PDF with a total rewrite as Text::PDF::API (procedural)
765  
766 =item *
767  
768 Unmaintainable Code triggered rewrite into new Namespace PDF::API2 (object-oriented, LGPL)
769  
770 =item *
771  
772 Object-Structure streamlined in 0.4x
773  
774 =back
775  
776 At Simms's request, the name of the new offering was changed from PDF::API4
777 to PDF::Builder, to reduce the chance of confusion due to parallel development.
778 Perry's intent is to keep all internal methods as upwardly compatible with
779 PDF::API2 as possible, although it is likely that there will be some drift
780 (incompatibilities) over time. At least initially, any program written based on
781 PDF::API2 should be convertible to PDF::Builder simply by changing "API2"
782 anywhere it occurs to "Builder". See the INFO/KNOWN_INCOMP known
783 incompatibilities file for further information.
784  
785 =head3 Thanks...
786  
787 Many users have helped out by reporting bugs and requesting enhancements. A
788 special shout out goes to those who have contributed code and tests, or
789 coordinated their package development with the needs of PDF::Builder:
790 Ben Bullock, Cary Gravel, Gregor Herrmann, Petr Pisar, Jeffrey Ratcliffe,
791 Steve Simms (via PDF::API2 fixes), and Johan Vromans.
792 Drop me a line if I've overlooked your contribution!
793  
794 =head1 DETAILED NOTES ON METHODS
795  
796 B<Note:> older versions of this package named various (hash element) options
797 with leading dashes (hyphens) in the name, e.g., '-encode'. The use of a dash
798 is now optional, and options are documented with names I<not> using dashes.
799 While the use of dashed option names is deprecated and discouraged, they will
800 remain legal for some time to come. At some point in the future, it is possible
801 that support for dashed names will be withdrawn, so it would be good practice
802 to start using undashed names in new and revised code.
803  
804 =head2 After saving a file...
805  
806 Note that a PDF object such as C<$pdf> cannot continue to be used after saving
807 an output PDF file or string with $pdf->C<save()>, C<saveas()>, or
808 C<stringify()>. There is some cleanup and other operations done internally
809 which make the object unusable for further operations. You will likely receive
810 an error message about B<can't call method new_obj on an undefined value> if
811 you try to keep using a PDF object.
812  
813 =head2 IntegrityCheck
814  
815 The PDF::Builder methods that open an existing PDF file, pass it by the
816 integrity checker method, C<$self-E<gt>IntegrityCheck(level, content)>. This method
817 servers two purposes: 1) to find any C</Version> settings that override the
818 PDF version found in the PDF heading, and 2) perform some basic validations on
819 the contents of the PDF.
820  
821 The C<level> parameter accepts the following values:
822  
823 =over
824  
825 =item Z<>0
826  
827 Do not output any diagnostic messages; just return any version override.
828  
829 =item Z<>1
830  
831 Output error-level (serious) diagnostic messages, as well as returning any version override.
832  
833 Errors include, in no place was the /Root object specified, or if it was, the indicated object was not found. An object claims another object as its child (/Kids list), but another object has already claimed that child. An object claims a child, but that child does not list a Parent, or the child lists a different Parent.
834  
835 =item Z<>2
836  
837 Output error- (serious) and warning- (less serious) level diagnostic messages, as well as returning any version override. B<This is the default.>
838  
839 =item Z<>3
840  
841 Output error- (serious), warning- (less serious), and note- (informational) level diagnostic messages, as well as returning any version override.
842  
843 Notes include, in no place was the (optional) /Info object specified, or if it was, the indicated object was not found. An object was referenced, but no entry for it was found among the objects. (This may be OK if the object is not defined, or is on the free list, as the reference will then be ignored.) An object is defined, but it appears that no other object is referencing it.
844  
845 =item Z<>4
846  
847 Output error-, warning-, and note-level diagnostic messages, as well as returning any version override. Also dump the diagnostic data structure.
848  
849 =item Z<>5
850  
851 Output error-, warning-, and note-level diagnostic messages, as well as returning any version override. Also dump the diagnostic data structure and the C<$self> data structure (generally useful only if you have already read in the PDF file).
852  
853 =back
854  
855 The version is a string (e.g., '1.5') if found, otherwise C<undef> (undefined value) is returned.
856  
857 For controlling the "automatic" call to IntegrityCheck (via opens), the level
858 may be given with the option (flag) C<diaglevel =E<gt> I<n>>, where C<n> is between 0 and 5.
859  
860 =head2 Preferences - set user display preferences
861  
862 =over
863  
864 =item $pdf->preferences(%options)
865  
866 Controls viewing preferences for the PDF.
867  
868 =back
869  
870 =head3 Page Mode Options
871  
872 =over
873  
874 =item fullscreen
875  
876 Full-screen mode, with no menu bar, window controls, or any other window visible.
877  
878 =item thumbs
879  
880 Thumbnail images visible.
881  
882 =item outlines
883  
884 Document outline visible.
885  
886 =back
887  
888 =head3 Page Layout Options
889  
890 =over
891  
892 =item singlepage
893  
894 Display one page at a time.
895  
896 =item onecolumn
897  
898 Display the pages in one column.
899  
900 =item twocolumnleft
901  
902 Display the pages in two columns, with odd-numbered pages on the left.
903  
904 =item twocolumnright
905  
906 Display the pages in two columns, with odd-numbered pages on the right.
907  
908 =back
909  
910 =head3 Viewer Options
911  
912 =over
913  
914 =item hidetoolbar
915  
916 Specifying whether to hide tool bars.
917  
918 =item hidemenubar
919  
920 Specifying whether to hide menu bars.
921  
922 =item hidewindowui
923  
924 Specifying whether to hide user interface elements.
925  
926 =item fitwindow
927  
928 Specifying whether to resize the document's window to the size of the displayed page.
929  
930 =item centerwindow
931  
932 Specifying whether to position the document's window in the center of the screen.
933  
934 =item displaytitle
935  
936 Specifying whether the window's title bar should display the
937 document title taken from the Title entry of the document information
938 dictionary.
939  
940 =item afterfullscreenthumbs
941  
942 Thumbnail images visible after Full-screen mode.
943  
944 =item afterfullscreenoutlines
945  
946 Document outline visible after Full-screen mode.
947  
948 =item printscalingnone
949  
950 Set the default print setting for page scaling to none.
951  
952 =item simplex
953  
954 Print single-sided by default.
955  
956 =item duplexflipshortedge
957  
958 Print duplex by default and flip on the short edge of the sheet.
959  
960 =item duplexfliplongedge
961  
962 Print duplex by default and flip on the long edge of the sheet.
963  
964 =back
965  
966 =head3 Page Fit Options
967  
968 These options are used for the C<firstpage> layout, as well as for
969 Annotations, Named Destinations and Outlines.
970 For Annotations and Named Destinations, the first form given is for the
971 fit as a hash element (option), while the second is as given as a list (before
972 any options).
973  
974 =over
975  
976 =item 'fit' => 1
977  
978 =item 'fit'
979  
980 Display the page designated by C<$page>, with its contents magnified just
981 enough to fit the entire page within the window both horizontally and
982 vertically. If the required horizontal and vertical magnification
983 factors are different, use the smaller of the two, centering the page
984 within the window in the other dimension. Bottom line: the entire page is
985 shown, as large as possible.
986  
987 =item 'fith' => $top
988  
989 =item 'fith', $top
990  
991 Display the page designated by C<$page>, with the vertical coordinate C<$top>
992 positioned at the top edge of the window and the contents of the page
993 magnified just enough to fit the entire width of the page within the
994 window. Bottom line: like 'fit', but scrolled vertically to put desired y
995 coordinate at the very top edge.
996  
997 =item 'fitv' => $left
998  
999 =item 'fitv', $left
1000  
1001 Display the page designated by C<$page>, with the horizontal coordinate
1002 C<$left> positioned at the left edge of the window and the contents of the
1003 page magnified just enough to fit the entire height of the page within
1004 the window. Bottom line: like 'fit', but scrolled horizontally to put desired x
1005 coordinate at the very left edge.
1006  
1007 =item 'fitr' => [ $left, $bottom, $right, $top ]
1008  
1009 =item 'fitr', $left, $bottom, $right, $top
1010  
1011 Display the page designated by C<$page>, with its contents magnified just
1012 enough to fit the rectangle specified by the coordinates C<$left>, C<$bottom>,
1013 C<$right>, and C<$top> entirely within the window both horizontally and
1014 vertically. If the required horizontal and vertical magnification
1015 factors are different, use the smaller of the two, centering the
1016 rectangle within the window in the other dimension. Bottom line: defines a
1017 I<viewport> into the page by giving its corners. Cf. 'xyz', but all 4 corners
1018 of the viewport rectangle are given.
1019  
1020 =item 'fitb' => 1
1021  
1022 =item 'fitb'
1023  
1024 Display the page designated by C<$page>, with its contents magnified just
1025 enough to fit its bounding box entirely within the window both
1026 horizontally and vertically. If the required horizontal and vertical
1027 magnification factors are different, use the smaller of the two,
1028 centering the bounding box within the window in the other dimension.
1029 Similar to 'fit'.
1030  
1031 =item 'fitbh' => $top
1032  
1033 =item 'fitbh', $top
1034  
1035 Display the page designated by C<$page>, with the vertical coordinate C<$top>
1036 positioned at the top edge of the window and the contents of the page
1037 magnified just enough to fit the entire width of its bounding box
1038 within the window. Like 'fitb', but scrolled vertically so that C<$top> is
1039 at the top edge of the display.
1040  
1041 =item 'fitbv' => $left
1042  
1043 =item 'fitbv', $left
1044  
1045 Display the page designated by C<$page>, with the horizontal coordinate
1046 C<$left> positioned at the left edge of the window and the contents of the
1047 page magnified just enough to fit the entire height of its bounding
1048 box within the window. Like 'fitb', but scrolled horizontally so that C<$left>
1049 is at the left edge of the display.
1050  
1051 =item 'xyz' => [ $left, $top, $zoom ]
1052  
1053 =item 'xyz', $left, $top, $zoom
1054  
1055 Display the page designated by C<$page>, with the coordinates C<[$left, $top]>
1056 positioned at the B<top-left corner> of the viewport and the contents of
1057 the page B<magnified> by the factor C<$zoom>. An C<undef> value for any of the
1058 parameters C<$left>, C<$top>, or C<$zoom> specifies that the current value of
1059 that parameter is to be retained unchanged. So, if you are viewing a page at
1060 a certain location and zoom factor, your new page should be positioned at the
1061 same x,y and be at the same zoom (if all three are C<undef>). Cf. 'fitr', but
1062 only one corner is given, along with the magnification.
1063  
1064 =back
1065  
1066 =head3 Initial Page Options
1067  
1068 =over
1069  
1070 =item firstpage => [ $page, %options ]
1071  
1072 Specifying the page (either a page number or a page object) to be
1073 displayed, plus one of the location options listed above in L<Page Fit Options>.
1074  
1075 =back
1076  
1077 =head3 Example
1078  
1079 $pdf->preferences(
1080 fullscreen => 1,
1081 onecolumn => 1,
1082 afterfullscreenoutlines => 1,
1083 firstpage => [$page, fit => 1],
1084 );
1085  
1086 =head2 info Example
1087  
1088 %h = $pdf->info(
1089 'Author' => "Alfred Reibenschuh",
1090 'CreationDate' => "D:20020911000000+01'00'",
1091 'ModDate' => "D:YYYYMMDDhhmmssOHH'mm'",
1092 'Creator' => "fredos-script.pl",
1093 'Producer' => "PDF::Builder",
1094 'Title' => "some Publication",
1095 'Subject' => "perl ?",
1096 'Keywords' => "all good things are pdf"
1097 );
1098 print "Author: $h{'Author'}\n";
1099  
1100 =head2 XMP XML example
1101  
1102 $xml = $pdf->xmpMetadata();
1103 print "PDFs Metadata reads: $xml\n";
1104 $xml=<<EOT;
1105 <?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>
1106 <?adobe-xap-filters esc="CRLF"?>
1107 <x:xmpmeta
1108 xmlns:x='adobe:ns:meta/'
1109 x:xmptk='XMP toolkit 2.9.1-14, framework 1.6'>
1110 <rdf:RDF
1111 xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
1112 xmlns:iX='http://ns.adobe.com/iX/1.0/'>
1113 <rdf:Description
1114 rdf:about='uuid:b8659d3a-369e-11d9-b951-000393c97fd8'
1115 xmlns:pdf='http://ns.adobe.com/pdf/1.3/'
1116 pdf:Producer='Acrobat Distiller 6.0.1 for Macintosh'></rdf:Description>
1117 <rdf:Description
1118 rdf:about='uuid:b8659d3a-369e-11d9-b951-000393c97fd8'
1119 xmlns:xap='http://ns.adobe.com/xap/1.0/'
1120 xap:CreateDate='2004-11-14T08:41:16Z'
1121 xap:ModifyDate='2004-11-14T16:38:50-08:00'
1122 xap:CreatorTool='FrameMaker 7.0'
1123 xap:MetadataDate='2004-11-14T16:38:50-08:00'></rdf:Description>
1124 <rdf:Description
1125 rdf:about='uuid:b8659d3a-369e-11d9-b951-000393c97fd8'
1126 xmlns:xapMM='http://ns.adobe.com/xap/1.0/mm/'
1127 xapMM:DocumentID='uuid:919b9378-369c-11d9-a2b5-000393c97fd8'/></rdf:Description>
1128 <rdf:Description
1129 rdf:about='uuid:b8659d3a-369e-11d9-b951-000393c97fd8'
1130 xmlns:dc='http://purl.org/dc/elements/1.1/'
1131 dc:format='application/pdf'>
1132 <dc:description>
1133 <rdf:Alt>
1134 <rdf:li xml:lang='x-default'>Adobe Portable Document Format (PDF)</rdf:li>
1135 </rdf:Alt>
1136 </dc:description>
1137 <dc:creator>
1138 <rdf:Seq>
1139 <rdf:li>Adobe Systems Incorporated</rdf:li>
1140 </rdf:Seq>
1141 </dc:creator>
1142 <dc:title>
1143 <rdf:Alt>
1144 <rdf:li xml:lang='x-default'>PDF Reference, version 1.6</rdf:li>
1145 </rdf:Alt>
1146 </dc:title>
1147 </rdf:Description>
1148 </rdf:RDF>
1149 </x:xmpmeta>
1150 <?xpacket end='w'?>
1151 EOT
1152  
1153 $xml = $pdf->xmpMetadata($xml);
1154 print "PDF metadata now reads: $xml\n";
1155  
1156 =head2 "BOX" METHODS
1157  
1158 B<A general note:> Use care if specifying a different Media Box (or other "box")
1159 for a page, than the global "box" setting, to define the whole "chain" of boxes
1160 on the page, to avoid surprises. For example, to define a global Media Box
1161 (paper size) and a global Crop Box, and then define a new page-level Media Box
1162 I<without> defining a new page-level Crop Box, may give odd results in the
1163 resultant cropping. Such combinations are not well defined.
1164  
1165 All dimensions in boxes default to the default User Unit, which is points (1/72
1166 inch). Note that the PDF specification limits sizes and coordinates to 14400
1167 User Units (200 inches, for the default User Unit of one point), and Adobe
1168 products (so far) follow this limit for Acrobat and Distiller. It is worth
1169 noting that other PDF writers and readers may choose to ignore the 14400 unit
1170 limit, with or without the use of a specified User Unit. Therefore, PDF::Builder
1171 does not enforce any limits on coordinates -- it's I<your> responsibility to
1172 consider what readers and other PDF tools may be used with a PDF you produce!
1173 Also note that earlier Acrobat readers had coordinate limits as small as 3240
1174 User Units (45 inches), and I<minimum> media size of 72 or 3 User Units.
1175  
1176 =head3 User Units (userunit)
1177  
1178 $pdf->userunit($number)
1179  
1180 =over
1181  
1182 The default User Unit in the PDF coordinate system is one point (1/72 inch). You
1183 can think of it as a scale factor to enable larger (or even, smaller) documents.
1184 This method may be used (for PDF 1.6 and higher) to set the User Unit to some
1185 number of points. For example, C<userunit(72)> will set the scale multiplier to
1186 72.0 points per User Unit, or 1 inch to the User Unit. Any number greater than
1187 zero is acceptable, although some readers and tools may not handle User Units of
1188 less than 1.0 very well.
1189  
1190 Not all readers respect the User Unit, if you give one, or handle it in exactly
1191 the same way. Adobe Distiller, for one, does not use it. How User Units are
1192 handled may vary from reader to reader. Adobe Acrobat, at this writing, respects
1193 User Unit in version 7.0 and up, but limits it to 75000 (giving a maximum
1194 document size of 15 million inches or 236.7 miles or 381 km). Other readers and
1195 PDF tools may allow a larger (or smaller) limit.
1196  
1197 B<Your Mileage May Vary:> Some readers ignore a global
1198 User Unit setting and do I<not> have pages inherit it (PDF::Builder duplicates
1199 it on each page to simulate inheritance). Some readers may give spurious
1200 warnings about truncated content when a Media Box is changed while User Units
1201 are being used. Some readers do strange things with Crop Boxes when a User Unit
1202 is in effect.
1203  
1204 Depending on the reader used, the effect of a larger User Unit (greater than 1)
1205 may mean lower resolution (chunkier or coarser appearance) in the rendered
1206 document. If you're printing something the size of a highway billboard, this may
1207 not matter to you, but you should be aware of the possibility (even with
1208 fractional coordinates). Conversely, a User Unit of less than 1.0 (if permitted)
1209 reduces the allowable size of your document, but I<may> result in greater
1210 resolution.
1211  
1212 A global (PDF level) User Unit setting is inherited by each page (an action by
1213 PDF::Builder, not necessarily automatically done by the reader), or can be
1214 overridden by calling userunit in the page. Do not give more than one global
1215 userunit setting, as only the last one will be used.
1216 Setting a page's User Unit (if C<< $page-> >> instead) is permitted (overriding
1217 the global setting for this page). However, many sources recommend against
1218 doing this, as results may not be as expected (once again, depending on the
1219 quirks of the reader).
1220  
1221 Remember to call C<userunit> I<before> calling anything having to do with page
1222 or box sizes, or coordinates. Especially when setting 'named' box sizes, the
1223 methods need to know the current User Unit so that named page sizes (in points)
1224 may be scaled down to the current User Unit.
1225  
1226 =back
1227  
1228 =head3 Media Box (mediabox)
1229  
1230 $pdf->mediabox($name)
1231  
1232 $pdf->mediabox($name, orient => 'orientation' )
1233  
1234 $pdf->mediabox($w,$h)
1235  
1236 $pdf->mediabox($llx,$lly, $urx,$ury)
1237  
1238 ($llx,$lly, $urx,$ury) = $pdf->mediabox()
1239  
1240 =over
1241  
1242 Sets the global Media Box (or page's Media Box, if C<< $page-> >> instead).
1243 This defines the width and height (or by corner
1244 coordinates, or by standard name) of the output page itself, such as the
1245 physical paper size. This is normally the largest of the "boxes". If any
1246 subsidiary box (within it) exceeds the media box, the portion of the material
1247 or boxes outside of the Media Box will be ignored. That is, the Media Box is
1248 the One Box to Rule Them All, and is the overall limit for other boxes (some
1249 documentation refers to the Media Box as "clipping" other boxes). In
1250 addition, the Media Box defines the overall I<coordinate system> for text and
1251 graphics operations.
1252  
1253 If no arguments are given, the current Media Box (global or page) coordinates
1254 are returned instead. The former C<get_mediabox> (page only) function was
1255 B<deprecated> and has been removed. In addition,
1256 when I<setting> the Media Box, the resulting coordinates are returned. This
1257 permits you to specify the page size by a name (alias) and get the dimensions
1258 back, all in one call.
1259  
1260 Note that many printers can B<not> print all the way to the
1261 physical edge of the paper, so you should plan to leave some blank margin,
1262 even outside of any crop marks and bleeds. Printers and on-screen readers are
1263 free to discard any content found outside the Media Box, and printers may
1264 discard some material just inside the Media Box.
1265  
1266 A I<global> Media Box is B<required> by the PDF spec; if not explicitly given,
1267 PDF::Builder will set the global Media Box to US Letter size (8.5in x 11in).
1268 This is the media size that will be used for all pages if you do not specify
1269 a C<mediabox> call on a page. That is,
1270 a global (PDF level) mediabox setting is inherited by each page, or can be
1271 overridden by setting mediabox in the page. Do not give more than one global
1272 mediabox setting, as only the last one will be used.
1273  
1274 If you give a single string name (e.g., 'A4'), you may optionally add an
1275 orientation to turn the page 90 degrees into Landscape mode:
1276 C<< orient => 'L' >> or C<< orient => 'l' >>. C<orient> is the only option
1277 recognized, and a string beginning with an 'L' or 'l' (for Landscape) is the
1278 only value of interest (anything else is treated as Portrait mode). The I<y>
1279 axis still runs from 0 at the bottom of the page to what used to be the page
1280 I<width> (now, I<height>) at the top, and likewise for the I<x> axis: 0 at left
1281 to (former) I<height> at the right. That is, the coordinate system is the same
1282 as before, except that the height and width are different.
1283  
1284 The lower left corner does not I<have> to be 0,0. It can be any values you want,
1285 including negative values (so long as the resulting media's sides are at least
1286 one point long). C<mediabox> sets the coordinate system (including the origin)
1287 of the graphics and text that will be drawn, as well as for subsequent "boxes".
1288 It's even possible to give any two opposite corners (such as upper left and
1289 lower right). The coordinate system will be rearranged (by the Reader) to
1290 still be the conventional minimum C<x> and C<y> in the lower left (i.e., you
1291 can't make C<y> I<increase> from top to bottom!).
1292  
1293 B<Example:>
1294  
1295 =back
1296  
1297 $pdf = PDF::Builder->new();
1298 $pdf->mediabox('A4'); # A4 size (595 Pt wide by 842 Pt high)
1299 ...
1300 $pdf->saveas('our/new.pdf');
1301  
1302 $pdf = PDF::Builder->new();
1303 $pdf->mediabox(595, 842); # A4 size, with implicit 0,0 LL corner
1304 ...
1305 $pdf->saveas('our/new.pdf');
1306  
1307 $pdf = PDF::Builder->new;
1308 $pdf->mediabox(0, 0, 595, 842); # A4 size, with explicit 0,0 LL corner
1309 ...
1310 $pdf->saveas('our/new.pdf');
1311  
1312 =over
1313  
1314 See the L<PDF::Builder::Resource::PaperSizes> source code for the full list of
1315 supported names (aliases) and their dimensions in points. You are free to add
1316 additional paper sizes to this file, if you wish. You might want to do this if
1317 you frequently use a standard page size in rotated (Landscape) mode. See also
1318 the C<getPaperSizes> call in L<PDF::Builder::Util>. These names (aliases) are
1319 also usable in other "box" calls, although useful only if the "box" is the same
1320 size as the full media (Media Box), and you don't mind their starting at 0,0.
1321  
1322 =back
1323  
1324 =head3 Crop Box (cropbox)
1325  
1326 $pdf->cropbox($name)
1327  
1328 $pdf->cropbox($name, orient => 'orientation')
1329  
1330 $pdf->cropbox($w,$h)
1331  
1332 $pdf->cropbox($llx,$lly, $urx,$ury)
1333  
1334 ($llx,$lly, $urx,$ury) = $pdf->cropbox()
1335  
1336 =over
1337  
1338 Sets the global Crop Box (or page's Crop Box, if C<< $page-> >> instead).
1339 This will define the media size to which the output will
1340 later be I<clipped>. Note that this does B<not> itself output any crop marks
1341 to guide cutting of the paper! PDF Readers should consider this to be the
1342 I<visible> portion of the page, and anything found outside it I<may> be clipped
1343 (invisible). By default, it is equal to the Media Box, but may be defined to be
1344 smaller, in the coordinate system set by the Media Box. A global setting will
1345 be inherited by each page, but can be overridden on a per-page basis.
1346  
1347 A Reader or Printer may choose to discard any clipped (invisible) part of the
1348 page, and show only the area I<within> the Crop Box. For example, if your page
1349 Media Box is A4 (0,0 to 595,842 Points), and your Crop Box is (100,100 to
1350 495,742), a reader such as Adobe Acrobat Reader may show you a page 395 by
1351 642 Points in size (i.e., just the visible area of your page). Other Readers
1352 may show you the full media size (Media Box) and a 100 Point wide blank area
1353 (in this example) around the visible content.
1354  
1355 If no arguments are given, the current Crop Box (global or page) coordinates
1356 are returned instead. The former C<get_cropbox> (page only) function was
1357 B<deprecated> and has been removed. If a Crop Box
1358 has not been defined, the Media Box coordinates (which always exist) will be
1359 returned instead. In addition,
1360 when I<setting> the Crop Box, the resulting coordinates are returned. This
1361 permits you to specify the crop box by a name (alias) and get the dimensions
1362 back, all in one call.
1363  
1364 Do not confuse the Crop Box with the C<Trim Box>, which shows where printed
1365 paper is expected to actually be I<cut>. Some PDF Readers may reduce the
1366 visible "paper" background to the size of the crop box; others may simply omit
1367 any content outside it. Either way, you would lose any trim or crop marks,
1368 printer instructions, color alignment dots, or other content outside the Crop
1369 Box. I<A good use of the Crop Box> would be limit printing to the area where a
1370 printer I<can> reliably put down ink, and leave white the edge areas where
1371 paper-handling mechanisms prevent ink or toner from being applied. This would
1372 keep you from accidentally putting valuable content in an area where a printer
1373 will refuse to print, yet permit you to include a bleed area and space for
1374 printer's marks and instructions. Needless to say, if your printer cannot print
1375 to the very edge of the paper, you will need to trim (cut) the printed sheets
1376 to get true bleeds.
1377  
1378 A global (PDF level) cropbox setting is inherited by each page, or can be
1379 overridden by setting cropbox in the page.
1380 As with C<mediabox>, only one crop box may be set at this (PDF) level.
1381 As with C<mediabox>, a named media size may have an orientation (l or L) for
1382 Landscape mode.
1383 Note that the PDF level global Crop Box will be used I<even if> the page gets
1384 its own Media Box. That is, the page's Crop Box inherits the global Crop Box,
1385 not the page Media Box, even if the page has its own media size! If you set the
1386 page's own Media Box, you should consider also explicitly setting the page
1387 Crop Box (and other boxes).
1388  
1389 =back
1390  
1391 =head3 Bleed Box (bleedbox)
1392  
1393 $pdf->bleedbox($name)
1394  
1395 $pdf->bleedbox($name, orient => 'orientation')
1396  
1397 $pdf->bleedbox($w,$h)
1398  
1399 $pdf->bleedbox($llx,$lly, $urx,$ury)
1400  
1401 ($llx,$lly, $urx,$ury) = $pdf->bleedbox()
1402  
1403 =over
1404  
1405 Sets the global Bleed Box (or page's Bleed Box, if C<< $page-> >> instead).
1406 This is typically used in printing on paper, where you want
1407 ink or color (such as thumb tabs) to be printed a bit beyond the final paper
1408 size, to ensure that the cut paper I<bleeds> (the cut goes I<through> the ink),
1409 rather than accidentally leaving some white paper visible outside. Allow
1410 enough "bleed" over the expected trim line to account for minor variations in
1411 paper handling, folding, and cutting; to avoid showing white paper at the edge.
1412 The Bleed Box is where I<printing> could actually extend to; the Trim Box is
1413 normally within it, where the paper would actually be I<cut>. The default
1414 value is equal to the Crop Box, but is often a bit smaller. The space between
1415 the Bleed Box and the Crop Box is available for printer instructions, color
1416 alignment dots, etc., while crop marks (trim guides) are at least partly within
1417 the bleed area (and should be printed after content is printed).
1418  
1419 If no arguments are given, the current Bleed Box (global or page) coordinates
1420 are returned instead. The former C<get_bleedbox> (page only) function was
1421 B<deprecated> and has been removed. If a Bleed Box
1422 has not been defined, the Crop Box coordinates (if defined) will be returned,
1423 otherwise the Media Box coordinates (which always exist) will be returned.
1424 In addition, when I<setting> the Bleed Box, the resulting coordinates are
1425 returned. This permits you to specify the bleed box by a name (alias) and get
1426 the dimensions back, all in one call.
1427  
1428 A global (PDF level) bleedbox setting is inherited by each page, or can be
1429 overridden by setting bleedbox in the page.
1430 As with C<mediabox>, only one bleed box may be set at this (PDF) level.
1431 As with C<mediabox>, a named media size may have an orientation (l or L) for
1432 Landscape mode.
1433 Note that the PDF level global Bleed Box will be used I<even if> the page gets
1434 its own Crop Box. That is, the page's Bleed Box inherits the global Bleed Box,
1435 not the page Crop Box, even if the page has its own media size! If you set the
1436 page's own Media Box or Crop Box, you should consider also explicitly setting
1437 the page Bleed Box (and other boxes).
1438  
1439 =back
1440  
1441 =head3 Trim Box (trimbox)
1442  
1443 $pdf->trimbox($name)
1444  
1445 $pdf->trimbox($name, orient => 'orientation')
1446  
1447 $pdf->trimbox($w,$h)
1448  
1449 $pdf->trimbox($llx,$lly, $urx,$ury)
1450  
1451 ($llx,$lly, $urx,$ury) = $pdf->trimbox()
1452  
1453 =over
1454  
1455 Sets the global Trim Box (or page's Trim Box, if C<< $page-> >> instead).
1456 This is supposed to be the actual dimensions of the
1457 finished page (after trimming of the paper). In some production environments,
1458 it is useful to have printer's instructions, cut marks, and so on outside of
1459 the trim box. The default value is equal to Crop Box, but is often a bit
1460 smaller than any Bleed Box, to allow the desired "bleed" effect.
1461  
1462 If no arguments are given, the current Trim Box (global or page) coordinates
1463 are returned instead. The former C<get_trimbox> (page only) function was
1464 B<deprecated> and has been removed. If a Trim Box
1465 has not been defined, the Crop Box coordinates (if defined) will be returned,
1466 otherwise the Media Box coordinates (which always exist) will be returned.
1467 In addition, when I<setting> the Trim Box, the resulting coordinates are
1468 returned. This permits you to specify the trim box by a name (alias) and get
1469 the dimensions back, all in one call.
1470  
1471 A global (PDF level) trimbox setting is inherited by each page, or can be
1472 overridden by setting trimbox in the page.
1473 As with C<mediabox>, only one trim box may be set at this (PDF) level.
1474 As with C<mediabox>, a named media size may have an orientation (l or L) for
1475 Landscape mode.
1476 Note that the PDF level global Trim Box will be used I<even if> the page gets
1477 its own Crop Box. That is, the page's Trim Box inherits the global Trim Box,
1478 not the page Crop Box, even if the page has its own media size! If you set the
1479 page's own Media Box or Crop Box, you should consider also explicitly setting
1480 the page Trim Box (and other boxes).
1481  
1482 =back
1483  
1484 =head3 Art Box (artbox)
1485  
1486 $pdf->artbox($name)
1487  
1488 $pdf->artbox($name, orient => 'orientation')
1489  
1490 $pdf->artbox($w,$h)
1491  
1492 $pdf->artbox($llx,$lly, $urx,$ury)
1493  
1494 ($llx,$lly, $urx,$ury) = $pdf->artbox()
1495  
1496 =over
1497  
1498 Sets the global Art Box (or page's Art Box, if C<< $page-> >> instead).
1499 This is supposed to define "the extent of the page's
1500 I<meaningful> content (including [margins])". It might exclude some content,
1501 such as Headlines or headings. Any binding or punched-holes margin would
1502 typically be outside of the Art Box, as would be page numbers and running
1503 headers and footers. The default value is equal to the Crop Box, although
1504 normally it would be no larger than any Trim Box. The Art Box may often be
1505 used for defining "important" content (e.g., I<excluding> advertisements) that
1506 may or may not be brought over to another page (e.g., N-up printing).
1507  
1508 If no arguments are given, the current Art Box (global or page) coordinates
1509 are returned instead. The former C<get_artbox> (page only) function was
1510 B<deprecated> and has been removed. If an Art Box
1511 has not been defined, the Crop Box coordinates (if defined) will be returned,
1512 otherwise the Media Box coordinates (which always exist) will be returned.
1513 In addition, when I<setting> the Art Box, the resulting coordinates are
1514 returned. This permits you to specify the art box by a name (alias) and get
1515 the dimensions back, all in one call.
1516  
1517 A global (PDF level) artbox setting is inherited by each page, or can be
1518 overridden by setting artbox in the page.
1519 As with C<mediabox>, only one art box may be set at this (PDF) level.
1520 As with C<mediabox>, a named media size may have an orientation (l or L) for
1521 Landscape mode.
1522 Note that the PDF level global Art Box will be used I<even if> the page gets
1523 its own Crop Box. That is, the page's Art Box inherits the global Art Box,
1524 not the page Crop Box, even if the page has its own media size! If you set the
1525 page's own Media Box or Crop Box, you should consider also explicitly setting
1526 the page Art Box (and other boxes).
1527  
1528 =back
1529  
1530 =head3 Suggested Box Usage
1531  
1532 See C<examples/Boxes.pl> for an example of using boxes.
1533  
1534 How you define your boxes (or let them default) is up to you, depending on
1535 whether you're duplex printing US Letter or A4 on your laser printer, to be
1536 spiral bound on the bind margin, or engaging a professional printer. In the
1537 latter case, discuss in advance with the print firm what capabilities (and
1538 limitations) they have
1539 and what information they need from a PDF file. For instance, they may not want
1540 a Crop Box defined, and may call for very specific box sizes. For large press
1541 runs, they may print multiple pages (N-up) duplexed on large web roll
1542 "signatures", which are then intricately folded and guillotined (trimmed) and
1543 bound together into books or magazines. You would usually just supply a PDF
1544 with all the pages; they would take care of the signature layout (which
1545 includes offsets and 180 degree rotations).
1546  
1547 (As an aside, don't count on a commercial printer having
1548 any particular font available, so be sure to ask. Usually they will want you
1549 to embed all fonts used, but ask first, and double-check before handing over
1550 the print job! TTF/OTF fonts (C<ttfont()>) are embedded by default, but other
1551 fonts (core, ps, bdf, cjk) are not! A printer I<may> have a core font
1552 collection, but they are free to substitute a "workalike" font for any given
1553 core font, and the results may not match what you saw on your PC!)
1554  
1555 On the assumption that you're using a single sheet (US Letter or A4) laser or
1556 inkjet printer, are you planning to trim each sheet down to a smaller final
1557 size? If so, you can do true bleeds by defining a Trim Box and a slightly
1558 larger Bleed Box. You would print bleeds (all the way to the finished edge)
1559 out to the Bleed Box, but nothing is enforced about the Bleed Box. At the other
1560 end of the spectrum, you would define the Media
1561 Box to be the physical paper size being printed on. Most printers reserve a
1562 little space on the sides (and possibly top and bottom) for paper handling, so
1563 it is often good to define your Crop Box as the printable area. Remember that
1564 the Media Box sets the coordinate system used, so you still need to avoid
1565 going outside the Crop Box with content (most readers and printers will not
1566 show any ink outside of the Crop Box). Whether or not you define a Crop Box,
1567 you're going to almost always end up with white paper on at least the sides.
1568  
1569 For small in-house jobs, you probably won't need color alignment dots and other
1570 such professional instructions and information between the Bleed Box and the
1571 Crop Box, but crop marks for trimming (if used) should go just outside the Trim
1572 Box (partly or wholly within the Bleed Box), and
1573 be drawn I<after> all content. If you're I<not> trimming the paper, don't try
1574 to do any bleed effects (including solid background color pages/covers), as
1575 you will usually have a white edge around the sheet anyway (printers leave a
1576 clean, dry route for the feed rollers). Don't count on a PDF document I<never>
1577 being physically printed,
1578 and not just displayed (where you can do things like bleed all the way to the
1579 media edge). Finally, for single sheet printing, an Art Box is
1580 probably unnecessary, but if you're combining pages into N-up prints, or doing
1581 other manipulations, it may be useful.
1582  
1583 =head3 Box Inheritance
1584  
1585 What Media, Crop, Bleed, Trim, and Art Boxes a page gets can be a little
1586 complicated. Note that usually, only the Media and Crop Boxes will have a
1587 clear visual effect. The visual effect of the other boxes (if any) may be
1588 very subtle.
1589  
1590 First, everything is set at the global (PDF) level. The Media Box is always
1591 defined, and defaults to US Letter (8.5 inches wide by 11 inches high). The
1592 global Crop Box inherits the Media Box, unless explicitly defined. The Bleed,
1593 Trim, and Art Boxes inherit the Crop Box, unless explicitly defined. A global
1594 box should only be defined once, as the last one defined is the one that will
1595 be written to the PDF!
1596  
1597 Second, a page inherits the global boxes, for its initial settings. You may
1598 call any of the box set methods (C<cropbox>, C<trimbox>, etc.) to explicitly
1599 set (override) any box for I<this> page. Note that setting a new Media Box for
1600 the page does B<not> reset the page's Crop Box -- it still uses whatever it
1601 inherited from the global Crop Box. You would need to explicitly set the page's
1602 Crop Box if you want a different setting. Likewise, the page's Bleed, Trim, and
1603 Art Boxes will not be reset by a new page Crop Box -- they will still inherit
1604 from the global (PDF) settings.
1605  
1606 Third, the page Media Box (the one actually used for output pages), clips or
1607 limits all the other boxes to extend no larger than its size. For example, if
1608 the Media Box is US Letter, and you set a Crop Box of A4 size, the smaller of
1609 the two heights (11 inches) would be effective, and the smaller of the two
1610 widths (8.26 inches, 595 Points) would be effective.
1611 The I<given> dimensions of a box are returned on query (get), not the
1612 I<effective> dimensions clipped by the Media Box.
1613  
1614 =head2 Outlines (Bookmarks)
1615  
1616 It is possible to create I<outlines> (a.k.a. I<bookmarks>) in a PDF document.
1617 You are not limited to entire pages as targets, but can adjust the destination
1618 C<dest()> to bring up a specific part of a page.
1619  
1620 =head3 Simple Single-Level set of Outline
1621  
1622 Inserts three outlines (at the same level) in a simple list of 12 pages
1623 (055_outlines example):
1624  
1625 my $doc = PDF::Builder-> open($infile);
1626  
1627 $doc-> outlines
1628 -> outline
1629 -> dest( $doc-> openpage( 1 ))
1630 -> title( '1st page (i)' );
1631  
1632 $doc-> outlines
1633 -> outline
1634 -> dest( $doc-> openpage( 4 ))
1635 -> title( '4th page (1)' );
1636  
1637 $doc-> outlines
1638 -> outline
1639 -> dest( $doc-> openpage( 11 ))
1640 -> title( '11th page (7)' );
1641  
1642 C<dest()> by default is a link to an entire page (given as the ordinal page
1643 number, I<not> the document's formatted page number). The C<title()> is what
1644 you see in the Outline or Bookmark section of your PDF Reader.
1645  
1646 =head3 A Multi-level example Outline
1647  
1648 It is also possible to define nested (multiple level) outlines. For the same
1649 set of pages as above, we will add two pages nested under B<page i> and three
1650 pages nested under B<page 1>. Note that it is common practice to make the top
1651 level I<sections> (e.g., I<preface, body, end matter>) and put all the real
1652 pages under them, but you still need to have each section "heading" map to a
1653 real page. Thus I<preface> might point to page B<i>, and have outlines to
1654 B<i, ii,> and B<iii> nested below it.
1655  
1656 my $doc = PDF::Builder-> open($infile);
1657 my $root = # Outlines object (root of whole thing)
1658 $doc-> outlines();
1659  
1660 my $top0 = # Outline object at top level, initially collapsed (closed)
1661 $root -> outline($root)
1662 -> is_open(0)
1663 -> dest( $doc-> openpage( 1 ))
1664 -> title( '1st page (i)' );
1665  
1666 my $top1 = # Outline object at top level
1667 $root -> outline($root)
1668 -> dest( $doc-> openpage( 4 ))
1669 -> title( '4th page (1)' );
1670  
1671 my $top2 = # Outline object at top level. no children
1672 $root -> outline($root)
1673 -> dest( $doc-> openpage( 11 ))
1674 -> title( '11th page (7)' );
1675  
1676 # add lower level bookmarks here. there's no reason that they couldn't be
1677 # mixed in with the higher levels above, provided that they come after
1678 # their parent is defined.
1679  
1680 # two pages under the first top-level page (i)
1681 $top0 -> outline($top0)
1682 -> dest( $doc-> openpage( 2 ))
1683 -> title( '2nd page (ii)' );
1684
1685 $top0 -> outline($top0)
1686 -> dest( $doc-> openpage( 3 ))
1687 -> title( '3rd page (iii)' );
1688  
1689 # three pages under the second top-level page (1). the third is
1690 # inserted after the first and before the second.
1691 my $first =
1692 $top1 -> outline($top1)
1693 -> dest( $doc-> openpage( 5 ))
1694 -> title( '5th page (2)' );
1695
1696 $top1 -> outline($top1)
1697 -> dest( $doc-> openpage( 6 ))
1698 -> title( '6th page (3)' );
1699
1700 $first -> insert_after()
1701 -> dest( $doc-> openpage( 7 ))
1702 -> title( '7th page (4)' );
1703
1704 It is possible to define a single outline (link) as B<closed>, in which case
1705 all the outlines nested under it will be hidden (with a "closed" twistor).
1706 Try to avoid going more than two levels deep (one level of nesting), as the
1707 Outlines/Bookmarks column is usually fairly narrow.
1708  
1709 =head2 FONT METHODS
1710  
1711 =head3 Core Fonts
1712  
1713 These are the "built-in" fonts, in the sense that any PDF Reader is guaranteed
1714 to supply and support them. The I<metrics> for the supported fonts are
1715 shipped with PDF::Builder, but not the fonts themselves.
1716  
1717 Core fonts are limited to B<single byte encodings>. The default encoding for
1718 the core fonts is WinAnsiEncoding (roughly the CP-1252/Windows-1252 superset of
1719 ISO-8859-1/Latin-1). See the C<encode> option below to change this encoding.
1720  
1721 There are some 14 core fonts (regular, I<italic>, B<bold>, and B<I<bold-italic>>
1722 variants) for Times [serif], Helvetica [sans serif], Courier [fixed pitch];
1723 plus two symbol fonts, Symbol and Zapf Dingbats) that are supposed to be
1724 available on any PDF Reader, B<although other fonts with very similar metrics
1725 are often substituted.>
1726  
1727 Windows machines have an additional 14 "core" fonts (15 if you count Bank
1728 Gothic): Georgia [serif], Verdana [sans serif], and Trebuchet [sans serif] in
1729 4 variants each, along with Webdings and Wingdings). These are
1730 I<usually> available on a Windows platform (but not guaranteed!). They are
1731 usually not installed by default on Linux, Mac, and other non-Windows
1732 platforms, so use caution if specifying these fonts.
1733  
1734 PDF::Builder I<does> supply the B<metrics> for the Windows core fonts (as well
1735 as the standard ones), so it should be possible to use PDF::Builder to
1736 generate documents (PDF files) containing these fonts, even if your platform
1737 may not be able to I<display> them. For instance, Font Manager will load the
1738 Windows core fonts on all platforms. It is up to the user to take care in
1739 selecting fonts, if they plan to be able to view documents using those fonts!
1740 On the other hand, the 020_corefonts example will not attempt to create listings
1741 of Windows core fonts unless it is run on a Windows platform.
1742  
1743 =head4 Examples
1744  
1745 $font1 = $pdf->corefont('Times-Roman', encode => 'latin2');
1746 $font2 = $pdf->corefont('Times-Bold');
1747 $font3 = $pdf->corefont('Helvetica');
1748 $font4 = $pdf->corefont('ZapfDingbats');
1749  
1750 Core fonts can also be requested via the C<font()> method
1751  
1752 $font5 = $pdf->font('Courier-Oblique');
1753  
1754 as well as being built into FontManager
1755  
1756 $font6 = $pdf->get_font('face'=>'Times', 'italic'=>1, 'bold'=>0);
1757  
1758 =head4 Notes and Limitations
1759  
1760 =over
1761  
1762 =item *
1763  
1764 You B<cannot> use UTF-8 or other multibyte encodings with core fonts, I<only>
1765 single byte encodings (256 characters maximum). A PDF Reader simply does not
1766 know what to do with a multibyte character, and likely will render it as a
1767 sequence of single characters (producing gibberish). Although most single-byte
1768 encodings, at least for European languages, are supported, it is possible that
1769 you might encounter an encoding that includes a character I<not> found in a
1770 given font file, or vice-versa (the font includes characters that the encoding
1771 does not give you access to).
1772  
1773 =item *
1774  
1775 Do not confuse Unicode character points (such as given with an HTML entity)
1776 with single byte values or multibyte characters. The only way to access a
1777 character defined for a given encoding is with a I<single> byte value in the
1778 range 0 to 255. For example, if you can't directly type a "Euro" symbol,
1779 it is C<\x80> in many encodings -- you would use that instead of the
1780 Unicode C<\x{20AC}> code point or C<x\{E282AC}> UTF-8 byte string. It's a
1781 matter of giving a single byte value that the PDF Reader can look up in its
1782 font definition to get the desired glyph. If the "Euro" symbol is not
1783 found in the encoding you're using, well, you're out of luck.
1784  
1785 =item *
1786  
1787 Be aware of what a given platform (operating system) and editor is using for
1788 its code page when it creates a file with your text to be turned into a PDF!
1789 If you typed a "Euro" but it's, say, a UTF-8 byte string in the file, you
1790 probably won't get a "Euro" in your PDF.
1791  
1792 =item *
1793  
1794 Note that core fonts use fixed lists of expected glyphs, along with metrics
1795 such as their widths. This may not exactly match up with whatever local font
1796 file is used by the PDF Reader. It's usually pretty close, but many cases have
1797 been found where the list of glyphs is different between the core fonts and
1798 various local font files, so be aware of this. There is no guarantee that all
1799 glyphs (code points) found in one single-byte encoding will be found in
1800 another, nor that font metrics are available for all glyphs covered by a given
1801 singe-byte encoding. If you are writing in English, or even in most Western
1802 European languages, this is usually not a problem with core fonts, but for
1803 other languages and alphabets, it might be.
1804  
1805 =item *
1806  
1807 Also be aware that a PDF Reader is free to substitute another font which is
1808 similar (but not necessarily identical) to the requested core font. For example,
1809 Windows machines often substitute I<Arial> for the requested I<Helvetica>. The
1810 metrics (widths) are the same, but the glyphs are a little different.
1811  
1812 =item *
1813  
1814 Core fonts are supposed to be available on all PDF Readers, so they are not
1815 embeddable in the PDF (as TTF fonts are). This is not believed to be a problem
1816 for archival (PDF/A) documents, but may become one at some point, so you should
1817 be aware.
1818  
1819 =back
1820  
1821 See L<PDF::Builder::Resource::Font/font automap> method for information on
1822 accessing more than 256 glyphs in a font, using B<planes>, I<although there is
1823 no guarantee that future changes to font files will permit consistent results>.
1824  
1825 =head4 Should you use TTF instead?
1826  
1827 If you need to reliably access certain characters not found in common
1828 encodings, please consider using TrueType (TTF) or OpenType (OTF) fonts via the
1829 C<ttfont()> method. Note that you will be responsible for specifying the exact
1830 path and full file name of the TTF file, and making sure the font file is
1831 available on the PDF Writer, and possibly on the Reader (if not embedded).
1832  
1833 This would enable you to use UTF-8 text, with extended glyph usability, as well
1834 as permitting the font itself to be embedded in the PDF, ensuring that you get
1835 I<exactly> the glyphs you want, without any substitutions. Kerning and
1836 ligature support (via HarfBuzz::Shaper) may be more available for TTF fonts.
1837 There are tools, such as I<FontForge>, which can do a fairly good
1838 (though, not perfect) job of converting a Type1 font library (if that's what
1839 your core fonts are, internally) to OTF.
1840  
1841 See also L<PDF::Builder::Resource::Font::CoreFont>.
1842  
1843 =head3 PS Fonts
1844  
1845 PostScript fonts are also known as "Type 1" fonts. These are not "built-in"
1846 fonts, and both the PDF Writer and any PDF Reader would need to provide the
1847 desired font files. PostScript fonts used to be very commonly used, but have
1848 fallen out of favor.
1849  
1850 PS (Type 1) fonts are I<not> shipped with PDF::Builder, but are
1851 expected to be found on the machine with the PDF reader. Most PDF readers do
1852 I<not> install PS fonts, and it is up to the user of the PDF reader to install
1853 the needed fonts. Unlike TrueType fonts, PS (T1) fonts are not embedded in the
1854 PDF, and must be supplied on the Reader end.
1855  
1856 PS fonts are limited to B<single byte encodings>. The default encoding for
1857 the PS fonts is WinAnsiEncoding (roughly the CP-1252/Windows-1252 superset of
1858 ISO-8859-1/Latin-1). See the C<encode> option below to change this encoding.
1859  
1860 One characteristic of PS font usage is that I<two> files are used for each
1861 font: a glyph file (C<.pfa> for ASCII format, C<.pfb> for binary format, or
1862 C<.t1> for an extended format), and a metrics file (C<.afm> for an ASCII
1863 format, or C<.pfm> for binary format). A binary glyph file may be used with
1864 an ASCII metrics file, and vice-versa, if desired or needed. The ASCII and
1865 binary files have the same content, just in different formats.
1866  
1867 B<Caution:> the file name given for the glyph file (first argument to C<psfont>)
1868 I<must> have a file extension of .pfa, .pfb, or .t1; as the extension will
1869 be checked to see how to parse the file.
1870  
1871 =head4 WARNING: End of Adobe Support
1872  
1873 B<Adobe has announced an end to support for Type 1 (Postscript/T1) fonts in its
1874 products. The announcement wordings are a bit vague, sometimes referring to
1875 "all products" and other times just to "authoring software". Presumably, Adobe
1876 PDF Readers (as well as Readers supplied by other parties) will continue to
1877 display PDFs with Type 1 fonts for quite some time, although this is by no
1878 means absolutely certain. Note that this does NOT mean that PDF::Builder or
1879 other Third Party authoring tools may not continue to support Type 1 fonts.
1880 This termination by Adobe of support of a now old and obsolete font format does
1881 not affect the use of PDF::Builder for authoring PDFs, nor is it binding on
1882 other non-Adobe readers and authoring tools. However, using Adobe products for
1883 editing of PDFs with Type 1 fonts, and possibly of displaying them, may no
1884 longer be possible. At any rate, users may want to consider starting to move
1885 away from Type 1 font usage and switch to TTF/OTF or even core fonts, although
1886 it is unknown how long Type 1 Reader support will continue.>
1887  
1888 =head4 Examples
1889  
1890 $font1 = $pdf->psfont('Times-Book.pfa', afmfile => 'Times-Book.afm');
1891 $font2 = $pdf->psfont('/fonts/Synest-FB.pfb', pfmfile => '/fonts/Synest-FB.pfm');
1892  
1893 PS fonts can also be requested via the C<font()> method
1894  
1895 $font3 = $pdf->font('/fonts/Times-Book.t1', afmfile => '/fonts/Times-Book.afm');
1896  
1897 as well as being capable of being loaded into FontManager
1898  
1899 $font4 = $pdf->get_font('face'=>'Times-Book', 'italic'=>0, 'bold'=>0);
1900  
1901 =head4 Notes and Limitations
1902  
1903 =over
1904  
1905 =item *
1906  
1907 You B<cannot> use UTF-8 or other multibyte encodings with PS fonts, I<only>
1908 single byte encodings (256 characters maximum). A PDF Reader (or Writer!)
1909 simply does not know what to do with a multibyte character, and likely will
1910 render it as a sequence of single characters (producing gibberish). Although
1911 most single-byte encodings, at least for European languages, are supported, it
1912 is possible that you might encounter an encoding that includes a character
1913 I<not> found in a given font file, or vice-versa (the font includes characters
1914 that the encoding does not give you access to).
1915  
1916 =item *
1917  
1918 Do not confuse Unicode character points (such as given with an HTML entity)
1919 with single byte values or multibyte characters. The only way to access a
1920 character defined for a given encoding is with a I<single> byte value in the
1921 range 0 to 255. For example, if you can't directly type a "Euro" symbol, it is
1922 C<\x80> in many encodings -- you would use that instead of the Unicode
1923 C<\x{20AC}> code point or C<x\{E282AC}> UTF-8 byte string. It's a matter of
1924 giving a single byte value that the PDF Reader can look up in its font
1925 definition to get the desired glyph. If the "Euro" symbol is not found in the
1926 encoding you're using, well, you're out of luck.
1927  
1928 =item *
1929  
1930 Be aware of what a given platform (operating system) and editor is using for
1931 its code page when it creates a file with your text to be turned into a PDF!
1932 If you typed a "Euro" but it's, say, a UTF-8 byte string in the file, you
1933 probably won't get a "Euro" in your PDF.
1934  
1935 =back
1936  
1937 See L<PDF::Builder::Resource::Font/font automap> method for information on
1938 accessing more than 256 glyphs in a font, using B<planes>, I<although there is
1939 no guarantee that future changes to font files will permit consistent results>.
1940  
1941 =head4 Should you use TTF instead?
1942  
1943 If you need to reliably access certain characters not found in common
1944 encodings, please consider using TrueType (TTF) or OpenType (OTF) fonts via the
1945 C<ttfont()> method. Note that you will be responsible for specifying the exact
1946 path and full file name of the TTF file, and making sure the font file is
1947 available on the PDF Writer, and possibly on the Reader (if not embedded).
1948  
1949 This would enable you to use UTF-8 text, with extended glyph usability, as well
1950 as permitting the font itself to be embedded in the PDF, ensuring that you get
1951 I<exactly> the glyphs you want, without any substitutions or failures due to
1952 lack of the desired files on the Reader. Kerning and
1953 ligature support (via HarfBuzz::Shaper) may be more available for TTF fonts.
1954 There are tools, such as I<FontForge>, which can do a fairly good
1955 (though, not perfect) job of converting a Type1 font library to OTF.
1956  
1957 See also L<PDF::Builder::Resource::Font::Postscript>.
1958  
1959 =head3 TrueType Fonts
1960  
1961 TrueType (TTF) fonts and their close cousins, OpenType (OTF) fonts, are widely
1962 used. These are often included with many operating systems, although they are
1963 not "built-in" to PDF, and both the PDF Writer and any PDF Reader (if the font
1964 is B<not> embedded) would need to provide the desired font files.
1965  
1966 TTF and OTF fonts are I<not> shipped with PDF::Builder, but are expected to be
1967 found on the machine with the PDF Writer (and if needed, the Reader). Most PDF
1968 readers do I<not> install TTF/OTF fonts, and it is up to the user of the PDF
1969 reader to install the needed fonts (if they were not embedded). Note that the
1970 default behavior I<is> to embed the font subset (glyphs actually used) into the
1971 PDF file, so that there is no chance of not having the correct font available
1972 on the reader.
1973  
1974 TTF and OTF fonts are B<not> limited to single byte encodings, but can use
1975 multibyte encodings such as UTF-8. The default encoding for these fonts is
1976 WinAnsiEncoding (roughly the CP-1252/Windows-1252 superset of
1977 ISO-8859-1/Latin-1). See the C<encode> option below to change this encoding.
1978  
1979 =head4 Examples
1980  
1981 $font1 = $pdf->ttfont('Times.ttf');
1982 $font2 = $pdf->ttfont('Georgia.otf');
1983  
1984 TTF/OTF fonts can also be requested via the C<font()> method
1985  
1986 $font3 = $pdf->font('/fonts/Sanskrit.ttf');
1987  
1988 as well as being capable of being loaded into FontManager
1989  
1990 $font4 = $pdf->get_font('face'=>'BrushScript', 'italic'=>0, 'bold'=>0);
1991  
1992 =head4 Notes and Limitations
1993  
1994 =over
1995  
1996 =item *
1997  
1998 B<CAUTION:> There is a "gotcha" with TrueType fonts that you need to be aware
1999 of when using them. PDF::Builder outputs to the text stream a list of I<glyph
2000 IDs> as four-digit hex codes, rather than the list of character byte codes
2001 used by other font types. The intent is to allow more than the standard Unicode
2002 points (alternate glyphs for ligatures and other uses). Don't count on it as
2003 encryption to hide your content (the PDF Reader will just display it anyway!),
2004 even though it I<does> make it hard to find specific text in a PDF using a text
2005 editor.
2006  
2007 =item *
2008  
2009 The B<Tw> operator, if used (C<$text-E<gt>wordspace(n)>) to adjust
2010 inter-word spacing, B<will be ignored> by most, if not all, PDF Readers
2011 (including Adobe products). This is because this operator is looking for actual
2012 ASCII spaces (x20 bytes) in the stream, to apply the width change to. Note that
2013 only ASCII spaces are affected (not other spaces), and not at all for TrueType
2014 and OpenType fonts (because they have 4-digit glyph IDs, not x20 bytes)!
2015 PDF::Builder has been updated to attempt to respect the B<Tw> operator when
2016 using TTF/OTF fonts. If the C<Tw> amount is non-zero, it will split up
2017 sentences on ASCII spaces (x20) and individually place words on the page. This
2018 necessarily bloats the PDF file size, but is the only way to adjust word
2019 spacing via the C<wordspace()> method. Note that again, I<only> ASCII spaces
2020 (x20) are affected (to match the behavior of the B<Tw> operator for other font
2021 types), and other spaces (xA0 required/non-breaking space, thin space, etc.)
2022 are not handled.
2023  
2024 =item *
2025  
2026 B<Warning:> BaseEncoding is I<not> set by default for TrueType fonts, so
2027 B<text in the PDF isn't searchable> (by the PDF reader) unless a ToUnicode
2028 CMap is included. A ToUnicode CMap I<is> included by default (unicodemap set
2029 to 1) by PDF::Builder, but allows it to be disabled (for performance and file
2030 size reasons) by setting unicodemap to 0. This will produce non-searchable
2031 text, which, besides being annoying to users, may prevent screen readers and
2032 other aids to disabled users from working correctly!
2033  
2034 =item *
2035  
2036 Do not confuse Unicode character points (such as given with an HTML entity)
2037 with single byte values or multibyte characters. The only way to access a
2038 character defined for a given encoding is with a I<single> code value in the
2039 allowable range. For example, if you can't directly type a "Euro" symbol, it is
2040 C<\x80> in many encodings -- you would use that for a single-byte encoding, or
2041 for UTF-8, the Unicode C<\x{20AC}> code point. You would never give the UTF-8
2042 byte string C<x\{E282AC}>. It needs to be understandable in the context of the
2043 current encoding, so that the 4-digit glyph code can be output.
2044  
2045 =item *
2046  
2047 Be aware of what a given platform (operating system) and editor is using for
2048 its code page when it creates a file with your text to be turned into a PDF!
2049 If you typed a "Euro" but it's, say, a UTF-8 byte string in the file, and you
2050 are using a single-byte encoding, you probably won't get a "Euro" in your PDF.
2051  
2052 =back
2053  
2054 =head4 Where is the font I just added?
2055  
2056 Well, sometimes you get lucky and can
2057 specify the exact directory that the C<.ttf> or C<.otf> file will reside in,
2058 making it easy to specify the path to the font file (for uses such as
2059 C<ttfont()>, C<font()>, or Font Manager calls). Other times, the operating
2060 system will play hide and seek with you, leaving you to expend much time and
2061 energy to track down where the file is. Linux distributions tend to have their
2062 own favorite hiding places for font files, but at least they tend to be
2063 consistent! On the other hand, Windows often decides that it knows better than
2064 you, and will put files in an unexpected place, and under an unexpected name!
2065  
2066 To find out where your TTF or OTF file ended up, if you don't see an obvious
2067 entry in /Windows/Fonts (even if you drag and dropped the font file there),
2068 you need to look in /Users/XXXX/AppData/Local/Microsoft/Windows/Fonts,
2069 depending on what user name you were signed on as when you installed the font.
2070 Even then, you may not be done, as the name may have been changed to something
2071 unrecognizable. You may need to look at Windows' mapping of font name to
2072 filename.
2073  
2074 In the command shell (command line), or whatever equivalent you like to use,
2075 enter "regedit" to bring up the registry editor. For the top level, choose
2076 (click on) either C<HKEY_LOCAL_MACHINE> (for global font settings, in
2077 /Windows/Fonts) or C<HKEY_CURRENT_USER> (for fonts installed by whoever is
2078 currently signed on, in /Users/XXXX/AppData...). From there, both have the same
2079 path: C<SOFTWARE E<gt> Microsoft E<gt> Windows NT E<gt> CurrentVersion E<gt>
2080 Fonts>. This should bring up a listing of all the installed fonts (full name,
2081 e.g. "Papyrus Regular") and their actual filename ("PAPYRUS.TTF"). For
2082 instance, I just installed (drag and drop into /Windows/Fonts) a blackletter
2083 "Gothic" font named I<English Towne Medium>. It ended up in my /Users/XXXX...
2084 directory as C<EnglishTowne.ttf>.
2085  
2086 You don't need to change anything in the registry, just look. You I<do> have
2087 the capability to change things, including hiding/showing the font, if you
2088 care to get into those things.
2089  
2090 See also L<PDF::Builder::Resource::CIDFont::TrueType>
2091  
2092 =head3 CJK Fonts
2093  
2094 B<Examples:>
2095  
2096 $font = $pdf->cjkfont('korean');
2097 $font = $pdf->cjkfont('traditional');
2098  
2099 B<Warning:> Unlike C<ttfont()>, the font file is I<not> embedded in the output
2100 PDF file. This is
2101 evidently behavior left over from the early days of CJK fonts, where the
2102 C<Cmap> and C<Data> were always external files, rather than internal tables.
2103 If you need a CJK-using PDF file to embed the font, for portability, you can
2104 create a PDF using C<cjkfont>, and then use an external utility (e.g.,
2105 C<pdfcairo>) to embed the font in the PDF. It may also be possible to use
2106 C<ttfont()> instead, to produce the PDF, provided you can deduce the correct
2107 font file name from examining the PDF file (e.g., on my Windows system, the
2108 "Ming" font would be C<< $font = $pdf->ttfont("C:/Program Files/Adobe/Acrobat DC/Resource/CIDFont/AdobeMingStd-Light.otf") >>.
2109 Of course, the font file used would have to be C<.ttf> or C<.otf>.
2110 It may act a little differently than C<cjkfont> (due a a different Cmap), but
2111 you I<should> be able to embed the font file into the PDF.
2112  
2113 See also L<PDF::Builder::Resource::CIDFont::CJKFont>
2114  
2115 Due to the lack of ongoing support for CJK fonts, and the apparent "arrested
2116 development" of PDF support for them at an early stage of life, we I<strongly>
2117 recommend that you attempt to directly use TTF or OTF fonts for Far-Eastern
2118 (CJK) text support (via C<ttfont()>) before resorting to C<cjkfont()> usage!
2119 Also, CJK fonts appear to be unusable as input for synthetic fonts, and
2120 normally aren't embedded in the PDF file (requiring the font file to be
2121 installed on the Reader).
2122  
2123 =head3 Synthetic Fonts
2124  
2125 B<Warning:> BaseEncoding is I<not> set by default for these fonts, so text
2126 in the PDF isn't searchable (by the PDF reader) unless a ToUnicode CMap is
2127 included. A ToUnicode CMap I<is> included by default (unicodemap set to 1) by
2128 PDF::Builder, but allows it to be disabled (for performance and file size
2129 reasons) by setting unicodemap to 0. This will produce non-searchable text,
2130 which, besides being annoying to users, may prevent screen
2131 readers and other aids to disabled users from working correctly!
2132  
2133 B<Examples:>
2134  
2135 $cf = $pdf->corefont('Times-Roman', encode => 'latin1');
2136 $sf = $pdf->synfont($cf, condense => 0.85); # compressed 85%
2137 $sfb = $pdf->synfont($cf, bold => 1); # embolden by 10em
2138 $sfi = $pdf->synfont($cf, oblique => -12); # italic at -12 degrees
2139  
2140 Note that I<CJK> fonts (created with the C<cjkfont> method) do B<not> work
2141 properly with C<synfont>. This is due to a different internal structure of the
2142 I<CJK> fonts, as compared to I<corefont>, I<ttfont>, and I<psfont> base fonts.
2143 If you require a synthesized (modified) CJK font, you might try finding the
2144 TTF or OTF original, use C<ttfont> to create the base font, and running
2145 C<synfont> against that, in the manner described for embedding L</CJK Fonts>.
2146  
2147 See also L<PDF::Builder::Resource::Font::SynFont>
2148  
2149 =head2 IMAGE METHODS
2150  
2151 This is additional information on enhanced libraries available for TIFF and
2152 PNG images. See specific information listings for GD, GIF, JPEG, and PNM image
2153 formats. In addition, see C<examples/Content.pl> for an example of placing an
2154 image on a page, as well as using in a "Form".
2155  
2156 =head3 Why is my image flipped or rotated?
2157  
2158 Something not uncommonly seen when using JPEG photos in a PDF is that the
2159 images will be rotated and/or mirrored (flipped). This may happen when using
2160 TIFF images too. What happens is that the camera stores an image just as it
2161 comes off the CCD sensor, regardless of the camera orientation, and does not
2162 rotate it to the correct orientation! It I<does> store a separate
2163 "orientation" flag to suggest how the image might be corrected, but not all
2164 image processing obeys this flag (PDF::Builder does B<not>.). For example, if
2165 you take a "portrait" (tall) photo of a tree (with the phone held vertically),
2166 and then use it in a PDF, the tree may appear to have been cut down! (appears
2167 in landscape mode)
2168  
2169 I have found some code that should allow the C<image_jpeg> or C<image> routine
2170 to auto-rotate to (supposedly) the correct orientation, by looking for the Exif
2171 metadata "Orientation" tag in the file. However, three problems arise:
2172  
2173 =over
2174  
2175 =item 1.
2176  
2177 If a photo has been edited, and rotated or flipped in the process, there is no guarantee that the Orientation tag has been corrected.
2178  
2179 =item 2.
2180  
2181 More than one Orientation tag may exist (e.g., in the binary APP1/Exif header, I<and> in XML data), and they may not agree with each other -- which should be used?
2182  
2183 =item 3.
2184  
2185 The code would need to uncompress the raster data, swap and/or transpose rows and/or columns, and recompress the raster data for inclusion into the PDF. This is costly and error-prone.
2186 In any case, the user would need to be able to override any auto-rotate function.
2187  
2188 =back
2189  
2190 For the time being, PDF::Builder will simply leave it up to the user of the
2191 library to take care of rotating and/or flipping an image which displays
2192 incorrectly. It is possible that we will consider adding some sort of query or warning that the image appears to I<not> be "normally" oriented (Orientation value 1 or "Top-left"), according to the Orientation flag. You can consider either (re-)saving the photo in an editor such as PhotoShop or GIMP, or using PDF::Builder code similar to the following (for images rotated 180 degrees):
2193  
2194 $pW = 612; $pH = 792; # page dimensions (US Letter)
2195 my $img = $pdf->image_jpeg("AliceLake.jpeg");
2196 # raw size WxH 4032x3024, scaled down to 504x378
2197 $sW = 4032/8; $sH = 3024/8;
2198 # intent is to center on US Letter sized page (LL at 54,207)
2199 # Orientation flag on this image is 3 (rotated 180 degrees).
2200 # if naively displayed (just $gfx->image call), it will be upside down
2201  
2202 $gfx->save();
2203
2204 ## method 0: simple display, is rotated 180 degrees!
2205 #$gfx->image($img, ($pW-$sW)/2,($pH-$sH)/2, $sW,$sH);
2206  
2207 ## method 1: translate, then rotate
2208 #$gfx->translate($pW,$pH); # to new origin (media UR corner)
2209 #$gfx->rotate(180); # rotate around new origin
2210 #$gfx->image($img, ($pW-$sW)/2,($pH-$sH)/2, $sW,$sH);
2211 # image's UR corner, not LL
2212  
2213 # method 2: rotate, then translate
2214 $gfx->rotate(180); # rotate around current origin
2215 $gfx->translate(-$sW,-$sH); # translate in rotated coordinates
2216 $gfx->image($img, -($pW-$sW)/2,-($pH-$sH)/2, $sW,$sH);
2217 # image's UR corner, not LL
2218  
2219 ## method 3: flip (mirror) twice
2220 #$scale = 1; # not rescaling here
2221 #$size_page = $pH/$scale;
2222 #$invScale = 1.0/$scale;
2223 #$gfx->add("-$invScale 0 0 -$invScale 0 $size_page cm");
2224 #$gfx->image($img, -($pW-$sW)/2-$sW,($pH-$sH)/2, $sW,$sH);
2225  
2226 $gfx->restore();
2227  
2228 If your image is also mirrored (flipped about an axis), simple rotation will
2229 not suffice. You could do something with a reversal of the coordinate system, as in "method 3" above (see L<PDF::Builder::Content/Advanced Methods>). To mirror only left/right, the second C<$invScale> would be positive; to mirror only top/bottom, the first would be positive. If all else fails, you could save a mirrored copy in a photo editor.
2230 90 or 270 degree rotations will require a C<rotate> call, possibly with "cm" usage to reverse mirroring.
2231 Incidentally, do not confuse this issue with the coordinate flipping performed
2232 by some Chrome browsers when printing a page to PDF.
2233  
2234 Note that TIFF images may have the same rotation/mirroring problems as JPEG,
2235 which is not surprising, as the Exif format was lifted from TIFF for use in
2236 JPEG. The cure will be similar to JPEG's.
2237  
2238 =head3 TIFF Images
2239  
2240 Note that the Graphics::TIFF support library does B<not> currently permit a
2241 filehandle for C<$file>.
2242  
2243 PDF::Builder will use the Graphics::TIFF support library for TIFF functions, if
2244 it is available, unless explicitly told not to. Your code can test whether
2245 Graphics::TIFF is available by examining C<< $tiff->usesLib() >> or
2246 C<< $pdf->LA_GT() >>.
2247  
2248 Note that the first query is only available once the C<$tiff> object has been
2249 created. This may or may not be too late for your purposes.
2250  
2251 =over
2252  
2253 =item = -1
2254  
2255 Graphics::TIFF I<is> installed, but your code has specified C<nouseGT>, to
2256 I<not> use it. The old, pure Perl, code (buggy!) will be used instead, as if
2257 Graphics::TIFF was not installed.
2258  
2259 =item = 0
2260  
2261 Graphics::TIFF is I<not> installed. Not all systems are able to successfully
2262 install this package, as it requires libtiff.a.
2263  
2264 =item = 1
2265  
2266 Graphics::TIFF is installed and is being used.
2267  
2268 =back
2269  
2270 Options:
2271  
2272 =over
2273  
2274 =item nouseGT => 1
2275  
2276 Do B<not> use the Graphics::TIFF library, even if it's available. Normally
2277 you I<would> want to use this library, but there may be cases where you don't,
2278 such as when you want to use a file I<handle> instead of a I<name>.
2279  
2280 =item silent => 1
2281  
2282 Do not give the message that Graphics::TIFF is not B<installed>. This message
2283 will be given only once, but you may want to suppress it, such as during
2284 t-tests.
2285  
2286 =back
2287  
2288 =head3 PNG Images
2289  
2290 PDF::Builder will use the Image::PNG::Libpng support library for PNG functions,
2291 if it is available, unless explicitly told not to. Your code can test whether
2292 Image::PNG::Libpng is available by examining C<< $png->usesLib() >> or
2293 C<< $pdf->LA_IPL() >>.
2294  
2295 Note that the first query is only available once the C<$png> object has been
2296 created. This may or may not be too late for your purposes.
2297  
2298 =over
2299  
2300 =item = -1
2301  
2302 Image::PNG::Libpng I<is> installed, but your code has specified C<nouseIPL>,
2303 to I<not> use it. The old, pure Perl, code (slower and less capable) will be
2304 used instead, as if Image::PNG::Libpng was not installed.
2305  
2306 =item = 0
2307  
2308 Image::PNG::Libpng is I<not> installed. Not all systems are able to successfully
2309 install this package, as it requires libpng.a.
2310  
2311 =item = 1
2312  
2313 Image::PNG::Libpng is installed and is being used.
2314  
2315 =back
2316  
2317 Options:
2318  
2319 =over
2320  
2321 =item nouseIPL => 1
2322  
2323 Do B<not> use the Image::PNG::Libpng library, even if it's available. Normally
2324 you I<would> want to use this library, when available, but there may be cases
2325 where you don't.
2326  
2327 =item silent => 1
2328  
2329 Do not give the message that Image::PNG::Libpng is not B<installed>. This
2330 message will be given only once, but you may want to suppress it, such as
2331 during t-tests.
2332  
2333 =item notrans => 1
2334  
2335 No transparency -- ignore tRNS chunk if provided, ignore Alpha channel if
2336 provided.
2337  
2338 =back
2339  
2340 =head2 USING SHAPER (HarfBuzz::Shaper library)
2341  
2342 # if HarfBuzz::Shaper is not installed, either bail out, or try to
2343 # use regular TTF calls instead
2344 my $rc;
2345 $rc = eval {
2346 require HarfBuzz::Shaper;
2347 1;
2348 };
2349 if (!defined $rc) { $rc = 0; }
2350 if ($rc == 0) {
2351 # bail out in some manner
2352 } else {
2353 # can use Shaper
2354 }
2355  
2356 my $fontfile = '/WINDOWS/Fonts/times.ttf'; # used by both Shaper and textHS
2357 my $fontsize = 15; # used by both Shaper and textHS
2358 my $font = $pdf->ttfont($fontfile);
2359 $text->font($font, $fontsize);
2360
2361 my $hb = HarfBuzz::Shaper->new(); # only need to set up once
2362 my %settings; # for textHS(), not Shaper
2363 $settings{'dump'} = 1; # see the diagnostics
2364 $settings{'script'} = 'Latn';
2365 $settings('dir'} = 'L'; # LTR
2366 $settings{'features'} = (); # required
2367  
2368 # -- set language (override automatic setting)
2369 #$settings{'language'} = 'en';
2370 #$hb->set_language( 'en_US' );
2371 # -- turn OFF ligatures
2372 #push @{ $settings{'features'} }, 'liga';
2373 #$hb->add_features( 'liga' );
2374 # -- turn OFF kerning
2375 #push @{ $settings{'features'} }, 'kern';
2376 #$hb->add_features( 'kern' );
2377 $hb->set_font($fontfile);
2378 $hb->set_size($fontsize);
2379 $hb->set_text("Let's eat waffles in the field for brunch.");
2380 # expect ffl and fi ligatures, and perhaps some kerning
2381  
2382 my $info = $hb->shaper();
2383 $text->textHS($info, \%settings); # strikethru, underline allowed
2384  
2385 The package HarfBuzz::Shaper may be optionally installed in order to use the
2386 text-shaping capabilities of the HarfBuzz library. These include kerning and
2387 ligatures in Western scripts (such as the Latin alphabet). More complex scripts
2388 can be handled, such as Arabic family and Indic scripts, where multiple forms
2389 of a character may be automatically selected, characters may be reordered, and
2390 other modifications made. The examples/HarfBuzz.pl script gives some examples
2391 of what may be done.
2392  
2393 Keep in mind that HarfBuzz works only with TrueType (.ttf) and OpenType (.otf)
2394 font files. It will not work with PostScript (Type1), core, bitmapped, or CJK
2395 fonts. Not all .ttf fonts have the instructions necessary to guide HarfBuzz,
2396 but most proper .otf fonts do. In other words, there are no guarantees that a
2397 particular font file will work with Shaper!
2398  
2399 The basic idea is to break up text into "chunks" which are of the same script
2400 (alphabet), language, direction, font face, font size, and variant (italic,
2401 bold, etc.). These could range from a single character to paragraph-length
2402 strings of text. These are fed to HarfBuzz::Shaper, along with flags, the font
2403 file to be used, and other supporting
2404 information, to create an array of output glyphs. Each element is a hash
2405 describing the glyph to be output, including its name (if available), its glyph
2406 ID (number) in the selected font, its x and y displacement (usually 0), and
2407 its "advance" x and y values, all in points. For horizontal languages (LTR and
2408 RTL), the y advance is normally 0 and the x advance is the font's character
2409 width, less any kerning amount.
2410  
2411 Shaper will attempt to figure out the script used and the text direction, based on the Unicode range; and a reasonable guess at the language used. The language
2412 can be overridden, but currently the script and text direction cannot be
2413 overridden.
2414  
2415 B<An important note:> the number of glyphs (array elements) may not be equal to
2416 the number of Unicode points (characters) given in the chunk's text string!
2417 Sometimes a character will be decomposed into several pieces (multiple glyphs);
2418 sometimes multiple characters may be combined into a single ligature glyph; and
2419 characters may be reordered (especially in Indic and Southeast Asian languages).
2420 As well, for Right-to-Left (bidirectional) scripts such as Hebrew and Arabic
2421 families, the text is output in Left-to-Right order (reversed from the input).
2422  
2423 With due care, a Shaper array can be manipulated in code. The elements are more
2424 or less independent of each other, so elements can be modified, rearranged,
2425 inserted, or deleted. You might adjust the position of a glyph with 'dx' and
2426 'dy' hash elements. The 'ax' value should be left alone, so that the wrong
2427 kerning isn't calculated, but you might need to adjust the "advance x" value by
2428 means of one of the following:
2429  
2430 =over
2431  
2432 =item B<axs>
2433  
2434 is a value to be I<substituted> for 'ax' (points)
2435  
2436 =item B<axsp>
2437  
2438 is a I<substituted> value (I<percentage>) of the original 'ax'
2439  
2440 =item B<axr>
2441  
2442 I<reduces> 'ax' by the value (points). If negative, increase 'ax'
2443  
2444 =item B<axrp>
2445  
2446 I<reduces> 'ax' by the given I<percentage>. Again, negative increases 'ax'
2447  
2448 =back
2449  
2450 B<Caution:> a given character's glyph ID is I<not> necessarily going to be the
2451 same between any two fonts! For example, an ASCII space (U+0020) might be
2452 C<E<lt>0001E<gt>> in one font, and C<E<lt>0003E<gt>> in another font (even one
2453 closely related!). A U+00A0 required blank (non-breaking space) may be output
2454 as a regular ASCII space U+0020. Take care if you need to find a particular
2455 glyph in the array, especially if the number of elements don't match. Consider
2456 making a text string of "marker" characters (space, nbsp, hyphen, soft hyphen,
2457 etc.) and processing it through HarfBuzz::Shaper to get the corresponding
2458 glyph numbers. You may have to count spaces, say, to see where you could break
2459 a glyph array to fit a line.
2460  
2461 The C<advancewidthHS()> method uses the same inputs as does C<textHS()>.
2462 Like C<advancewidth()>, it returns the chunk length in points. Unlike
2463 C<advancewidth()>, you cannot override the glyph array's font, font size, etc.
2464  
2465 Once you have your (possibly modified) array of glyphs, you feed it to the
2466 C<textHS()> method to render it to the page. Remember that this method handles
2467 only a single line of text; it does not do line splitting or fitting -- that
2468 I<you> currently need to do manually. For Western scripts (e.g., Latin), that
2469 might not be too difficult, but for other scripts that involve extensive
2470 modification of the raw characters, it may be quite difficult to split
2471 I<words>, but you still may be able to split at inter-word spaces.
2472  
2473 A useful, but not exhaustive, set of functions are allowed by C<textHS()> use.
2474 Support includes direction setting (top-to-bottom and bottom-to-top directions,
2475 e.g., for Far Eastern languages in traditional orientation), and explicit
2476 script names and language (depending on what support HarfBuzz itself gives).
2477 B<Not yet> supported are features such as discretionary ligatures and manual
2478 selection of glyphs (e.g., swashes and alternate forms).
2479  
2480 Currently, C<textHS()> can only handle a single text string. We are looking at
2481 how fitting to a line length (splitting up an array) could be done, as well as
2482 how words might be split on hard and soft hyphens. At some point, full paragraph
2483 and page shaping could be possible.
2484  
2485 =cut
2486  
2487 0 ▶sub _docs {
Subroutine _docs: not called CC=1 SCAR=0.0
2488 # dummy stub
2489 }
2490  
2491 1 1;
« Builder/Content/Column_docs.pm ↑ Summary Builder/Resource/Colors.pm »