PDF::Page
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
Instance methods
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
Draws an elliptical arc. Angles are in degrees, measured counterclockwise from the positive X axis.
Sets the blend mode for compositing.
page.blend_mode(:multiply)
page.blend_mode(PDF::Content::GraphicsState::BlendMode::Screen)
Draws a circle. Uses cubic Bezier curves to approximate the circle.
page.fill_color(:blue)
page.circle(200, 300, 50)
page.fill
Sets the clipping path using the non-zero winding rule. Must be followed by a path painting operator (stroke, fill, or end_path).
Sets the clipping path using the even-odd rule. Must be followed by a path painting operator (stroke, fill, or end_path).
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
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
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
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)
Draws an ellipse. Uses cubic Bezier curves to approximate the ellipse.
page.stroke_color(:red)
page.ellipse(200, 300, 80, 40)
page.stroke
Sets the fill color (CMYK, values 0.0 to 1.0).
page.fill_cmyk(1.0, 0.0, 0.0, 0.0) # Cyan
Sets the fill color (RGB, values 0.0 to 1.0).
Sets the fill color using a named color.
page.fill_color(:blue)
page.fill_color(:orange)
Sets the fill color from a hex string.
page.fill_color("#0000FF") # Blue
page.fill_color("#F0F") # Magenta (short form)
Sets the fill color space.
page.fill_color_space("DeviceCMYK")
Sets the fill color (grayscale, 0.0 = black, 1.0 = white).
page.fill_gray(0.5) # 50% gray
Finalizes the page, creating content stream and registering objects. Called by Document#finalize!
Sets the flatness tolerance. Controls how smooth curves are rendered (0-100, default 0).
page.flatness(1)
Sets the current font for subsequent text operations.
page.font "Helvetica", size: 12
page.font "Helvetica-Bold", size: 18
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}
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
Draws a line from one point to another.
page.line({100, 100}, {200, 200})
page.stroke
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)
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)
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)
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
Draws a polygon from an array of points.
# Triangle
page.polygon([{100, 100}, {150, 200}, {200, 100}])
page.fill_stroke
Sets the rendering intent.
page.rendering_intent(:perceptual)
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
Draws a rounded rectangle.
page.fill_color(:green)
page.rounded_rectangle(100, 100, 200, 100, 10)
page.fill
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
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)
Sets both fill and stroke opacity.
page.set_opacity(fill: 0.5, stroke: 0.8)
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
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
Sets the stroke color (RGB, values 0.0 to 1.0).
Sets the stroke color using a named color.
page.stroke_color(:red)
page.stroke_color(:navy)
Sets the stroke color from a hex string.
page.stroke_color("#FF0000") # Red
page.stroke_color("#0F0") # Green (short form)
Sets the stroke color space.
page.stroke_color_space("DeviceRGB")
Sets the stroke color (grayscale, 0.0 = black, 1.0 = white).
page.stroke_gray(0.5) # 50% gray
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
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}
Converts this page to an indirect object.
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)
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