NAME

PDF::Make::Writer - PDF object serializer

SYNOPSIS

use PDF::Make::Writer;
use PDF::Make::Arena;

my $arena = PDF::Make::Arena->new;

# Create some objects
my $name = $arena->name('Type');
my $dict = $arena->dict;
$dict->set('Type', $arena->name('Catalog'));
$dict->set('Pages', $arena->ref(2, 0));

# Serialize them
my $writer = PDF::Make::Writer->new;
$writer->write($dict);
my $bytes = $writer->to_bytes;
# $bytes = "<</Type /Catalog/Pages 2 0 R>>"

# Method chaining
my $output = PDF::Make::Writer->new
    ->write($arena->int(42))
    ->write($arena->name('Test'))
    ->to_bytes;

DESCRIPTION

PDF::Make::Writer serializes pdfmake_obj_t object trees into byte-exact PDF syntax per ISO 32000-1:2008 §7.3.

The serializer is deterministic and produces identical output for identical input. It handles:

  • Scalars: null, boolean, integer, real

  • Strings: literal (with escapes) and hexadecimal

  • Names: with §7.3.5 escape sequences

  • Composites: arrays, dictionaries, streams

  • References: indirect object references (N G R)

METHODS

new

my $writer = PDF::Make::Writer->new;

Create a new writer instance with an empty internal buffer.

write

$writer->write($obj);

Serialize $obj and append to the internal buffer. Returns $self for method chaining.

to_bytes

my $bytes = $writer->to_bytes;

Return the accumulated serialized bytes as a string and reset the internal buffer. After calling to_bytes, the writer is ready for reuse.

len

my $length = $writer->len;

Return the current length of the internal buffer in bytes.

buf

my $ptr = $writer->buf;

Return a pointer to the internal buffer data. For advanced use only.

NUMBER FORMATTING

Numbers are formatted locale-independently per §7.3.3:

  • Integers are printed without decimal point: 42, -123

  • Reals use minimal fractional digits: 1.5, 0.25

  • Integer-valued reals print without decimal: 42 not 42.0

STRING ESCAPING

Literal strings (§7.3.4.2) escape:

\n  newline
\r  carriage return
\t  tab
\b  backspace
\f  form feed
\\  backslash
\(  left parenthesis
\)  right parenthesis

Hexadecimal strings emit uppercase: <DEADBEEF>

NAME ESCAPING

Names (§7.3.5) escape bytes outside the regular set using #XX:

  • NUL (0x00)

  • Whitespace (space, tab, newline, carriage return, form feed)

  • Delimiters: ( ) < [ ] { } / %>

  • The number sign: #

  • Bytes outside 0x21-0x7E

SEE ALSO

PDF::Make::Obj, PDF::Make::Arena, PDF::Make

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.