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 classs 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::ImagebBox 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.

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:

doc.layout.box(:column, columns: 2, gap: 15)   # => column_box_instance
doc.layout.box(:column) do |column|            # column box with one child
  column.lorem_ipsum
end
formatted_text_box(data, width: 0, height: 0, style: 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 and/or Hash objects:

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

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

    text

    The text to be formatted.

    link

    A URL that should be linked to. If no text is provided but a link, the link is used as text.

    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.

See text_box for details on width, height, style, style_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}}])

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

image_box(file, width: 0, height: 0, 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 and style_properties.

Examples:

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

See: HexaPDF::Layout::ImageBox

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, 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.

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("Test " * 15)
layout.text("Now " * 7, width: 100)
layout.text("Another test", font_size: 15, fill_color: "green")
layout.text("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