class

PDF::Page

Inherits Reference < Object

Represents a single page in a PDF document.

Pages contain content streams that describe what to draw, and resources that are used by the content (fonts, images, etc.).

Usage

Pages are created via Document#page:

pdf.page do |page|
  page.font "Helvetica", size: 12
  page.text "Hello!", at: {72, 720}
end

Constructors

new(document : Document, width : Float64 = 612.0, height : Float64 = 792.0)
Source

Instance methods

arc(cx : Number, cy : Number, radius : Number, start_angle : Number, end_angle : Number) : self

Draws an arc (portion of an ellipse). Angles are in degrees, measured counterclockwise from the positive X axis.

page.arc(200, 200, 50, 0, 90)  # Quarter circle
page.stroke
Source
arc_with_radii(cx : Number, cy : Number, rx : Number, ry : Number, start_angle : Number, end_angle : Number) : self

Draws an elliptical arc. Angles are in degrees, measured counterclockwise from the positive X axis.

Source
blend_mode(mode : Content::GraphicsState::BlendMode) : self

Sets the blend mode for compositing.

page.blend_mode(:multiply)
page.blend_mode(PDF::Content::GraphicsState::BlendMode::Screen)
Source
blend_mode(mode : Symbol) : self
Source
circle(cx : Number, cy : Number, radius : Number) : self

Draws a circle. Uses cubic Bezier curves to approximate the circle.

page.fill_color(:blue)
page.circle(200, 300, 50)
page.fill
Source
clip

Sets the clipping path using the non-zero winding rule. Must be followed by a path painting operator (stroke, fill, or end_path).

Source
clip!

Convenience: clips to the current path and ends the path.

Source
clip_even_odd

Sets the clipping path using the even-odd rule. Must be followed by a path painting operator (stroke, fill, or end_path).

Source
clip_even_odd!

Convenience: clips (even-odd) to the current path and ends the path.

Source
close_fill_stroke

Closes the path, fills, and strokes.

Source
close_fill_stroke_even_odd

Closes the path, fills (even-odd), and strokes.

Source
close_path

Closes the current subpath by drawing a line to the starting point.

Source
close_stroke

Closes the current subpath and strokes.

Source
curve_to(x1 : Number, y1 : Number, x2 : Number, y2 : Number, x3 : Number, y3 : Number) : self

Appends a cubic Bezier curve to the path. Uses two control points and an endpoint.

page.move_to(100, 100)
page.curve_to(150, 200, 250, 200, 300, 100)  # Two control points, then endpoint
page.stroke
Source
curve_v(x2 : Number, y2 : Number, x3 : Number, y3 : Number) : self

Appends a cubic Bezier curve using current point as first control point. The first control point is the current point.

page.move_to(100, 100)
page.curve_v(200, 150, 200, 100)  # Second control point, then endpoint
page.stroke
Source
curve_y(x1 : Number, y1 : Number, x3 : Number, y3 : Number) : self

Appends a cubic Bezier curve with endpoint as second control point. The second control point coincides with the endpoint.

page.move_to(100, 100)
page.curve_y(150, 150, 200, 100)  # First control point, then endpoint
page.stroke
Source
dash(array : Array(Number), phase : Number = 0) : self

Sets the dash pattern for stroked lines.

# Solid line (default)
page.dash([])

# Dashed: 5 on, 3 off
page.dash([5, 3])

# Dash-dot: 6 on, 3 off, 1 on, 3 off
page.dash([6, 3, 1, 3])

# With phase offset
page.dash([5, 3], phase: 2)
Source
dash(pattern : Content::GraphicsState::DashPattern) : self
Source
document

Parent document

Source
ellipse(cx : Number, cy : Number, rx : Number, ry : Number) : self

Draws an ellipse. Uses cubic Bezier curves to approximate the ellipse.

page.stroke_color(:red)
page.ellipse(200, 300, 80, 40)
page.stroke
Source
end_path

Ends the path without painting (used for clipping).

Source
fill

Fills the current path.

Source
fill_cmyk(c : Number, m : Number, y : Number, k : Number) : self

Sets the fill color (CMYK, values 0.0 to 1.0).

page.fill_cmyk(1.0, 0.0, 0.0, 0.0)  # Cyan
Source
fill_color(r : Number, g : Number, b : Number) : self

