https://github.com/dantio/html2pdfmake
html2pdfmake parse html to content that can be used in pdfmake.
https://github.com/dantio/html2pdfmake
pdf pdfmake pdfmake-library
Last synced: 5 months ago
JSON representation
html2pdfmake parse html to content that can be used in pdfmake.
- Host: GitHub
- URL: https://github.com/dantio/html2pdfmake
- Owner: dantio
- License: isc
- Created: 2022-04-02T13:51:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-06-30T16:20:22.000Z (about 4 years ago)
- Last Synced: 2025-09-29T04:48:22.317Z (9 months ago)
- Topics: pdf, pdfmake, pdfmake-library
- Language: TypeScript
- Homepage: https://dantio.github.io/html2pdfmake/
- Size: 1.18 MB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# html2pdfmake
Advanced HTML to PDFMake DocDefinition parser.
Parse HTML/DOM to pdfmake.
## Install
```bash
npm i html2pdfmake
yarn add html2pdfmake
```
## Quick Usage
### Module
```html
Text
import {parse} from 'https://cdn.jsdelivr.net/npm/html2pdfmake/dist/html2pdfmake.mjs';
const {content, images, patterns} = parse(document.getElementById('template'));
pdfMake.createPdf({
// everything else
content,
images,
patterns
})
```
### UMD
```html
Text
const {content, images, patterns} = html2pdfmake.parse(document.getElementById('template'));
pdfMake.createPdf({
// everything else
content,
images,
patterns
})
```
## Config
Pass the config as the second parameter in `parse` function.
```ts
// parse('
', config);
export type Config = {
globalStyles?: CssStyles, // custom global styles
styles: CssStyles, // additional styles merged that overwrites globals styles
nodeRule?: NodeRule // set the custom node rule. Return undefined if pre-defined rules should be applied.
styleRule?: StyleRule // set the custom style rule. Return false if pre-defined rules should be applied.
collapseMargin: true, // Enable/Disable margin collapse
collapseWhitespace: true, // Enable/Disable witespace collapse
render: (e, data) => e, // set render function for header/footer
document: () => window.document, // set the global document object
parseCss: (style: NodeListOf) => {}, // define parser
defaultFont: 'Roboto', // set the default font
fonts?: TFontDictionary // pass the fonts that you are using for pdfmake to filter unsupported fonts.
}
```
### nodeRule example
```js
const config = {
nodeRule: (el) => {
if (el.nodeName === '#text') {
return {
text: 'My Custom text'
};
}
return undefined; // use pre-defined rules
}
}
```
### styleRule example
```js
const config = {
styleRule: (directive, value, props) => {
if (directive === 'color') {
props.color = 'red';
return true; //
}
return false;
}
}
```
### NodeJS
Install a HTML parser like JSDOM.
## Run examples
```sh
npm i http-server -g
npm i
npm run watch
# open new terminal
http-server .
# open localhost:8080
```