GitHub
ComponentsCanvas

Canvas

A React component for freely drawing any content on the page.

import { Canvas } from '@react-pdf/renderer';

const Bar = () => (
  <Canvas
    style={{ width: 200, height: 40 }}
    paint={(painter, availableWidth, availableHeight) =>
      painter.rect(0, 0, availableWidth * 0.6, availableHeight).fill('tomato')
    }
  />
);
Show preview

Valid props

Prop nameDescriptionTypeDefault
styleDefines view styles. See moreObject, Arrayundefined
paintPainter functionFunctionundefined
debugEnables debug mode on view bounding box. See moreBooleanfalse
fixedRenders component in all wrapped pages. See moreBooleanfalse
breakForces the wrapping algorithm to start a new page when rendering this element. See moreBooleanfalse
minPresenceAheadHint that no page wrapping should occur between all sibling elements following the element within n points. See moreNumber0
bookmarkAttach bookmark to element. See moreString or Bookmarkundefined

React-pdf does not check how much space your drawing takes, so make sure you always define a width and height on the style prop.

Painter function

Prop used to perform drawings inside the Canvas. It takes 3 arguments:

  • Painter object: Wrapper around pdfkit drawing methods. Use this to draw inside the Canvas
  • availableWidth: Width of the Canvas element.
  • availableHeight: Height of the Canvas element.

Painter object

Wrapper around pdfkit methods you can use to draw inside the Canvas. All operations are chainable. For more information about how these methods work, please refer to pdfkit documentation.

Shapes
  • rect
  • roundedRect
  • circle
  • ellipse
  • polygon
Paths
  • path
  • moveTo
  • lineTo
  • bezierCurveTo
  • quadraticCurveTo
Painting
  • fill
  • stroke
  • clip
Color
  • fillColor
  • strokeColor
  • opacity
  • fillOpacity
  • strokeOpacity
Strokes
  • lineWidth
  • lineCap
  • lineJoin
  • miterLimit
  • dash
Gradients
  • linearGradient
  • radialGradient
Text
  • font
  • fontSize
  • text
Transform
  • translate
  • scale
  • rotate
State
  • save
  • restore

On this page