Generate modular PDFs via pdfmake using JSX.
import PDFMake from 'pdfmake';import JsxPdf from 'jsx-pdf';import OpenSans from './font-descriptors'; const pdfMake = OpenSans; const stream = pdfMake; // write the stream to a file; this could also be streamed to an HTTP connection, stdout etcstream;stream;stream;
Quick start
Javascript
yarn add jsx-pdf @babel/plugin-transform-react-jsx
- Configure the part of your build that transpiles your JSX to use
JsxPdf.createElement
as the element factory.- For babel, add the configuration below to your
.babelrc
.
"plugins": - For babel, add the configuration below to your
Typescript
yarn add -D @types/jsx-pdf
- For TypeScript, add the configuration below to your
tsconfig.json
. Settingjsx
toreact
configures TypeScript to handle JSX transpiling for you, and thejsxFactory
option specifies the element factory to use. SettingjsxFragmentFactory
allows you to use JSX Fragments.
"compilerOptions": ,
- Code away! See the examples below.
You can also run our example script by running yarn demo
and opening the generated pdf at example/example.pdf
. Check the console logs for additional information.
Components
Similar to modern front-end frameworks, you can define your own components using declarative syntax.
Basic example:
import JsxPdf from 'jsx-pdf'; const Greeting = name <text>Hello name!</text>; const doc = <document> <content> <Greeting ="Bob" /> </content> </document>;
List example:
import JsxPdf from 'jsx-pdf'; const GroupGreeting = names <> names </>; const doc = <document> <content> <GroupGreeting = /> </content> </document>;
Inline If example:
import JsxPdf from 'jsx-pdf'; const Signature = <text>JSX-PDF Inc</text>; const SignedGreeting = name <> name && <Greeting = /> <Signature /> </>; const doc = <document> <content> <SignedGreeting /> </content> </document>;
Inline If-Else example:
import JsxPdf from 'jsx-pdf'; const AnonymousGreeting = <text>We know you</text>; const SignedGreeting = name <> name ? <Greeting = /> : <AnonymousGreeting /> <Signature /> </>; const doc = <document> <content> <SignedGreeting /> </content> </document>;
Element variable example:
import JsxPdf from 'jsx-pdf'; const SignedGreeting = name let greeting; if name greeting = <Greeting = />; else greeting = <AnonymousGreeting />; return <> greeting <Signature /> </> ;; const doc = <document> <content> <SignedGreeting /> </content> </document>;
Styling
Styling can be done by adding appropriate attributes to tags. It's often helpful for readability to group style-related attributes together and use the spread syntax.
import JsxPdf from 'jsx-pdf'; const Greeting = name const styles = italics: true fontSize: 10 ; return <text >Hello name!</text>;; const doc = <document> <content> <Greeting ="Bob" /> </content> </document>;
Context
Each component has access to global context and can update it if necessary.
import JsxPdf from 'jsx-pdf'; const AllowedUsersProvider = attributes context updateContext ; return attributeschildren0;; const SecureGreeting = name allowedUsers allowedUsers ? <text>Hello name!</text> : <text>You are not allowed</text> ; const doc = <AllowedUsersProvider> <document> <content> <SecureGreeting ="Bob" /> </content> </document> </AllowedUsersProvider>;
Document primitives
This section describes basic elements provided by the library. More information about supported attributes and advanced examples can be found here.
Top elements
Each document has to be enclosed within document
tag with nested content
, and optional header
and footer
. The document is the place for configuration that affects the whole PDF, such as page margins, page size, default style, and metadata.
import JsxPdf from 'jsx-pdf'; const doc = <document = ="A4" = = > <header>Greeting</header> <content>Hello Bob!</content> <footer>JSX-PDF Inc</footer> </document>;
Dynamic Header and Footer
If you want to use the dynamic header functionality in pdfmake, simply pass a render function as the only child of the header or footer:
const doc = <document> <header> currentPage pageCount <text> Page currentPage of pageCount </text> </header> <content>/* ... */</content> </document>;
The parameters are:
currentPage
- the 1-indexed page for which the content is being renderedpageCount
- the total number of pages in the documentpageSize
- an object containing information about the dimensions of the page.
Paragraphs
Paragraphs are defined using text
tag.
import JsxPdf from 'jsx-pdf'; const doc = <document> <content> <text> This sentence will be rendered as one paragraph even though there are line breaks </text> <text>This is another paragraph</text> </content> </document>;
In order to apply styling to a group of paragraphs, they can be wrapped with a stack
tag.
import JsxPdf from 'jsx-pdf'; const doc = <document> <content> <stack ="red"> <text>First red parahraph</text> <text>Second red parahraph</text> </stack> <text ="blue">Blue parahraph</text> </content> </document>;
Columns
Elements nested in columns
tag will be stacked horizontally.
import JsxPdf from 'jsx-pdf'; const doc = <document> <content> <columns => <column =>Fixed width column</column> <column ="10%">Percentage width column</column> <column ="auto"> Column that adjusts width based on the content </column> <column ="*">Column that fills the remaining space</column> </columns> </content> </document>;
Lists
Both ordered and unordered lists are supported.
import JsxPdf from 'jsx-pdf'; const docWithOrderedList = <document> <content> <ol = = ="lower-roman"> <text>Item 1</text> <text>Item 2</text> <text>Item 3</text> </ol> </content> </document>; const docWithUnorderedList = <document> <content> <ul ="blue" ="red" ="square"> <text>Item 1</text> <text>Item 2</text> <text>Item 3</text> </ul> </content> </document>;
Tables
table
tag provides a simple way of creating table layouts.
const leftCellStyle = color: 'grey'; const doc = <document> <content> <table = = ="headerLineOnly"> <row> <cell>Fixed width column</cell> <cell>Column that fills the remaining space</cell> <cell>Column that adjusts width based on the content</cell> </row> <row> <cell >Cell 11</cell> <cell>Cell 12</cell> <cell>Cell 13</cell> </row> <row> <cell >Cell 21</cell> <cell>Cell 22</cell> <cell>Cell 23</cell> </row> </table> </content> </document>;
Images
image
supports JPEG and PNG formats.
import JsxPdf from 'jsx-pdf'; const doc = <document> <content> <image ="/home/bob/photos/Bob.png" = = /> </content> </document>;
SVGs
The svg
tag can be used to render SVG images. The width
, height
and fill
attributes can be used to control the size of the image as described in the pdfmake docs.
import JsxPdf from 'jsx-pdf'; const doc = <document> <content> <svg = /> </content> </document>;
QR Codes
The qr
tag can be used to render QR codes. There are various options available as described in the pdfmake docs.
import JsxPdf from 'jsx-pdf'; const doc = <document> <content> <qr ="My text" /> </content> </document>;
API
renderPdf
Accepts JSX and returns a PDF JSON representation in the format expected by pdfmake.
createElement
This function converts JSX to object representation. Every time JSX syntax is used, the function has to be made available. The functionality depends on the babel plugin @babel/plugin-transform-react-jsx
(or equivalent), and Babel must be set up in the project in order to transpile the JSX correctly.
Example .babelrc
file:
Disclaimer
Copyright 2018 Schibsted
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
License
By contributing to this project, you agree that your contributions will be licensed under its MIT license.