class HexaPDF::Document::Layout

Parent

This class provides methods for working with classes in the HexaPDF::Layout module.

Often times the layout related classes are used through HexaPDF::Composer which makes it easy to create documents. However, sometimes one wants to have a bit more control or do something special and use the HexaPDF::Layout classes directly. This is possible but it is better to use those classes through an instance of this class because it makes it more convenient and ties everything together. Incidentally, HexaPDF::Composer relies on this class for a good part of its work.

Boxes

The main focus of the class is on providing convenience methods for creating box objects. The most often used box classes like HexaPDF::Layout::TextBox or HexaPDF::Layout::ImageBox can be created through dedicated methods:

Other, more general boxes don't have their own method but can be created through the general box method. This method uses the 'layout.boxes.map' configuration option.

Additionally, the _box suffix can be omitted, so calling text, formatted_text and image also works. Furthermore, all box names defined in the 'layout.boxes.map' configuration option can be used as method names (with or without a _box suffix) and will invoke box, i.e. column and column_box will also work.

Box Styles

All box creation methods accept HexaPDF::Layout::Style objects or names for style objects (defined via style). This allows one to predefine certain styles (like first level heading, second level heading, paragraph, …) and consistently use them throughout the document creation process.

One style property, HexaPDF::Layout::Style#font, is handled specially:

  • If no font is set on a style, the font “Times” is automatically set because otherwise there would be problems with text drawing operations (font is the only style property that has no valid default value).

  • Standard style objects only allow font wrapper objects to be set via the HexaPDF::Layout::Style#font method. This class makes usage easier by allowing strings or an array [name, options_hash] to be used, like with e.g Content::Canvas#font. So to use Helvetica as font, one could just do:

    style.font = 'Helvetica'
    

    And if Helvetica in its bold variant should be used it would be:

    style.font = ['Helvetica', variant: :bold]
    

Attributes

styles[R]

The mapping of style name (a Symbol) to HexaPDF::Layout::Style instance.

Public Class Methods

new(document)

Creates a new Layout object for the given PDF document.

Public Instance Methods

box(name, width: 0, height: 0, style: nil, **box_options, &block)

Creates the named box and returns it.

The name argument refers to the registered name of the box class that is looked up in the 'layout.boxes.map' configuration option. The box_options are passed as-is to the initialization method of that box class

If a block is provided, a ChildrenCollector is yielded and the collected children are passed to the box initialization method via the :children keyword argument.

See text_box for details on width, height and style (note that there is no style_properties argument).

Example:

layout.box(:column, columns: 2, gap: 15)   # => column_box_instance
layout.box(:column) do |column|            # column box with one child
  column.lorem_ipsum
end
formatted_text_box(data, width: 0, height: 0, style: nil, properties: nil, box_style: nil, **style_properties)

Creates a HexaPDF::Layout::TextBox like text_box but allows parts of the text to be formatted differently.

The argument data needs to be an array of String, HexaPDF::Layout::InlineBox and/or Hash objects and is transformed so that it is suitable as argument for the text box:

  • A String object is treated like {text: data}.

  • A HexaPDF::Layout::InlineBox is used without modification.

  • Hashes can contain any style properties and the following special keys:

    text

    The text to be formatted. If this is set and :box is not, the hash will be transformed into a text fragment.

    link

    A URL that should be linked to. If no text is provided but a link, the link is used for the text. If this is set and :box is not, the hash will be transformed into a text fragment with an appropriate link overlay.

    style

    The style to be use as base style instead of the style created from the style and style_properties arguments. See HexaPDF::Layout::Style::create for allowed values.

    If any style properties are set, the used style is duplicated and the additional properties applied.

    The final style is used for a created text fragment.

    box

    An inline box to be used. If this is set, the hash will be transformed into an inline box.

    The value must be one or more (as an array) positional arguments to be used with the inline_box method. The rest of the hash keys are passed as keyword arguments to inline_box except for :block that value of which would be passed as the block.

See text_box for details on width, height, style, style_properties, properties and box_style.

Examples:

