pdfmakejsx
Create the document definition for pdfmake using JSX.
Usage
npm install pdfmakejsx
Convert JSX to JS
If you are using typescript, tsconfig.json
needs compilerOptions.jsx
to be set to react
.
You can also add compilerOptions.jsxFactory
with h
(default is React.createElement
) to tell typescript how to interpret TSX files. This is not recommended if you use other JSX libraries in the same codebase. To set the JSX pragma on a file to file basis add /** @jsx h */
at the top of each JSX file.
/** @jsx h */
With a defined jsxFactory
, just importing h
is enough.
If you are using Babel, set the pragma option as explained here.
Example
pdf.tsx
try.ts
console.logtoDocumentDefinitionpage
npx ts-node try.ts
logs
This is a document definition that can be passed to pdfmake
.
pdfmakejsx
does not include pdfmake
, you will have to install that separately.
Base elements
<pdf>
has to be the parent element. It takes only 3 direct children: <content>
, <header>
and <footer>
, any other will be ignored.
The base elements, such as <text>
above, are as close as possible to the pdfmake
specs. It is highly recommended to use typescript to let your IDE and compiler tell you which elements and properties are allowed.
However some elements differ slightly form the document definition specs:
- A required
src
property has been added to<image>
<image ="image.png" =/>
becomes
- Tables have
<row>
elements
<table => <row> <text =>A</text> <text =>B</text> </row> <row> <text>1</text> <text>2</text> </row></table>
becomes
<canvas>
has its own internal elements
<line>
, <rect>
, <polyline>
and <ellipse>
will be ignored if used outside of canvas. So will any other elements inside the canvas.
<canvas> <line = = = = /> <rect = = = = ="blue"/> <polyline = = ="green" /> <ellipse = = = = ="red" /> <text>Will be ignored</text></canvas>
becomes