PDF::Create(3pm)

πŸ“¦
version 1.46 Β· 3 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

okay bestie, come CLOSE 🫢 this is the manual page for PDF::Create(3pm), straight out of the User Contributed Perl Documentation corner of the internet, and it's about to be your new fave. ✨


🌸 NAME 🌸

PDF::Create β€” create PDF files. that's it. that's the whole name. πŸ’…

⭐ VERSION ⭐

Version 1.46. vintage in a chic way. πŸ‘‘

πŸ’– DESCRIPTION πŸ’–

PDF::Create lets you create a PDF document using a whole bunch of primitives. the result comes out as a PDF file or a stream. and for the record, PDF stands for Portable Document Format. πŸ“„βœ¨

Documents can have several pages, a table of content, an information section, and many many other PDF elements. multi-page, organized, information-having β€” iconic behaviour honestly. πŸŽ€

πŸ¦‹ SYNOPSIS πŸ¦‹

okay so here's the pitch: PDF::Create provides an easy module to create PDF output straight from your perl script. it is designed to be easy to use, and simple to install and maintain. the trifecta bestie. πŸ’… it provides a couple of subroutines to handle text, fonts, images and drawing primitives. simple documents? easy peasy with the supplied routines. no cap. πŸ”₯

And the part that really ate: on top of being reasonably simple, PDF::Create is written in pure Perl and has no external dependencies β€” no libraries, no other modules, nothing. it should run on any platform where perl is available. πŸŒˆπŸ¦‹

for the complex stuff, you do need some understanding of the underlying Postscript/PDF format. in that case, it might be better to go with the more complete PDF::API2 modules β€” you gain more features at the expense of a steeper learning curve. 🫠

Example PDF creation with PDF::Create (peep PDF::Create::Page for details of the methods available on a page):

use strict; use warnings;
use PDF::Create;

my $pdf = PDF::Create->new(
    'filename'     => 'sample.pdf',
    'Author'       => 'John Doe',
    'Title'        => 'Sample PDF',
    'CreationDate' => [ localtime ]
);

# Add a A4 sized page
my $root = $pdf->new_page('MediaBox' => $pdf->get_page_size('A4'));

# Add a page which inherits its attributes from $root
my $page1 = $root->new_page;

# Prepare a font
my $font = $pdf->font('BaseFont' => 'Helvetica');

# Prepare a Table of Content
my $toc = $pdf->new_outline('Title' => 'Title Page', 'Destination' => $page1);

# Write some text
$page1->stringc($font, 40, 306, 426, 'PDF::Create');
$page1->stringc($font, 20, 306, 396, "version $PDF::Create::VERSION");
$page1->stringc($font, 20, 306, 300, 'by John Doe <john.doe@example.com>');

# Add another page
my $page2 = $root->new_page;

# Draw some lines
$page2->line(0, 0,   592, 840);
$page2->line(0, 840, 592, 0);

$page2->string($font, 20, 50, 400, "default Γ‘ Γ© Γ­ Γ³ ΓΊ Γ±  Á Γ‰ Í Γ“ Ú Γ‘ ΒΏ Β‘ a e i o u n'");
$page2->string_underline($font, 20, 50, 400, "default Γ‘ Γ© Γ­ Γ³ ΓΊ Γ±  Á Γ‰ Í Γ“ Ú Γ‘ ΒΏ Β‘ a e i o u n'");

use utf8;
$page2->string($font, 20, 50, 350, "use utf8 Γ‘ Γ© Γ­ Γ³ ΓΊ Γ±  Á Γ‰ Í Γ“ Ú Γ‘ ΒΏ Β‘ a e i o u n'");
$page2->string_underline($font, 20, 50, 350, "use utf8 Γ‘ Γ© Γ­ Γ³ ΓΊ Γ±  Á Γ‰ Í Γ“ Ú Γ‘ ΒΏ Β‘ a e i o u n'");

no utf8;
$page2->string($font, 20, 50, 300, "no utf8 Γ‘ Γ© Γ­ Γ³ ΓΊ Γ±  Á Γ‰ Í Γ“ Ú Γ‘ ΒΏ Β‘ a e i o u n'");
$page2->string_underline($font, 20, 50, 300, "no utf8 Γ‘ Γ© Γ­ Γ³ ΓΊ Γ±  Á Γ‰ Í Γ“ Ú Γ‘ ΒΏ Β‘ a e i o u n'");

