Skip to content

Commit e464ecd

Browse files
committed
Add PdfDocument, PdfHeader, PdfFooter. Render to TDocumentDefinitions instead of Content.
1 parent c2a3a31 commit e464ecd

23 files changed

+617
-257
lines changed

README.md

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ npm i react-pdfmake-reconciler
1616

1717
- Write complex PDF in JSX. Render JSX into PDF Make content structure.
1818
- Utilize React features like:
19-
- Context. Note that outside React context does not penetrate into PDF renderer.
19+
- Context. Note that outside React contexts do not penetrate into PDF renderer.
2020
- Components
2121
- Hooks
22-
- Working React update loop, (although it is unlikely to trigger user events inside PDF.)
23-
- e.g. async setState calls
24-
- Code autocomplete in JSX for "PDF Make components"
22+
- Working React update loop, (although it is unlikely to trigger user events inside PDF.), e.g.
23+
- async setState calls
24+
- useEffect call
25+
- TypeScript typing for PDF Make Components (`<pdf-*>` components)
26+
- React Developer Tools support
27+
28+
![React Developer Tools Demo](./screenshots/react-devtools-demo.png)
2529

2630
## Running demo
2731

@@ -32,56 +36,103 @@ pnpm dev
3236

3337
## Usage
3438

35-
See `/demo` and tests for more extensive examples.
39+
See `/demo` and [tests](./src/__tests__/PdfRenderer.test.tsx) for more extensive examples.
3640

3741
### Simple examples
3842

3943
```tsx
4044
/// <reference types="react-pdfmake-reconciler/react-jsx" />
4145

42-
import { PdfRenderer } from 'react-pdfmake-reconciler/PdfRenderer'
46+
import { PdfRenderer } from "react-pdfmake-reconciler/PdfRenderer";
4347

44-
const {unmount} = PdfRenderer.render(
48+
const { unmount } = PdfRenderer.render(
4549
<pdf-text bold>Hello World!</pdf-text>,
46-
content => console.log(content)
47-
)
50+
(document) => console.log(document),
51+
);
4852

4953
/*
5054
Console:
5155
{
52-
$__reactPdfMakeType: 'pdf-text',
53-
text: 'Hello World!',
54-
bold: true
56+
content: {
57+
$__reactPdfMakeType: 'pdf-text',
58+
text: 'Hello World!',
59+
bold: true
60+
}
5561
}
5662
*/
5763

5864
// Call unmount to detach node tree.
59-
unmount()
65+
unmount();
6066
```
6167

6268
```tsx
63-
import { PdfRenderer } from 'react-pdfmake-reconciler/PdfRenderer'
69+
import { PdfRenderer } from "react-pdfmake-reconciler/PdfRenderer";
6470

65-
const content = await PdfRenderer.renderOnce(<pdf-text bold>Hello World!</pdf-text>)
71+
const document = PdfRenderer.renderOnce(<pdf-text bold>Hello World!</pdf-text>);
6672
```
6773

6874
### PDF elements
6975

70-
Newly defined intrinsic elements have the `pdf-` prefix. Roughly, each type of PDF Make node corresponds to one element type, where the property specifying `Content` is mapped to the `children` prop. For example:
76+
Newly defined intrinsic elements by this package have the `pdf-` prefix. Roughly speaking, each type of PDF Make content object corresponds to one element type, where the property specifying the `Content` is mapped to the `children` prop. For example:
7177

7278
```tsx
7379
const pdfMakeContent = {
74-
text: 'GitHub',
75-
link: 'https://www.github.com'
76-
}
80+
text: "GitHub",
81+
link: "https://www.github.com",
82+
};
7783

7884
// is mapped to
7985

86+
const pdfNode = <pdf-text link="https://www.github.com">GitHub</pdf-text>;
87+
```
88+
89+
There are also virtual element types. For more information, read [JSDocs in types](./src/types/PdfElements.ts) for more information.
90+
91+
### Document, Header, and Footer
92+
93+
You can easily define extra document definition props straight inside your JSX using `<PdfDocument>`. It is optional to put the body of the document inside this component.
94+
95+
Implemented using React Portals, you can define static/dynamic header and footer using `<PdfHeader>` and `<PdfFooter>`.
96+
97+
These components can appear anywhere within your JSX structure, although you may follow this convention for a better looking structure:
98+
99+
```tsx
100+
import { PdfDocument, PdfHeader, PdfFooter } from "react-pdfmake-reconciler";
101+
80102
const pdfNode = (
81-
<pdf-text link="https://www.github.com">
82-
GitHub
83-
</pdf-text>
84-
)
103+
<PdfDocument orientation="landscape">
104+
{/* Example static header */}
105+
<PdfHeader>This is a header</PdfHeader>
106+
{/* Example dynmaic footer */}
107+
<PdfFooter>
108+
{(pageNumber, pageCount) => (
109+
<pdf-text>
110+
Page {pageNumber} / {pageCount}
111+
</pdf-text>
112+
)}
113+
</PdfFooter>
114+
{bodyGoesHere}
115+
</PdfDocument>
116+
);
85117
```
86118

87-
There are also virtual element types. For more information, read JSDocs in types.
119+
### PdfPreview
120+
121+
`<PdfPreview>` provides an easy way to render your React PDF Make Reconciler JSX in the browser. You can also debug your PDF JSX using the [React Developer Tools](https://chromewebstore.google.com/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi) browser extension.
122+
123+
```tsx
124+
import { FC, StrictMode } from "react";
125+
import { PdfPreview } from "react-pdfmake-reconciler";
126+
127+
const App: FC = () => (
128+
<div>
129+
<PdfPreview>
130+
{/* Optional */}
131+
<StrictMode>
132+
{/* Only use components that resolves to pdf-* components from here on out. DOM elements won't work. */}
133+
<pdf-text>Hello World!</pdf-text>
134+
</StrictMode>
135+
</PdfPreview>
136+
</div>
137+
);
138+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"prettier": "^3.1.0",
6464
"react": "^18.2.0",
6565
"react-dom": "^18.2.0",
66-
"typescript": "^5.2.2",
66+
"typescript": "^5.3.2",
6767
"vite": "^5.0.0",
6868
"vite-plugin-dts": "^3.6.3",
6969
"vitest": "^0.34.6"

0 commit comments

Comments
 (0)