layout.formatted_text_box(["Some string"])
layout.formatted_text_box(["Some ", {text: "string", fill_color: 128}])
layout.formatted_text_box(["Some ", {link: "https://example.com",
                                     fill_color: 'blue', text: "Example"}])
layout.formatted_text_box(["Some ", {text: "string", style: {font_size: 20}}])
layout.formatted_text_box(["Some ", {box: [:text, "string"], valign: :top}])
block = lambda {|list| list.text("First item"); list.text("Second item") }
layout.formatted_text_box(["Some ", {box: :list, item_spacing: 10, block: block}])

See: text_box, HexaPDF::Layout::TextBox, HexaPDF::Layout::TextFragment

image_box(file, width: 0, height: 0, properties: nil, style: nil, **style_properties)

Creates a HexaPDF::Layout::ImageBox for the given image.

The file argument can be anything that is accepted by HexaPDF::Document::Images#add or a HexaPDF::Type::Form object.

See text_box for details on width, height, style, style_properties and properties.

Examples:

layout.image_box(machu_picchu, border: {width: 3})
layout.image_box(machu_picchu, height: 30)

See: HexaPDF::Layout::ImageBox

inline_box(box_or_name, *args, valign: :baseline, **kwargs, &block)

Creates an inline box for use together with text fragments.

The valign argument ist used to specify the vertical alignment of the box within the text line. See HexaPDF::Layout::Line for details.

If a box instance is provided as first argument, it is used. Otherwise the first argument has to be the name of a box creation method and args, kwargs and block are passed to it.

Example:

layout.inline_box(:text, "Hallo")
layout.inline_box(:list) {|list| list.text("Hallo") }
lorem_ipsum_box(sentences: 4, count: 1, **text_box_properties)

Uses text_box to create count paragraphs of lorem ipsum text.

The text_box_properties arguments are passed as is to text_box.

method_missing(name, *args, **kwargs, &block)

Allows creating boxes using more convenient method names:

Calls superclass method
style(name) → style
style(name, base: :base, **properties) → style

Creates or updates the HexaPDF::Layout::Style object called name with the given property values and returns it.

This method allows convenient access to the stored styles and to update them. Such styles can then be used by name in the various box creation methods, e.g. text_box or image_box.

If neither base nor any style properties are specified, the style name is just returned.

If the style name does not exist yet and the argument base specifies the name of another style, that style is duplicated and used as basis for the style. This also means that the referenced base style needs be defined first!

The special name :base should be used for setting the base style which is used when no specific style is set.

Note that the style property 'font' is handled specially, see the class documentation for details.

Example:

layout.style(:base, font_size: 12, leading: 1.2)
layout.style(:header, font: 'Helvetica', fill_color: "008")
layout.style(:header1, base: :header, font_size: 30)

See: HexaPDF::Layout::Style

text_box(text, width: 0, height: 0, style: nil, properties: nil, box_style: nil, **style_properties)

Creates a HexaPDF::Layout::TextBox for the given text.

This method is of the two main methods for creating text boxes, the other being formatted_text_box.

width, height

The arguments width and height are used as constraints and are respected when fitting the box. The default value of 0 means that no constraints are set.

style, style_properties

The box and the text are styled using the given style. This can either be a style name set via style or anything HexaPDF::Layout::Style::create accepts. If any additional style_properties are specified, the style is duplicated and the additional styles are applied.

properties

This can be used to set custom properties on the created text box. See Box#properties for details and usage.

box_style

Sometimes it is necessary for the box to have a different style than the text, e.g. when using overlays. In such a case use box_style for specifiying the style of the box (a style name set via style or anything HexaPDF::Layout::Style::create accepts).

The style together with the style_properties will be used for the text style.

Examples:

layout.text_box("Test " * 15)
layout.text_box("Now " * 7, width: 100)
layout.text_box("Another test", font_size: 15, fill_color: "green")
layout.text_box("Different box style", fill_color: 'white', box_style: {
  underlays: [->(c, b) { c.rectangle(0, 0, b.content_width, b.content_height).fill }]
})

See: formatted_text_box, HexaPDF::Layout::TextBox, HexaPDF::Layout::TextFragment