class HexaPDF::Composer


The composer class can be used to create PDF documents from scratch. It uses HexaPDF::Layout::Frame and HexaPDF::Layout::Box objects underneath and binds them together to provide a convenient interface for working with them.


First, a new Composer objects needs to be created, either using ::new or the utility method ::create.

On creation a HexaPDF::Document object is created as well the first page and an accompanying HexaPDF::Layout::Frame object. The frame is used by the various methods for general document layout tasks, like positioning of text, images, and so on. By default, it covers the whole page except the margin area. How the frame gets created can be customized by overriding the create_frame method.

Once the Composer object is created, its methods can be used to draw text, images, … on the page. Behind the scenes HexaPDF::Layout::Box (and subclass) objects are created and drawn on the page via the frame.

If the frame of a page is full and a box doesn't fit anymore, a new page is automatically created. The box is either split into two boxes where one fits on the first page and the other on the new page, or it is drawn completely on the new page. A new page can also be created by calling the new_page method.

The x and y methods provide the point where the next box would be drawn if it fits the available space. This information can be used, for example, for custom drawing operations through canvas which provides direct access to the HexaPDF::Content::Canvas object of the current page.

When using canvas and modifying the graphics state, care has to be taken to avoid problems with later box drawing operations since the graphics state cannot completely be reset (e.g. transformations of the canvas cannot always be undone). So it is best to save the graphics state before and restore it afterwards.


HexaPDF::Composer.create('output.pdf', margin: 36) do |pdf|
  pdf.text("Hello World", valign: :center)



The Content::Canvas of the current page. Can be used to perform arbitrary drawing operations.


The PDF document that is created.


The HexaPDF::Layout::Frame for automatic box placement.


The current page (a HexaPDF::Type::Page object).

Public Class Methods

create(output, **options, &block)

Creates a new PDF document and writes it to output. The options are passed to ::new.


HexaPDF::Composer.create('output.pdf', margin: 36) do |pdf|
new(page_size: :A4, page_orientation: :portrait, margin: 36) { |composer| ... }

Creates a new Composer object and optionally yields it to the given block.


Can be any valid predefined page size (see Type::Page::PAPER_SIZE) or an array [llx, lly, urx, ury] specifying a custom page size.


Specifies the orientation of the page, either :portrait or :landscape. Only used if page_size is one of the predefined page sizes.


The margin to use. See HexaPDF::Layout::Style::Quad#set for possible values.


composer =            # uses the default values :Letter, margin: 72) do |composer|

Public Instance Methods

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

Draws the named box at the current position.

It uses HexaPDF::Document::Layout#box behind the scenes to create the named box. See that method for details on the arguments.

Examples:, image: composer.document.images.add(machu_picchu))

See: HexaPDF::Document::Layout#box

create_stamp(width, height) { |canvas| ... }

Creates a stamp (Form XObject) which can be used like an image multiple times on a single page or on multiple pages.

The width and the height of the stamp need to be set (frame.width/height or might be good choices).


stamp = composer.create_stamp(50, 50) do |canvas|
    rectangle(10, 10, 30, 30).fill_stroke
composer.image(stamp, width: 20, height: 20)
composer.image(stamp, width: 50)


Draws the given HexaPDF::Layout::Box.

The box is drawn into the current frame if possible. If it doesn't fit, the box is split. If it still doesn't fit, a new region of the frame is determined and then the process starts again.

If none or only some parts of the box fit into the current frame, one or more new pages are created for the rest of the box.

formatted_text(data, width: 0, height: 0, style: nil, box_style: nil, **style_properties)

Draws text like text but allows parts of the text to be formatted differently.

It uses HexaPDF::Document::Layout#formatted_text_box behind the scenes to create the HexaPDF::Layout::TextBox that does the actual work. See that method for details on the arguments.


composer.formatted_text(["Some string"])
composer.formatted_text(["Some ", {text: "string", fill_color: 128}])
composer.formatted_text(["Some ", {link: "",
                                   fill_color: 'blue', text: "Example"}])
composer.formatted_text(["Some ", {text: "string", style: {font_size: 20}}])

See: text, HexaPDF::Layout::TextBox, HexaPDF::Layout::TextFragment

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

Draws the given image at the current position.

It uses HexaPDF::Document::Layout#image_box behind the scenes to create the HexaPDF::Layout::ImageBox that does the actual work. See that method for details on the arguments.


composer.image(machu_picchu, border: {width: 3})
composer.image(machu_picchu, height: 30)

See: HexaPDF::Layout::ImageBox

new_page(page_size: nil, page_orientation: nil, margin: nil)

Creates a new page, making it the current one.

If any of page_size, page_orientation or margin are set, they will be used instead of the default values and will become the default values.


composer.new_page  # uses the default values
composer.new_page(page_size: :A5, margin: [72, 36])
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.

See HexaPDF::Document::Layout#style for details; this method is just a thin wrapper around that method.

Example:, font_size: 12, leading: 1.2), font: 'Helvetica', fill_color: "008"), base: :header, font_size: 30)

See: HexaPDF::Layout::Style

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

Draws the given text at the current position into the current frame.

The text will be positioned at the current position if possible. Otherwise the next best position is used. If the text doesn't fit onto the current page or only partially, new pages are created automatically.

This method is of the two main methods for creating text boxes, the other being formatted_text. It uses HexaPDF::Document::Layout#text_box behind the scenes to create the HexaPDF::Layout::TextBox that does the actual work.

See HexaPDF::Document::Layout#text_box for details on the arguments.


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

write(output, optimize: true, **options)

Writes the PDF document to the given output.

See Document#write for details.


The x-position of the cursor inside the current frame.


The y-position of the cursor inside the current frame.