Sets the fill color (RGB, values 0.0 to 1.0).

Source
fill_color(name : Symbol) : self

Sets the fill color using a named color.

page.fill_color(:blue)
page.fill_color(:orange)
Source
fill_color(hex : String) : self

Sets the fill color from a hex string.

page.fill_color("#0000FF")  # Blue
page.fill_color("#F0F")     # Magenta (short form)
Source
fill_color_space(name : String) : self

Sets the fill color space.

page.fill_color_space("DeviceCMYK")
Source
fill_even_odd

Fills the path using the even-odd rule.

Source
fill_gray(gray : Number) : self

Sets the fill color (grayscale, 0.0 = black, 1.0 = white).

page.fill_gray(0.5)  # 50% gray
Source
fill_stroke

Fills and strokes the current path.

Source
fill_stroke_even_odd

Fills (even-odd) and strokes the current path.

Source
finalize!

Finalizes the page, creating content stream and registering objects. Called by Document#finalize!

Source
flatness(value : Number) : self

Sets the flatness tolerance. Controls how smooth curves are rendered (0-100, default 0).

page.flatness(1)
Source
font(name : String, size : Number = 12) : self

Sets the current font for subsequent text operations.

page.font "Helvetica", size: 12
page.font "Helvetica-Bold", size: 18
Source
font(ttf_font : Fonts::TrueTypeFont, size : Number = 12) : self

Sets the current font to a TrueType font.

my_font = pdf.load_font("./fonts/OpenSans-Regular.ttf")
page.font my_font, size: 12
page.text "Hello with custom font!", at: {72, 720}
Source
height
Source
image(img : Images::Base, *, at : Tuple(Number, Number), width : Number | Nil = nil, height : Number | Nil = nil, scale : Number | Nil = nil) : self

Draws an image at the specified position.

Position is in points from the bottom-left corner of the page. You can specify either width, height, or scale to control size. If only width or height is given, aspect ratio is maintained.

image = PDF::Images::Image.load("photo.jpg")
page.image image, at: {72, 500}, width: 200
page.image image, at: {300, 500}, height: 150
page.image image, at: {72, 200}, scale: 0.5
Source
line(from : Tuple(Number, Number), to : Tuple(Number, Number)) : self

Draws a line from one point to another.

page.line({100, 100}, {200, 200})
page.stroke
Source
line_cap(style : Content::GraphicsState::LineCap) : self

Sets the line cap style.

  • :butt - Square end at endpoint (default)
  • :round - Semicircular arc at endpoint
  • :square - Square end extended half line width beyond endpoint
page.line_cap(:round)
page.line_cap(PDF::Content::GraphicsState::LineCap::Round)
Source
line_cap(style : Symbol) : self
Source
line_join(style : Content::GraphicsState::LineJoin) : self

Sets the line join style.

  • :miter - Sharp corner (default)
  • :round - Rounded corner
  • :bevel - Beveled corner
page.line_join(:round)
page.line_join(PDF::Content::GraphicsState::LineJoin::Round)
Source
line_join(style : Symbol) : self
Source
line_to(x : Number, y : Number) : self

Draws a line from the current point to (x, y).

Source
line_width(width : Number) : self

Sets the line width.

Source
miter_limit(limit : Number) : self

Sets the miter limit for line joins. The miter limit controls when mitered joins are converted to bevel joins. Default is 10.0.

page.miter_limit(5.0)
Source
move_to(x : Number, y : Number) : self

Moves the current point to (x, y).

Source
opacity(value : Number) : self

Sets the fill opacity (0.0 = transparent, 1.0 = opaque). This is a convenience method that uses ExtGState.

page.opacity(0.5)  # 50% opacity for fill
page.fill_color(:red)
page.rectangle(100, 100, 100, 100)
page.fill
Source
polygon(points : Array(Tuple(Number, Number))) : self

Draws a polygon from an array of points.

# Triangle
page.polygon([{100, 100}, {150, 200}, {200, 100}])
page.fill_stroke
Source
rectangle(x : Number, y : Number, width : Number, height : Number) : self

Draws a rectangle.

Source
rendering_intent(intent : Content::GraphicsState::RenderingIntent) : self

Sets the rendering intent.

page.rendering_intent(:perceptual)
Source
rendering_intent(intent : Symbol) : self
Source
restore_graphics_state

