React-pdf (2024)

Styling

Because a document without styles would be very boring, react-pdf ships a powerful styling solution using CSS and Flexbox.

StyleSheet API

React-pdf also sticks with the primitives specs when it comes to styling.

StyleSheet.create()

Create a stylesheet. This method expects a valid JS object as only argument (containing as much css definitions as you want) and returns an object that you can pass down to components via the style prop

import React from 'react';import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';const styles = StyleSheet.create({ page: { backgroundColor: 'tomato' }, section: { color: 'white', textAlign: 'center', margin: 30 }});const doc = ( <Document> <Page size="A4" style={styles.page}> <View style={styles.section}> <Text>Section #1</Text> </View> </Page> </Document>);ReactPDF.render(doc);

Inline styling

There's no need to call StyleSheet.create in order to style components. You can also just pass a plain JS object to the style prop and react-pdf will get the job done.

import React from 'react';import { Page, Text, View, Document } from '@react-pdf/renderer';const MyDocument = () => ( <Document> <Page size="A4" style={{ backgroundColor: 'tomato' }}> <View style={{ color: 'white', textAlign: 'center', margin: 30 }}> <Text>Section #1</Text> </View> </Page> </Document>);

Mixing both solutions

The style prop also accepts an Array as value, containing any possible combination of the last two alternatives

import React from 'react';import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';const styles = StyleSheet.create({ page: { backgroundColor: 'tomato' }, section: { textAlign: 'center', margin: 30 }});const MyDocument = () => ( <Document> <Page size="A4" style={styles.page}> <View style={[styles.section, { color: 'white' }]}> <Text>Section #1</Text> </View> </Page> </Document>);

Protip: This can be useful when you want to apply both predefined styles, and styles based on props

Media queries

There may be times in which you'll need to apply different styles based on the document context. For that, we provide media-queries support (just as you would do it for the web!). You can query based on both width and height (min and max), and also orientation:

import React from 'react';import { Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';const styles = StyleSheet.create({ section: { width: 200, '@media max-width: 400': { width: 300, }, '@media orientation: landscape': { width: 400, }, }});const MyDocument = () => ( <Document> <Page size="A4" style={styles.page}> <View style={styles.section}> <Text>Section #1</Text> </View> </Page> </Document>);

Valid units

pt (default. Based on the standard 72 dpi PDF document)

in inches

mm millimeters

cm centimeters

% percentage

vw viewport/page width

vh viewport/page height

Valid CSS properties

Flexbox

  • alignContent
  • alignItems
  • alignSelf
  • flex
  • flexDirection
  • flexWrap
  • flexFlow
  • flexGrow
  • flexShrink
  • flexBasis
  • justifyContent

Layout

  • bottom
  • display
  • left
  • position
  • right
  • top
  • overflow
  • zIndex

Dimension

  • height
  • maxHeight
  • maxWidth
  • minHeight
  • minWidth
  • width

Color

  • backgroundColor
  • color
  • opacity

Text

  • fontSize
  • fontFamily
  • fontStyle
  • fontWeight
  • letterSpacing
  • lineHeight
  • maxLines
  • textAlign
  • textDecoration
  • textDecorationColor
  • textDecorationStyle
  • textIndent
  • textOverflow
  • textTransform

Sizing/positioning

  • object-fit
  • object-position

Margin/padding

  • margin
  • marginHorizontal
  • marginVertical
  • marginTop
  • marginRight
  • marginBottom
  • marginLeft
  • padding
  • paddingHorizontal
  • paddingVertical
  • paddingTop
  • paddingRight
  • paddingBottom
  • paddingLeft

Transformations

  • transform:rotate
  • transform:scale
  • transform:scaleX
  • transform:scaleY
  • transform:translate
  • transform:translateX
  • transform:translateY
  • transform:skew
  • transform:skewX
  • transform:skewY
  • transform:matrix
  • transformOrigin

Borders

  • border
  • borderColor
  • borderStyle
  • borderWidth
  • borderTop
  • borderTopColor
  • borderTopStyle
  • borderTopWidth
  • borderRight
  • borderRightColor
  • borderRightStyle
  • borderRightWidth
  • borderBottom
  • borderBottomColor
  • borderBottomStyle
  • borderBottomWidth
  • borderLeft
  • borderLeftColor
  • borderLeftStyle
  • borderLeftWidth
  • borderTopLeftRadius
  • borderTopRightRadius
  • borderBottomRightRadius
  • borderBottomLeftRadius
React-pdf (2)
React-pdf (2024)
Top Articles
Indicadores de análisis técnico más populares en trading
How Taking a Vacation Improves Your Well-Being
Dew Acuity
Www.politicser.com Pepperboy News
30 Insanely Useful Websites You Probably Don't Know About
Seething Storm 5E
Free Robux Without Downloading Apps
Citi Card Thomas Rhett Presale
Smokeland West Warwick
Ncaaf Reference
Pwc Transparency Report
Craigslist Chautauqua Ny
Top Hat Trailer Wiring Diagram
Immediate Action Pathfinder
Truck Toppers For Sale Craigslist
Powerball winning numbers for Saturday, Sept. 14. Check tickets for $152 million drawing
2020 Military Pay Charts – Officer & Enlisted Pay Scales (3.1% Raise)
Officialmilarosee
Mythical Escapee Of Crete
Manuela Qm Only
Beaufort 72 Hour
Keyn Car Shows
Wrights Camper & Auto Sales Llc
Cona Physical Therapy
Dexter Gomovies
Srjc.book Store
950 Sqft 2 BHK Villa for sale in Devi Redhills Sirinium | Red Hills, Chennai | Property ID - 15334774
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Egg Crutch Glove Envelope
Ducky Mcshweeney's Reviews
Watchseries To New Domain
Ishow Speed Dick Leak
SOC 100 ONL Syllabus
Delaware judge sets Twitter, Elon Musk trial for October
Gpa Calculator Georgia Tech
1v1.LOL Game [Unblocked] | Play Online
Check From Po Box 1111 Charlotte Nc 28201
18 terrible things that happened on Friday the 13th
2023 Nickstory
Hireright Applicant Center Login
Setx Sports
Frigidaire Fdsh450Laf Installation Manual
Cvs Coit And Alpha
Jigidi Free Jigsaw
Craigslist Pets Charleston Wv
6463896344
Shannon Sharpe Pointing Gif
Rétrospective 2023 : une année culturelle de renaissances et de mutations
Optimal Perks Rs3
BYU Football: Instant Observations From Blowout Win At Wyoming
ats: MODIFIED PETERBILT 389 [1.31.X] v update auf 1.48 Trucks Mod für American Truck Simulator
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6298

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.