class HexaPDF::Serializer


Knows how to serialize Ruby objects for a PDF file.

For normal serialization purposes, the serialize or serialize_to_io methods should be used. However, if the type of the object to be serialized is known, a specialized serialization method like serialize_float can be used.

Additionally, an object for encrypting strings and streams while serializing can be set via the encrypter= method. The assigned object has to respond to encrypt_string(str, ind_obj) (where the string is part of the indirect object; returns the encrypted string) and encrypt_stream(stream) (returns a fiber that represents the encrypted stream).

How This Class Works

The main public interface consists of the serialize and serialize_to_io methods which accept an object and return its serialized form. During serialization of this object it is accessible by individual serialization methods via the @object instance variable (useful if the object is a composed object).

Internally, the __serialize method is used for invoking the correct serialization method based on the class of a given object. It is also used for serializing individual parts of a composed object.

Therefore the serializer contains one serialization method for each class it needs to serialize. The naming scheme of these methods is based on the class name: The full class name is converted to lowercase, the namespace separator ‘::’ is replaced with a single underscore and the string “serialize_” is then prepended.


NilClass                 => serialize_nilclass
TrueClass                => serialize_trueclass
HexaPDF::Object          => serialize_hexapdf_object

If no serialization method for a specific class is found, the ancestors classes are tried.

See: PDF2.0 s7.3



The encrypter to use for encrypting strings and streams. If nil, strings and streams are not encrypted.

Default: nil

Public Class Methods


Creates a new Serializer object.

Public Instance Methods


Returns the serialized form of the given object.

For developers: While the object is serialized, methods can use the instance variable @object to obtain information about or use the object in case it is a composed object.


Serializes an Array object.

See: PDF2.0 s7.3.6


Raises an error to provide better failure messages.


Serializes the false value.

See: PDF2.0 s7.3.2


Serializes a Float object.

See: PDF2.0 s7.3.3


Serializes a Hash object (i.e. a PDF dictionary object).

See: PDF2.0 s7.3.7


Serializes an Integer object.

See: PDF2.0 s7.3.3


Serializes the nil value.

See: PDF2.0 s7.3.9


Serializes a Numeric object (either Integer or Float).

This method should be used for cases where it is known that the object is either an Integer or a Float.

See: PDF2.0 s7.3.3


Serializes a String object.

See: PDF2.0 s7.3.4


Serializes a Symbol object (i.e. a PDF name object).

See: PDF2.0 s7.3.5


The ISO PDF specification differs in respect to the supported date format. When converting to a date string, a format suitable for both is output.

See: PDF2.0 s7.9.4, ADB1.7 3.8.3

serialize_to_io(obj, io)

Serializes the given object and writes it to the IO.

Also see: serialize


Serializes the true value.

See: PDF2.0 s7.3.2