1PDF::Builder::Page(3) User Contributed Perl DocumentationPDF::Builder::Page(3)
2
3
4
6 PDF::Builder::Page - Methods to interact with individual pages
7
9 my $pdf = PDF::Builder->new();
10
11 # Add a page to a new or existing PDF
12 my $page = $pdf->page();
13
14 # Set the physical (media) page size
15 # Set prepress page boundaries, a convenience function for those times when
16 # it is not necessary to set other prepress (print-related) page boundaries
17 $page->size('letter'); # by common size name
18 #$page->size([0, 0, 612, 792]); # by points LLx,LLy, URx,URy
19
20 # alternately, can set (or get) prepress page boundaries
21 $page->boundaries('media' => '12x18', 'trim' => 0.5 * 72);
22
23 # Add an image
24 my $image = $pdf->image('/path/to/file.jpg');
25 $page->object($image, $x,$y, $w,$h);
26
27 # Create a content object for text
28 my $text = $page->text();
29
30 # Create a content object for drawing shapes
31 my $canvas = $page->graphics(); # or gfx()
32
33 # Now to draw graphics (using $canvas object) and text (using $text object).
34 # NOTE that everything in the graphics (canvas) object will be laid down on
35 # the page BEFORE anything in the text object is laid down. That is,
36 # text will cover graphics, but not vice-versa. This is simply due to
37 # the order in which the objects were defined.
38
40 $page = PDF::Builder::Page->new($pdf, $parent, $index)
41 Returns a page object (called from $pdf->page()).
42
43 Page Size Methods
44 $page->userunit($value)
45 Sets the User Unit for this one page. See "User Units" in
46 PDF::Builder::Docs for more information.
47
48 $page->mediabox($alias)
49 $page->mediabox($alias, 'orient' => 'orientation')
50 $page->mediabox($w,$h)
51 $page->mediabox($llx,$lly, $urx,$ury)
52 ($llx,$lly, $urx,$ury) = $page->mediabox()
53 Sets or gets the Media Box for this one page. See "Media Box" in
54 PDF::Builder::Docs for more information. The method always returns
55 the current bounds (after any set operation).
56
57 ($llx,$lly, $urx,$ury) = $page->get_mediabox()
58 Gets the Media Box corner coordinates based on best estimates or
59 the default. These are in the order given in a mediabox call (4
60 coordinates).
61
62 This method is Deprecated, and will likely be removed in the
63 future. Use the global ($pdf) or page ($page) mediabox() call with
64 no parameters instead.
65
66 $page->cropbox($alias)
67 $page->cropbox($alias, 'orient' => 'orientation')
68 $page->cropbox($w,$h)
69 $page->cropbox($llx,$lly, $urx,$ury)
70 ($llx,$lly, $urx,$ury) = $page->cropbox()
71 Sets or gets the Crop Box for this one page. See "Crop Box" in
72 PDF::Builder::Docs for more information. The method always returns
73 the current bounds (after any set operation).
74
75 ($llx,$lly, $urx,$ury) = $page->get_cropbox()
76 Gets the Crop Box based on best estimates or the default.
77
78 This method is Deprecated, and will likely be removed in the
79 future. Use the global ($pdf) or page ($page) cropbox() call with
80 no parameters instead.
81
82 $page->bleedbox($alias)
83 $page->bleedbox($alias, 'orient' => 'orientation')
84 $page->bleedbox($w,$h)
85 $page->bleedbox($llx,$lly, $urx,$ury)
86 ($llx,$lly, $urx,$ury) = $page->bleedbox()
87 Sets or gets or gets the Bleed Box for this one page. See "Bleed
88 Box" in PDF::Builder::Docs for more information. The method always
89 returns the current bounds (after any set operation).
90
91 ($llx,$lly, $urx,$ury) = $page->get_bleedbox()
92 Gets the Bleed Box based on best estimates or the default.
93
94 This method is Deprecated, and will likely be removed in the
95 future. Use the global ($pdf) or page ($page) bleedbox() call with
96 no parameters instead.
97
98 $page->trimbox($alias)
99 $page->trimbox($alias, 'orient' => 'orientation')
100 $page->trimbox($w,$h)
101 $page->trimbox($llx,$lly, $urx,$ury)
102 ($llx,$lly, $urx,$ury) = $page->trimbox()
103 Sets or gets the Trim Box for this one page. See "Trim Box" in
104 PDF::Builder::Docs for more information. The method always returns
105 the current bounds (after any set operation).
106
107 ($llx,$lly, $urx,$ury) = $page->get_trimbox()
108 Gets the Trim Box based on best estimates or the default.
109
110 This method is Deprecated, and will likely be removed in the
111 future. Use the global ($pdf) or page ($page) trimbox() call with
112 no parameters instead.
113
114 $page->artbox($alias)
115 $page->artbox($alias, 'orient' => 'orientation')
116 $page->artbox($w,$h)
117 $page->artbox($llx,$lly, $urx,$ury)
118 ($llx,$lly, $urx,$ury) = $page->artbox()
119 Sets or gets the Art Box for this one page. See "Art Box" in
120 PDF::Builder::Docs for more information. The method always returns
121 the current bounds (after any set operation).
122
123 ($llx,$lly, $urx,$ury) = $page->get_artbox()
124 Gets the Art Box based on best estimates or the default.
125
126 This method is Deprecated, and will likely be removed in the
127 future. Use the global ($pdf) or page ($page) artbox() call with no
128 parameters instead.
129
130 $page->rotate($deg)
131 Rotates the page by the given degrees, which must be a multiple of
132 90. An angle that is not a multiple of 90 will be rounded to the
133 nearest 90 degrees, with a message.
134
135 Note that the rotation angle is clockwise for a positive amount!
136 E.g., a rotation of +90 (or -270) will have the bottom edge of the
137 paper at the left of the screen. After rotating the page 180
138 degrees, "[0, 0]" (originally lower left corner) will be be in the
139 top right corner of the page, rather than the bottom left. X will
140 increase to the right, and Y will increase downward.
141
142 (This allows you to auto-rotate to landscape without changing the
143 mediabox! There are other ways to accomplish this end, such as
144 using the "size()" method, which will not change the coordinate
145 system (move the origin).)
146
147 Do not confuse this "rotate()" call with the graphics context
148 rotation (Content.pm) "rotate()", which permits any angle, is of
149 opposite direction, and does not shift the origin!
150
151 Alternate name: "rotation"
152
153 This has been added for PDF::API2 compatibility.
154
155 $page->size($size) # Set
156 @rectangle = $page->size() # Get
157 Set the physical page size or return the coordinates of the
158 rectangle enclosing the physical page size. This is an alternate
159 method provided for compatibility with PDF::API2.
160
161 # Set the physical page (media) size using a common size name
162 $page->size('letter');
163
164 # Set the page size using coordinates in points (X1, Y1, X2, Y2)
165 $page->size([0, 0, 612, 792]);
166
167 # Get the page coordinates in points
168 my @rectangle = $page->size();
169
170 See Page Sizes below for possible values. The size method is a
171 convenient shortcut for setting the PDF's media box when other
172 prepress (print-related) page boundaries aren't required. It's
173 equivalent to the following:
174
175 # Set
176 $page = $page->boundaries('media' => $size);
177
178 # Get
179 @rectangle = $page->boundaries()->{'media'}->@*;
180
181 $page = $page->boundaries(%boundaries)
182 \%boundaries = $page->boundaries()
183 Set prepress page boundaries to facilitate printing. Returns the
184 current page boundaries if called without arguments. This is an
185 alternate method provided for compatibility with PDF::API2.
186
187 # Set
188 $page->boundaries(
189 # 13x19 inch physical sheet size
190 'media' => '13x19',
191 # sheet content is 11x17 with 0.25" bleed
192 'bleed' => [0.75 * 72, 0.75 * 72, 12.25 * 72, 18.25 * 72],
193 # 11x17 final trimmed size
194 'trim' => 0.25 * 72,
195 );
196
197 # Get
198 %boundaries = $page->boundaries();
199 ($x1,$y1, $x2,$y2) = $page->boundaries('trim');
200
201 The %boundaries hash contains one or more page boundary keys (see
202 Page Boundaries) to set or replace, each with a corresponding size
203 (see Page Sizes).
204
205 If called without arguments, the returned hashref will contain
206 (Get) all five boundaries. If called with one string argument, it
207 returns the coordinates for the specified page boundary. If more
208 than one boundary type is given, only the first is processed, and a
209 warning is given that the remainder are ignored.
210
211 Page Boundaries
212
213 PDF defines five page boundaries. When creating PDFs for print shops,
214 you'll most commonly use just the media box and trim box. Traditional
215 print shops may also use the bleed box when adding printer's marks and
216 other information.
217
218 media
219 The media box defines the boundaries of the physical medium on
220 which the page is to be printed. It may include any extended area
221 surrounding the finished page for bleed, printing marks, or other
222 such purposes. The default value is as defined for PDF, a US letter
223 page (8.5" x 11").
224
225 crop
226 The crop box defines the region to which the contents of the page
227 shall be clipped (cropped) when displayed or printed. The default
228 value is the page's media box. This is a historical page boundary.
229 You'll likely want to set the bleed and/or trim boxes instead.
230
231 bleed
232 The bleed box defines the region to which the contents of the page
233 shall be clipped when output in a production environment. This may
234 include any extra bleed area needed to accommodate the physical
235 limitations of cutting, folding, and trimming equipment. The actual
236 printed page (media box) may include printing marks that fall
237 outside the bleed box. The default value is the page's crop box.
238
239 trim
240 The trim box defines the intended dimensions of the finished page
241 after trimming. It may be smaller than the media box to allow for
242 production-related content, such as printing instructions, cut
243 marks, or color bars. The default value is the page's crop box.
244
245 art The art box defines the extent of the page's meaningful content
246 (including potential white space) as intended by the page's
247 creator. The default value is the page's crop box.
248
249 Page Sizes
250
251 PDF page sizes are stored as rectangular coordinates. For convenience,
252 PDF::Builder also supports a number of aliases and shortcuts that are
253 more human-friendly. The following formats are available:
254
255 a standard paper size
256 $page->boundaries('media' => 'A4');
257
258 Aliases for the most common paper sizes are built in (case-
259 insensitive). US: Letter, Legal, Ledger, Tabloid (and others)
260 Metric: 4A0, 2A0, A0 - A6, 4B0, 2B0, and B0 - B6 (and others)
261
262 a "WxH" string in inches
263 $page->boundaries('media' => '8.5x11');
264
265 Many US paper sizes are commonly identified by their size in inches
266 rather than by a particular name. These can be passed as strings
267 with the width and height separated by an "x". Examples: "4x6",
268 "12x18", "8.5x11"
269
270 a number representing a reduction (in points) from the next-larger box
271 For example, a 12" x 18" physical sheet to be trimmed down to an
272 11" x 17" sheet can be specified as follows:
273
274 # Note: There are 72 points per inch
275 $page->boundaries('media' => '12x18', 'trim' => 0.5 * 72);
276
277 # Equivalent
278 $page->boundaries('media' => [0, 0, 12 * 72, 18 * 72],
279 'trim' => [0.5 * 72, 0.5 * 72, 11.5 * 72, 17.5 * 72]);
280
281 This example shows a 12" x 18" physical sheet that will be reduced
282 to a final size of 11" x 17" by trimming 0.5" from each edge. The
283 smaller page boundary is assumed to be centered within the larger
284 one.
285
286 The "next-larger box" follows this order, stopping at the first
287 defined value:
288
289 art -> trim -> bleed -> media
290 crop -> media
291
292 This option isn't available for the media box, since it is by
293 definition, the largest boundary.
294
295 [$width, $height] in points
296 $page->boundaries('media' => [8.5 * 72, 11 * 7.2]);
297
298 For other page or boundary sizes, the width and height (in points)
299 can be given directly as an array.
300
301 [$x1, $y1, $x2, $y2] in points
302 $page->boundaries('media' => [0, 0, 8.5 * 72, 11 * 72]);
303
304 Finally, the absolute (raw) coordinates of the bottom-left and top-
305 right corners of a rectangle can be specified.
306
307 $gfx = $page->gfx(%opts)
308 $gfx = $page->gfx($prepend)
309 $gfx = $page->gfx()
310 Returns a graphics content object, for drawing paths and shapes.
311
312 You may specify the "prepend" flag in the old or new way. The old
313 way is to give a single boolean value (0 false, non-zero true). The
314 new way is to give a hash element named 'prepend', with the same
315 values.
316
317 gfx(boolean_value $prepend)
318 gfx('prepend' => boolean_value)
319
320 If $prepend is true, or the option 'prepend' is given with a true
321 value, the content will be prepended to the page description (at
322 the beginning of the page's content stream). Otherwise, it will be
323 appended. The default is false.
324
325 gfx('compress' => boolean_value)
326
327 You may specify a compression flag saying whether the drawing
328 instructions are to be compressed. If not given, the default is for
329 the overall PDF compression setting to be used (on by default).
330
331 You may have more than one gfx object. They and text objects will
332 be output as objects and streams in the order defined, with all
333 actions pertaining to this gfx object appearing in one stream.
334 However, note that graphics and text objects are not fully
335 independent of each other: the exit state (linewidth, strokecolor,
336 etc.) of one object is the entry state of the next object in line
337 to be output, and so on.
338
339 If you intermix multiple gfx and text objects on a page, the
340 results may be confusing. Say you have $gfx1, $text1, $gfx2, and
341 $text2 on your page (created in that order). PDF::Builder will
342 output all the $gfx1->action calls in one stream, then all the
343 $text1->action calls in the next stream, and likewise for $gfx2
344 usage and finally $text2.
345
346 Then it's PDF's turn to confuse you. PDF will process the entire
347 $gfx1 object stream, accumulating the graphics state to the end of
348 the stream, and using that as the entry state into $text1. In a
349 similar manner, $gfx2 and $text2 are read, processed, and rendered.
350 Thus, a change in, say, the dash pattern in the middle of $gfx1,
351 after you have output some $gfx2, $text1, and $text2 material, may
352 suddenly show up at the beginning of $text1 (and continue through
353 $gfx2 and $text2)!
354
355 It is possible to use multiple graphics objects, to avoid having to
356 change settings constantly, but you may want to consider resetting
357 all your settings at the first call to each object, so that you are
358 starting from a known base. This may most easily be done by using
359 $type->restore() and ->save() just after creating $type:
360
361 $text1 = $page->text();
362 $text1->save();
363 $grfx1 = $page->gfx();
364 $grfx1->restore();
365 $grfx1->save();
366 $text2 = $page->text();
367 $text2->restore();
368 $text2->save();
369 $grfx2 = $page->gfx();
370 $grfx1->restore();
371
372 Alternate name: "graphics"
373
374 This has been added for PDF::API2 compatibility.
375
376 $text = $page->text(%opts)
377 $text = $page->text($prepend)
378 $text = $page->text()
379 Returns a text content object, for writing text. See
380 PDF::Builder::Content for details.
381
382 You may specify the "prepend" flag in the old or new way. The old
383 way is to give a single boolean value (0 false, non-zero true). The
384 new way is to give a hash element named 'prepend', with the same
385 values.
386
387 text(boolean_value $prepend)
388 text('prepend' => boolean_value)
389
390 If $prepend is true, or the option 'prepend' is given with a true
391 value, the content will be prepended to the page description (at
392 the beginning of the page's content stream). Otherwise, it will be
393 appended. The default is false.
394
395 text('compress' => boolean_value)
396
397 You may specify a compression flag saying whether the text content
398 is to be compressed. If not given, the default is for the overall
399 PDF compression setting to be used (on by default).
400
401 Please see the discussion above in "gfx()" regarding multiple
402 graphics and text objects on one page, how they are grouped into
403 PDF objects and streams, and the rendering consequences of running
404 through one entire object at a time, before moving on to the next.
405
406 The text object has many settings and attributes of its own, but
407 shares many with graphics (gfx), such as strokecolor, fillcolor,
408 linewidth, linedash, and the like. Thus there is some overlap in
409 attributes, and graphics and text calls can affect each other.
410
411 $page = $page->object($object, $x,$y, $scale_x,$scale_y)
412 Places an image or other external object (a.k.a. XObject) on the
413 page in the specified location.
414
415 For images, $scale_x and $scale_y represent the width and height of
416 the image on the page in points. If $scale_x is omitted, it will
417 default to 72 pixels per inch. If $scale_y is omitted, the image
418 will be scaled proportionally based on the image dimensions.
419
420 For other external objects, the scale is a multiplier, where 1 (the
421 default) represents 100% (i.e. no change).
422
423 If the object to be placed depends on a coordinate transformation
424 (e.g. rotation or skew), first create a content object using
425 "graphics", then call "object" in PDF::Builder::Content after
426 making the appropriate transformations.
427
428 $ant = $page->annotation()
429 Returns a new annotation object.
430
431 $page->resource($type, $key, $obj)
432 Adds a resource to the page-inheritance tree.
433
434 Example:
435
436 $co->resource('Font', $fontkey, $fontobj);
437 $co->resource('XObject', $imagekey, $imageobj);
438 $co->resource('Shading', $shadekey, $shadeobj);
439 $co->resource('ColorSpace', $spacekey, $speceobj);
440
441 Note: You only have to add the required resources if they are NOT
442 handled by the *font*, *image*, *shade* or *space* methods.
443
444
445
446perl v5.36.0 2022-09-13 PDF::Builder::Page(3)