NAME

PDF::Make::Document - PDF document structure and file emission

SYNOPSIS

use PDF::Make::Document;

my $doc = PDF::Make::Document->new;

# Get the arena for creating objects
my $arena = $doc->arena;

# Create a Pages dictionary
my $pages = $arena->dict;
$pages->set('Type', $arena->name('Pages'));
$pages->set('Kids', $arena->array);
$pages->set('Count', $arena->int(0));
my $pages_num = $doc->add($pages);

# Create a Catalog dictionary
my $catalog = $arena->dict;
$catalog->set('Type', $arena->name('Catalog'));
$catalog->set('Pages', $doc->ref($pages_num));
my $cat_num = $doc->add($catalog);

# Set the document root
$doc->set_root($cat_num);

# Write to bytes or file
my $bytes = $doc->to_bytes;
$doc->to_file('/tmp/test.pdf');

DESCRIPTION

PDF::Make::Document manages a PDF document structure and emits complete PDF files per ISO 32000-2:2020.

A document owns:

  • An arena for object allocation

  • A table of indirect objects

  • Trailer references (Root, Info, ID)

The to_bytes method emits a complete PDF file including:

  • %PDF-2.0 header with binary comment

  • Body of indirect objects (N G obj ... endobj)

  • Classic cross-reference table

  • Trailer dictionary with startxref and %%EOF

METHODS

new

my $doc = PDF::Make::Document->new;

Create a new empty document.

add

my $num = $doc->add($obj);

Add an object to the document as an indirect object. Returns the object number (1-based). The generation number is always 0.

set_root

$doc->set_root($num);

Set the document root (catalog) reference. This is required.

set_info

$doc->set_info($num);

Set the document information dictionary reference. This is optional.

to_bytes

my $bytes = $doc->to_bytes;

Write the complete PDF file to a byte string.

to_file

$doc->to_file($path);

Write the complete PDF file to the specified path.

Metadata Accessors

$doc->title('My Document');
my $title = $doc->title;

Get/set metadata fields. Available: title, author, subject, keywords, creator, producer.

get_meta / set_meta

$doc->set_meta($key, $value);
my $val = $doc->get_meta($key);

Get/set arbitrary metadata fields by key.

add_page

my $page = $doc->add_page;
my $page = $doc->add_page($width, $height);

Add a page to the document. Defaults to US Letter size.

FILE STRUCTURE

The emitted PDF follows §7.5 of ISO 32000-2:2020:

  • Header: %PDF-2.0 followed by binary comment

  • Body: Indirect objects as N G obj ... endobj

  • Cross-reference table: Classic xref format

  • Trailer: Dictionary with /Size, /Root, /Info, /ID

SEE ALSO

PDF::Make, PDF::Make::Writer, PDF::Make::Obj

AUTHOR

LNATION <email@lnation.org>

COPYRIGHT AND LICENSE

Copyright (C) 2024 by LNATION

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