Restores the graphics state.

Source
rotate(degrees : Number) : self

Rotates the coordinate system by the given angle in degrees. Rotation is counterclockwise.

page.save_graphics_state do
  page.translate(300, 400)
  page.rotate(45)
  page.rectangle(-25, -25, 50, 50)  # Rotated 45 degrees
  page.fill
end
Source
rounded_rectangle(x : Number, y : Number, w : Number, h : Number, radius : Number) : self

Draws a rounded rectangle.

page.fill_color(:green)
page.rounded_rectangle(100, 100, 200, 100, 10)
page.fill
Source
save_graphics_state

Saves the graphics state.

Source
save_graphics_state

Saves the graphics state, yields, then restores.

Source
scale(sx : Number, sy : Number) : self

Scales the coordinate system.

page.save_graphics_state do
  page.scale(2.0, 2.0)  # Double size
  page.rectangle(50, 50, 25, 25)  # Appears as 50x50 at (100, 100)
  page.fill
end
Source
scale(s : Number) : self

Scales uniformly in both directions.

Source
set_graphics_state(ext_g_state : Objects::ExtGState) : self

Sets an extended graphics state. The ExtGState will be registered as a resource and referenced.

gs = PDF::Objects::ExtGState.new
gs.fill_opacity = 0.5
gs.blend_mode = PDF::Content::GraphicsState::BlendMode::Multiply
page.set_graphics_state(gs)
Source
set_opacity(fill : Number | Nil = nil, stroke : Number | Nil = nil) : self

Sets both fill and stroke opacity.

page.set_opacity(fill: 0.5, stroke: 0.8)
Source
skew(ax : Number, ay : Number) : self

Skews the coordinate system. ax is the skew angle in degrees along the X axis. ay is the skew angle in degrees along the Y axis.

page.save_graphics_state do
  page.skew(15, 0)  # Skew along X axis
  page.rectangle(100, 100, 50, 50)
  page.fill
end
Source
solid

Resets to solid line (no dash).

Source
stroke

Strokes the current path.

Source
stroke_cmyk(c : Number, m : Number, y : Number, k : Number) : self

Sets the stroke color (CMYK, values 0.0 to 1.0).

page.stroke_cmyk(0.0, 1.0, 1.0, 0.0)  # Red in CMYK
Source
stroke_color(r : Number, g : Number, b : Number) : self

Sets the stroke color (RGB, values 0.0 to 1.0).

Source
stroke_color(name : Symbol) : self

Sets the stroke color using a named color.

page.stroke_color(:red)
page.stroke_color(:navy)
Source
stroke_color(hex : String) : self

Sets the stroke color from a hex string.

page.stroke_color("#FF0000")  # Red
page.stroke_color("#0F0")     # Green (short form)
Source
stroke_color_space(name : String) : self

Sets the stroke color space.

page.stroke_color_space("DeviceRGB")
Source
stroke_gray(gray : Number) : self

Sets the stroke color (grayscale, 0.0 = black, 1.0 = white).

page.stroke_gray(0.5)  # 50% gray
Source
stroke_opacity(value : Number) : self

Sets the stroke opacity (0.0 = transparent, 1.0 = opaque).

page.stroke_opacity(0.5)  # 50% opacity for stroke
page.stroke_color(:blue)
page.line_width(5)
page.rectangle(100, 100, 100, 100)
page.stroke
Source
text(content : String, *, at : Tuple(Number, Number)) : self

Draws text at the specified position.

Position is in points from the bottom-left corner of the page.

page.text "Hello, World!", at: {72, 720}
Source
to_indirect_object(parent_ref : Objects::Reference) : Objects::Indirect

Converts this page to an indirect object.

Source
transform(a : Number, b : Number, c : Number, d : Number, e : Number, f : Number) : self

Applies a transformation matrix to the current transformation matrix. The transformation matrix is [a b c d e f], representing:

| a  b  0 |
| c  d  0 |
| e  f  1 |
# Translate by (100, 50)
page.transform(1, 0, 0, 1, 100, 50)
Source
translate(tx : Number, ty : Number) : self

Translates the coordinate system by (tx, ty).

page.save_graphics_state do
  page.translate(100, 50)
  page.rectangle(0, 0, 50, 50)  # Draws at (100, 50)
  page.fill
end
Source
width

Page dimensions in points

Source