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 previewHide preview
Valid props
| Prop name | Description | Type | Default |
|---|---|---|---|
| style | Defines view styles. See more | Object, Array | undefined |
| paint | Painter function | Function | undefined |
| debug | Enables debug mode on view bounding box. See more | Boolean | false |
| fixed | Renders component in all wrapped pages. See more | Boolean | false |
| break | Forces the wrapping algorithm to start a new page when rendering this element. See more | Boolean | false |
| minPresenceAhead | Hint that no page wrapping should occur between all sibling elements following the element within n points. See more | Number | 0 |
| bookmark | Attach bookmark to element. See more | String or Bookmark | undefined |
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 CanvasavailableWidth: 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