|
NAMEPDFLib - More OO interface to pdflib_pl.pmSYNOPSISuse PDFLib; my $pdf = PDFLib->new("foo.pdf"); DESCRIPTIONA cleaner API than pdflib_pl.pm, which is a very low-level (non-OO) interface.PDFLib APInew(...)Construct a new PDF object. No parameters required.Parameters are passed as name/value pairs (i.e. a hash):
Example: my $pdf = PDFLib->new(creator => "My PDF Program", author => "Me", title => "Business Report"); finishLet PDFLib know you are finished processing this PDF. This method should not normally need to be called, as it is called automatically for you.deleteOnly call this if you are manually calling finish() also. It deletes the used memory for this PDF.get_bufferIf (and only if) you didn't supply a filename in the call to new(), then get_buffer will return to you the PDF as a string. Very useful for generating PDFs on the fly for a web server.filename(...)A getter and setter method for the PDF's filename. Pass in a filename as a string to set a new filename. returns the old filename.info(key => value)A getter and setter method for the PDF info fields (such as Title, Creator, Author, etc). A key is required. If you pass in a value it will set the new value. Returns the old value.papersize(...)A getter and setter for the current paper size. An optional value that can be an array ref of [x, y], or a string from the list of paper sizes below, will set the current paper size. Returns the old/current paper size.orientation(...)A getter and setter for the current page orientation. All this really does is swap the x and y values in the paper size if orientation == "landscape". Returns the current/old orientation.start_page(...)Start a new page. If a page has already been started, this will call end_page() automatically for you.Options are passed in as name/value pairs, and are passed to PDFLib::Page->new() below. end_pageEnd the current page. It should not normally be necessary to call this, however you may need it if you wish to load an image after you have called start_page (images must be loaded when no pages are open)set_font(...)Set the current font being used. The parameters allowed are:
string_width(text => $text)Returns the width of the text in the current font face and size.Alternatively pass in the following options:
set_text_pos(x, y)Sets the current text output position.get_text_posReturns the current text output position as a list (x, y).print($text)Prints the text passed as a parameter to the current page (and creates a new page if there is no current page) at the current output position.Note: this will not wrap, and text can and will fall off the edge of your page. print_at($text, x => $x, y => $y)Prints text at the given X and Y coordinates.print_boxed($text, ...)This is perhaps the most interesting output method as it allows you to define a bounding box to put the text into, and PDFLib will wrap the text for you. The only problem with it is that you cannot change the font while printing into this kind of bounding box. Better to use "new_bounding_box" below.The parameters you can pass are:
Returns zero, or the number of characters from your text that would not fit into the box. print_line($text)Print the text at the current output position, with a carriage return at the end.get_value($key, [$modifier])There are many values that you can retrieve from pdflib, all are covered in the extensive documentation. This method is a wrapper for that.set_value($key => $value)PDFLib also allows you to set values. This method does just that. Note that not all values that you can "get" allow you to also "set" that value. Read the pdflib documentation for more information on values you can set.get_parameter($param, [$modifier])This is very similar to get_value above. No, I don't know why pdflib makes this distinction, before you ask :-)set_parameter($param => $value)Same again. See the pdflib docs for which options are available.new_bounding_box(%params)Creates a new BoundingBox (see below) that you can print into.Example: my $bb = $pdf->new_bounding_box( x => 30, y => 800, w => 300, h => 800 ); $bb->print($long_text); $bb->finish; # MUST call this! Valid parameters are:
load_image(...)Load an image. Parameters available are:
This returns a PDFLib::Image object. add_image(...)Add an image to the current page (or creates a new page if necessary).Options are passed as name/value pairs. Available options are:
add_bookmark(...)Adds a bookmark to the PDF file (normally displayed in a tree view on the left hand side of the pages in Adobe acrobat reader). Takes the following parameters:
add_link(...)Turns a square area of the page into a web link. Takes the following parameters:
set_border_style($style, $width)The border in question here is a border around a link. Style must be one of "solid" or "dashed". Note that links have a border around them by default, so you need to unset that with:$pdf->set_border_style("solid", 0); Unless you want all your links to have ugly boxes around them Note: This will not help broken linux clients like xpdf and gv (or other viewers based on ghostscript) which display a border around links anyway, sadly. Graphics FunctionsDoing graphics in PDFLib is fairly easy, though there are some gotchas to watch out for. For example, using line_to() does not draw the line immediately - to finalize the line you have to call stroke(). This is because PDFLib allows you to instead call fill(), in case you wish to draw some funny shape and fill it in. Here's a quick example of drawing a line:$pdf->start_page; $pdf->move_to(20, 20); $pdf->line_to(400, 400); $pdf->stroke; Which draws a line from the bottom left to the middle of the page. All graphics calls must be accompanied by a call to either one of stroke, close_path_stroke, fill, fill_stroke, close_path_fill_stroke, clip, or end_path. (note this means you can have 1 or 4 or 36 graphics calls, and a single call to one of the above, but beware of the results). set_dash(LIST)Set the current dash pattern. The list should be an alternating black/white length. For example:$pdf->set_dash(3,1,4,2); The dash is reset at the beginning of each new page. set_flat($flatness)Sets the flatness of lines.Flatness describes the maximum distance (in device pixels) between the path and an approximation constructed from straight line segments. (I don't really know what that means, explanations would be welcome!) set_line_joinSets the shape of the corners of paths that are stroked.Pass in a single option, one of: "miter", "round", or "bevel". Think of them like this: miter: \ \ > / / round: \ \ ) / / bevel: \ \ | / / set_line_capSets the shape at the end of a path with respect to stroking.Pass in a single option, one of: "butt_end", "round_end", or "square_end". Think of them like this: butt_end ----| round_end ----) square_end ----] (protrudes line_width/2 beyond end point) set_miter_limit($limit)Sets the miter limit. See the pdflib manual for more informationset_line_width($width)Set the current line width. $width is in units of the current user coordinate system.reset_graphicsResets all the graphics options to their defaults.save_graphics_stateSave the current graphics state. Must be balanced by a restore_graphics_state().save/restore graphics states can be nested, though at the moment it is up to you to ensure they balance on a page. This may be changed for some Perl magic in the future though. restore_graphics_stateRestore the last saved graphics state.move_to($x, $y)Set the current point.line_to($x, $y)Draw a line from the current point to the coordinates specified.bezierDraw a bezier curve from the current point to (x3, y3), using the points (x1, y1) and (x2, y2) as control points. Parameters are passed as a hash:$pdf->bezier( x1 => 20, y1 => 20, x2 => 40, y1 => 0, x3 => 20, y1 => 60, ); circleDraw a circle using parameters x, y and r (radius).arcDraw an arc. Parameters are:
rectDraw a rectangle. Parameters are passed as a hash, and are simply x, y, w, h.close_pathClose the current path.This draws a line from the current point to the starting point of the subpath. strokeDraws the current path as line.You must call this (or one of the path ending functions) or no line will be drawn. close_path_strokeCloses the path and strokes it.fillFills the current path using the currently selected colour.fill_strokeFills the current path and strokes it.close_path_fill_strokeCloses the current path, fills it, and strokes it.clipUses the current path as a clipping region. Often useful in conjunction with save/restore_graphics_state.end_pathEnds the path without doing anything.Changing the Coordinate SystemPDFLib allows you to alter the coordinate system in various ways while you are working. This is affected by save/restore_graphics_state, so it's always useful to wrap these methods around a save/restore block.The most useful thing this allows you to do is draw shapes not listed above, like ellipses, parallelograms, etc. coord_translate ($x, $y)Translate the coordinate system by x, ycoord_scale ($xscale, $yscale)Scale the coordinate system.coord_rotate ($degrees)Rotate the coordinate system the given number of degrees (0 - 360)coord_skew ($xshear, $yshear)Skew (or shear) the coordinate system by the number of degrees given in the X and Y directions.coord_set_matrix (%params)Set the current transformation matrix. This is heavy stuff - use the other functions instead unless you know what you're doing.Params are a hash containing a, b, c, d, e and f entries. coord_concat_matrix (%params)Concatenate to the current transformation matrix.Params are the same as coord_set_matrix. Colour Functionsset_colour/set_colorThis function allows you to set the current colour.The color can be specified in a number of ways, but always as a hash parameter somehow. It is easiest to show using examples: # 40% grayscale. $pdf->set_color(gray => 0.4); $pdf->set_color(rgb => [$r, $g, $b]); $pdf->set_color(cmyk => [$c, $m, $y, $k]); # see make_spot_color below $pdf->set_color(spot => { handle => $h, tint => 0.3 }); # see make_pattern below $pdf->set_color(pattern => $pattern); You can also pass in a type parameter to set either the "stroke" or the "fill" colour, or "both". The default is "both": $pdf->set_color(type => 'stroke', gray => 0.5); make_spot_color/make_spot_colourMakes a named spot colour using the name passed as a parameter.Useful for saving away the current colour to restore it later. begin_patterStarts a pattern. Parameters are passed as a hash of width, height, xstep, ystep, and painttype. See the pdflib manual for more details.end_patternFinishes the current pattern.PDFLib::Page%SizeYou can access the built-in page sizes directly via the %PDFLib::Page::Size hash. For example:my @a4 = @{ $PDFLib::Page::Size{a4} }; Which gives you a width and a height in the array. This can be useful for making sure you don't overstep the bounds of a page when drawing. See "Default Paper Sizes" below for the available sizes. PDFLib::Image APIThe following methods are available on the object returned from "$pdf-"load_image()> above.widthReturn the image's width in points.heightReturn the image's height in points.PDFLib::BoundingBoxBoundingBox is a bounded printing API. You create a bounding box, then print into it, and it wraps and/or ensures you don't print outside of that box.For details of bounding boxes, see "new_bounding_box" above. When you are finished with a bounding box, you must call finish() on it so that it can clean up. Also note that if you create subsequent bounding boxes one after the other, a font change on the bounding box will have effect on the next bounding box - it's transparently passed through to the page level. This is probably desirable (see the output of t/06bounding.t in the distribution for example). print($text)Returns the characters it could not fit into the bounding box, which is useful if you are doing multiple bounding boxes or pages.Default Paper SizesThe following paper sizes are available. Units are in "points". Any of these can be rotated by providing an orientation of "landscape". Alternate paper sizes can be used by passing an array ref of [x, y] to anything requiring a papersize, but that generally shouldn't be necessary.
TODOLots more of the pdflib API needs to be added and tested here. Notably the support for other types of attachments, and support for all of the graphics primatives.AUTHORAxKit.com Ltd,Matt Sergeant, matt@axkit.com LICENSEThis is free software. You may distribute it under the same terms as Perl itself.POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |