React-pdf

Components

React-pdf follows the React primitives specification, making the learning process very straightforward if you come from another React environment (such as react-native). Additionally, it implements custom Component types that allow you to structure your PDF document.

Document

This component represents the PDF document itself. It must be the root of your tree element structure, and under no circumstances should it be used as child of another react-pdf component. In addition, it should only have children of type <Page />.

Valid props

Prop nameDescriptionTypeDefault
titleSets title info on the document's metadataStringundefined
authorSets author info on the document's metadataStringundefined
subjectSets subject info on the document's metadataStringundefined
keywordsSets keywords associated info on the document's metadataStringundefined
creatorSets creator info on the document's metadataString"react-pdf"
producerSets producer info on the document's metadataString"react-pdf"
onRenderCallback after document renders. Receives document blob argument in webFunctionundefined

Page

Represents single page inside the PDF documents, or a subset of them if using the wrapping feature. A <Document /> can contain as many pages as you want, but ensures not rendering a page inside any component besides Document.

Valid props

Prop nameDescriptionTypeDefault
sizeDefines page size. If String, must be one of the available page sizes. Height is optional, if ommited it will behave as "auto".String, Array, Number, Object"A4"
orientationDefines page orientation. Valid values: "portrait" or "landscape"String"portrait"
wrapEnables page wrapping for this page. See moreBooleantrue
styleDefines page styles. See moreObject, Arrayundefined
debugEnables debug mode on page bounding box. See moreBooleanfalse
rulerEnables vertical and horizontal rulers on page. See moreBooleanfalse
rulerStepsGrid separation for horizontal and vertical rulers See moreInteger Float String50
verticalRulerEnables vertical ruler on page. See moreBooleanfalse
verticalRulerStepsGrid separation for vertical ruler See moreInteger Float String50
horizontalRulerEnables horizontal ruler on page. See moreBooleanfalse
horizontalRulerStepsGrid separation for horizontal rulers See moreInteger Float String50

View

The most fundamental component for building a UI and is designed to be nested inside other views and can have 0 to many children.

Valid props

Prop nameDescriptionTypeDefault
wrapEnable/disable page wrapping for element See moreBooleantrue
styleDefines view styles. See moreObject, Arrayundefined
renderRender dynamic content based on context See moreFunctionundefined
debugEnables debug mode on view bounding box. See moreBooleanfalse
fixedRender component in all wrapped pages. See moreBooleanfalse

Image

A React component for displaying network or local (Node only) JPG or PNG images, as well as base64 encoded image strings.

Valid props

Prop nameDescriptionTypeDefault
srcSource of the image See Source objectSource objectundefined
sourceAlias of src See Source objectSource objectundefined
styleDefines view styles. See moreObject, Arrayundefined
debugEnables debug mode on view bounding box. See moreBooleanfalse
fixedRenders component in all wrapped pages. See moreBooleanfalse
cacheEnables image caching between consecutive rendersBooleantrue
safePathThe safe path from which local images (Node only) can be renderedString./public
allowDangerousPaths Node onlyAllows local images to be retrieved from paths outside the safePath. Use this at your own risk!Booleanfalse
Source object

Defines the source of an image. Can be in any of these four valid froms:

Form typeDescriptionExample
StringValid image URL or filesystem path (Node only)www.react-pdf.org/test.jpg
URL objectEnables to pass extra parameters on how to fetch images{ uri: valid-url, method: 'GET', headers: {}, body: '' }
Data bufferRenders buffer image via the data key. It's also recommended to provide the image format so the engine knows how to proccess it{ data: Buffer, format: 'png' \| 'jpg' }
FunctionA function that returns (can also return a promise that resolves to) any of the above formats() => String \| Promise<String>

Text

A React component for displaying text. Text supports nesting of other Text or Link components to create inline styling.

Valid props

Prop nameDescriptionTypeDefault
wrapEnables/disables page wrapping for element See moreBooleantrue
renderRenders dynamic content based on context See moreFunctionundefined
styleDefines view styles. See moreObject, Arrayundefined
debugEnables debug mode on view bounding box. See moreBooleanfalse
fixedRenders component in all wrapped pages. See moreBooleanfalse
hyphenationCallbackSpecifies how much hyphenated breaks should be avoidedInteger600

A React component for displaying an hyperlink. Link’s can be nested inside a Text component, or being inside any other valid primitive.

Valid props

Prop nameDescriptionTypeDefault
srcValid URLStringundefined
wrapEnable/disable page wrapping for element See moreBooleantrue
styleDefines view styles. See moreObject, Arrayundefined
debugEnables debug mode on view bounding box. See moreBooleanfalse
fixedRender component in all wrapped pages. See moreBooleanfalse

Note

A React component for displaying a note annotation inside the document.

Valid props

Prop nameDescriptionTypeDefault
styleDefines view styles. See moreObject, Arrayundefined
childrenNote string contentStringundefined
fixedRenders component in all wrapped pages. See moreBooleanfalse

Canvas

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

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

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 arround 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 arround 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.

Available methods:

  • dash
  • clip
  • save
  • path
  • fill
  • font
  • text
  • rect
  • scale
  • moveTo
  • lineTo
  • stroke
  • rotate
  • circle
  • lineCap
  • opacity
  • ellipse
  • polygon
  • restore
  • lineJoin
  • fontSize
  • fillColor
  • lineWidth
  • translate
  • miterLimit
  • strokeColor
  • fillOpacity
  • roundedRect
  • strokeOpacity
  • bezierCurveTo
  • quadraticCurveTo
  • linearGradient
  • radialGradient

PDFViewer Web only

Iframe PDF viewer for client-side generated documents.

Valid props

Prop nameDescriptionTypeDefault
styleDefines iframe stylesObject, Arrayundefined
classNameDefines iframe class nameString undefined
childrenPDF document implementationDocumentundefined
widthWidth of embedded PDF iframeString, Numberundefined
heightHeight of embedded PDF iframeString, Numberundefined

Other props are passed through to the iframe.


Anchor tag to enable generate and download PDF documents on the fly. Refer to on the fly rendering for more information.

Valid props

Prop nameDescriptionTypeDefault
documentPDF document implementationDocumentundefined
fileNameDownload PDF file nameStringundefined
styleDefines anchor tag stylesObject, Arrayundefined
classNameDefines anchor tag class nameStringundefined
childrenAnchor tag contentDOM node, Functionundefined

BlobProvider Web only

Easy and declarative way of getting document's blob data without showing it on screen. Refer to on the fly rendering for more information.

Valid props

Prop nameDescriptionTypeDefault
documentPDF document implementationDocumentundefined
childrenRender prop with blob, url, error and loading state as argumentsFunctionundefined