$toc->new_outline('Title' => 'Second Page', 'Destination' => $page2);

# Close the file and write the PDF
$pdf->close;

πŸŽ€ CONSTRUCTOR πŸŽ€

The method new(%params) creates a new pdf structure for your PDF. it returns an object handle which you can use to add more stuff to the PDF. the parameter keys to the constructor are detailed as below: πŸ‘‡

  • filename β€” the destination file that will contain the resulting PDF, or '-' for stdout. If neither filename or fh are specified, the content will be stored in memory and returned when calling close(). 🧠
  • fh β€” an already opened filehandle that will contain the resulting PDF. See the comment above regarding close().
  • Version β€” the PDF Version to claim. can be 1.0 to 1.3 (default: 1.
  • PageMode β€” how the document should appear when opened. Possible values: UseNone (Default), UseOutlines, UseThumbs and FullScreen. πŸ‘€
  • Author β€” the name of the person who created this document. 🫢
  • Creator β€” if the document was converted into a PDF document from another form, this is the name of the application that created the document.
  • Title β€” the title of the document. ✨
  • Subject β€” the subject of the document.
  • Keywords β€” keywords associated with the document. πŸ”–
  • CreationDate β€” the date the document was created. This is passed as an anonymous array in the same format as localtime returns. πŸ—“οΈ
  • Debug β€” the debug level, defaults to 0. It can be any positive integers. πŸ›

Example:

my $pdf = PDF::Create->new(
    'filename'     => 'sample.pdf',
    'Version'      => 1.2,
    'PageMode'     => 'UseOutlines',
    'Author'       => 'John Doe',
    'Title'        => 'My Title',
    'CreationDate' => [ localtime ]
);

writing a CGI? slay. you can send your PDF on the fly to stdout / directly to the browser using '-' as filename. πŸ’«

CGI Example:

use CGI;
use PDF::Create;

print CGI::header(-type => 'application/x-pdf', -attachment => 'sample.pdf');
my $pdf = PDF::Create->new(
    'filename'     => '-',
    'Author'       => 'John Doe',
    'Title'        => 'My title',
    'CreationDate' => [ localtime ]
);

πŸ‘‘ METHODS πŸ‘‘

✨ new_page(%params) ✨

Add a page to the document using the given parameters. new_page must be called first to initialize a root page, used as model for further pages. it returns a handle to the newly created page. 🌱 parameters can be:

  • Parent β€” the parent of this page in the pages tree. This is page object.
  • Resources β€” resources required by this page.
  • MediaBox β€” rectangle specifying the natural size of the page, for example the dimensions of an A4 sheet of paper. The coordinates are measured in default user space units. It must be the reference of 4 values array. You can use get_page_size to get the size of standard paper sizes. get_page_size knows about A0-A6, A4L (landscape), Letter, Legal, Broadsheet, Ledger, Tabloid, Executive and 36x36. πŸ“
  • CropBox β€” rectangle specifying the default clipping region for the page when displayed or printed. The default is the value of the MediaBox. βœ‚οΈ
  • ArtBox β€” rectangle specifying an area of the page to be used when placing PDF content into another application. The default is the value of the CropBox. [PDF 1.3] 🎨
  • TrimBox β€” rectangle specifying the intended finished size of the page (for example, the dimensions of an A4 sheet of paper). In some cases, the MediaBox will be a larger rectangle, which includes printing instructions, cut marks or other content. The default is the value of the CropBox. [PDF 1.3]. πŸ–¨οΈ
  • BleedBox β€” rectangle specifying the region to which all page content should be clipped if the page is being output in a production environment. In such environments, a bleed area is desired, to accommodate physical limitations of cutting, folding, and trimming equipment. The actual printed page may include printer's marks that fall outside the bleed box. The default is the value of the CropBox. [PDF 1.3]
  • Rotate β€” specifies the number of degrees the page should be rotated clockwise when it is displayed or printed. This value must be zero (the default) or a multiple of 90. The entire page, including contents is rotated. πŸ”„

Example:

…and the new_page example finishes like this, bestie:

my $a4 = $pdf->new_page( 'MediaBox' => $pdf->get_page_size('A4') );

my $page1 = $a4->new_page;
$page1->string($f1, 20, 306, 396, "some text on page 1");

my $page2 = $a4->new_page;
$page2->string($f1, 20, 306, 396, "some text on page 2");

πŸ’… font(%params) πŸ’…

okay so this one preps a font using the arguments you hand it. This font will be added to the document only if it is used at least once before the close method is called. Params are listed below:

  • Subtype β€” type of font. PDF defines some types of fonts. It must be one of the predefined types Type1, Type3, TrueType or Type0. In this version, only Type1 is supported. This is the default value.
  • Encoding β€” specifies the encoding from which the new encoding differs. It must be one of the predefined encodings MacRomanEncoding, MacExpertEncoding or WinAnsiEncoding. In this version, only WinAnsiEncoding is supported. This is the default value.
  • BaseFont β€” the PostScript name of the font. It can be one of the following base fonts: Courier, Courier-Bold, Courier-BoldOblique, Courier-Oblique, Helvetica, Helvetica-Bold, Helvetica-BoldOblique, Helvetica-Oblique, Times-Roman, Times-Bold, Times-Italic, Times-BoldItalic or Symbol.

The ZapfDingbats font is not supported in this version. Default font is Helvetica.

my $f1 = $pdf->font('BaseFont' => 'Helvetica');

🌸 new_outline(%params) 🌸

Adds an outline to the document using the given parameters. Returns the newly created outline. Parameters can be:

  • Title β€” the title of the outline. Mandatory.
  • Destination β€” the destination of this outline item. In this version, it is only possible to give a page as destination. The default destination is the current page.
  • Parent β€” the parent of this outline in the outlines tree. This is an outline object. This way you represent the tree of your outlines.

Example:

my $outline = $pdf->new_outline('Title' => 'Item 1');
$pdf->new_outline('Title' => 'Item 1.1', 'Parent' => $outline);
$pdf->new_outline('Title' => 'Item 1.2', 'Parent' => $outline);
$pdf->new_outline('Title' => 'Item 2');

πŸŽ€ get_page_size($name) πŸŽ€

Returns the size of standard paper used for MediaBox-parameter of new_page. get_page_size has one optional parameter to specify the paper name. Possible values are a0-a6, a4l, letter, broadsheet, ledger, tabloid, legal, executive and 36x36. Default is a4.

my $root = $pdf->new_page( 'MediaBox' => $pdf->get_page_size('A4') );

✨ version($number) ✨

Set and return version number. Valid version numbers are 1.0, 1.1, 1.2 and 1.3.

πŸ’– close(%params) πŸ’–

Close does the work of creating the PDF data from the objects collected before. You must call close() after you have added all the contents as most of the real work building the PDF is performed there. If you omit calling close you get no PDF output. Returns the raw content of the PDF. If fh was provided when creating object of PDF::Create then it does not try to close the file handle. It is, therefore, advised you call flush() rather than close().

🌈 flush() 🌈

Generate the PDF content and returns the raw content as it is.

πŸ¦‹ reserve($name, $type) πŸ¦‹

Reserve the next object number for the given object type.

πŸ’œ add_comment($message) πŸ’œ

Add comment to the document. The string will show up in the PDF as postscript-style comment:

% this is a postscript comment

πŸ”₯ annotation(%params) πŸ”₯

Adds an annotation object, for the time being we only do the 'Link' - 'URI' kind. This is a sensitive area in the PDF document where text annotations are shown or links launched. PDF::Create only supports URI links at this time.

URI links have two components, the text or graphics object and the area where the mouseclick should occur.

For the object to be clicked on you'll use standard text of drawing methods. To define the click-sensitive area and the destination URI.

Example:

# Draw a string and underline it to show it is a link
$pdf->string($f1, 10, 450, 200, 'http://www.cpan.org');

my $line = $pdf->string_underline($f1, 10, 450, 200, 'http://www.cpan.org');

# Create the hot area with the link to open on click
$pdf->annotation(
    Subtype => 'Link',
    URI     => 'http://www.cpan.org',
    x       => 450,
    y       => 200,
    w       => $l,
    h       => 15,
    Border  => [0,0,0]
);

The point (x, y) is the bottom left corner of the rectangle containing hotspot rectangle, (w, h) are the width and height of the hotspot rectangle. The Border describes the thickness of the border surrounding the rectangle hotspot.

The function string_underline returns the width of the string, this can be used directly for the width of the hotspot rectangle.

πŸ–ΌοΈ image($filename) πŸ–ΌοΈ

Prepare an XObject (image) using the given arguments. This image will be added to the document if it is referenced at least once before the close method is called. In this version GIF, interlaced GIF and JPEG is supported. Usage of interlaced GIFs are slower because they are decompressed, modified and compressed again. The gif support is limited to images with a LZW minimum code size of 8. Small images with few colors can have a smaller minimum code size and will not work. If you get errors regarding JPEG compression, then the compression method used in your JPEG file is not supported by PDF::Image::JPEG. Try resaving the JPEG file with different compression options (for example, disable progressive compression).

Example:

my $img = $pdf->image('image.jpg');

$page->image(
    image  => $img,
    xscale => 0.25, # scale image for better quality
    yscale => 0.25,
    xpos   => 50,
    ypos   => 60,
    xalign => 0,
    yalign => 2,
);

πŸ“¦ get_data() πŸ“¦

If you did not ask the $pdf object to write its output to a file, you can pick up the pdf code by calling this method. It returns a big string. You need to call close first.

πŸ’€ LIMITATIONS πŸ’€

PDF::Create comes with a couple of limitations or known caveats:

🧠 PDF Size / Memory 🧠

Unless using a filehandle, PDF::Create assembles the entire PDF in memory. If you create very large documents on a machine with a small amount of memory your program can fail because it runs out of memory. If using a filehandle, data will be written immediately to the filehandle after each method.

⚠️ huge PDF + tiny RAM = memory go bye-bye, bestie. use a filehandle if you can.

πŸŽ€ Small GIF images πŸŽ€

Some gif images get created with a minimal lzw code size of less than 8. PDF::Create can not decode those and they must be converted.

🫢 SUPPORT 🫢

I support PDF::Create in my spare time between work and family, so the amount of work I put in is limited.

If you experience a problem make sure you are at the latest version first many of things have already been fixed.

Please register bug at the CPAN bug tracking system at <http://rt.cpan.org> or send email to bug-PDF-Create [at] rt.cpan.org

Be sure to include the following information:

  • PDF::Create Version you are running
  • Perl version (perl -v)
  • Operating System vendor and version
  • Details about your operating environment that might be related to the issue being described
  • Exact cut and pasted error or warning messages
  • The shortest, clearest code you can manage to write which reproduces the bug described.

I appreciate patches against the latest released version of PDF::Create which fix the bug.

Feature request can be submitted like bugs. If you provide patch for a feature which does not go against the PDF::Create philosophy (keep it simple) then you have a good chance for it to be accepted.

πŸ‘€ SEE ALSO πŸ‘€

  • Adobe PDF <http://www.adobe.com/devnet/pdf/pdf_reference.html>
  • PDF::Labels Routines to produce formatted pages of mailing labels in PDF, uses PDF::Create internally.
  • PDF::Haru Perl interface to Haru Free PDF Library.
  • PDF::EasyPDF PDF creation from a one-file module, similar to PDF::Create.
  • PDF::CreateSimple Yet another PDF creation module
  • PDF::Report A wrapper written for PDF::API2.

πŸ‘‘ AUTHORS πŸ‘‘

  • Fabien Tassin
  • GIF and JPEG-support: Michael Gross (info@mdgrosse.net)
  • Maintenance since 2007: Markus Baertschi (markus@markus.org)
  • Currently maintained by Mohammad S Anwar (MANWAR) "<mohammad.anwar at yahoo.com>"

πŸ“¦ REPOSITORY πŸ“¦

<https://github.com/manwar/pdf-create>

Copyright 1999-2001, Fabien Tassin. All rights reserved. It may be used and modified freely, but I do request that this copyright notice remain attached to the file. You may modify this module as you wish, but if you redistribute a modified version, please attach a note listing the modifications you have made.

Copyright 2007 Markus Baertschi

Copyright 2010 Gary Lieberman

πŸ“œ LICENSE πŸ“œ

This is free software; you can redistribute it and / or modify it under the same terms as Perl 5.6.0.


and that's the whole page, bestie πŸ’– source/version: PDF::Create(3pm), perl v5.38.2, dated 2024-03-07 β€” slay, no cap πŸŽ€βœ¨

πŸ“¦ where this page comes from