Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aymkdn/html-to-pdfmake

This module permits to convert HTML to the PDFMake format
https://github.com/aymkdn/html-to-pdfmake

javascript pdf

Last synced: 3 days ago
JSON representation

This module permits to convert HTML to the PDFMake format

Awesome Lists containing this project

README

        

# html-to-pdfmake

Convert HTML to PDFMake format with ease. This library bridges the gap between HTML content and [PDFMake](https://pdfmake.github.io/docs/) document definitions, allowing you to generate PDFs from basic HTML while maintaining based styling and structure.

**Note**: if you need to convert a complex HTML, check some online solutions, like [Doppio](https://doppio.sh/), or you could try to convert [your HTML to canvas](https://github.com/chearon/dropflow) and then to [export it to PDF](https://github.com/parallax/jsPDF).

## Features

- Convert HTML to PDFMake-compatible format
- Preserve basic styling and structure
- Support for tables, lists, images, and more
- Customizable styling options
- Works in both browser and Node.js environments
- Handle nested elements
- Custom tag support
- Image handling with reference support

## Online Demo

Try it live with the [online demo](https://aymkdn.github.io/html-to-pdfmake/index.html).

## Quick Start

### Browser Usage

```html





// Convert HTML to PDFMake format
const html = `
<div>
<h1>Sample Document</h1>
<p>This is a <strong>simple</strong> example with <em>formatted</em> text.</p>
</div>
`;

const converted = htmlToPdfmake(html);
const docDefinition = { content: converted };

// Generate PDF
pdfMake.createPdf(docDefinition).download('document.pdf');

```

### Node.js Usage

```bash
npm install html-to-pdfmake jsdom
```

```javascript
const pdfMake = require('pdfmake/build/pdfmake');
const pdfFonts = require('pdfmake/build/vfs_fonts');
const htmlToPdfmake = require('html-to-pdfmake');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;

// the below line may vary depending on your version of PDFMake
// please, check https://github.com/bpampuch/pdfmake to know how to initialize this library
pdfMake.vfs = pdfFonts;

// initiate the "window" object in Node
const { window } = new JSDOM('');

// Convert HTML to PDFMake format
const html = `


Sample Document


This is a simple example with formatted text.



`;

const converted = htmlToPdfmake(html, { window });
const docDefinition = { content: converted };

// Generate PDF
pdfMake.createPdf(docDefinition).getBuffer((buffer) => {
require('fs').writeFileSync('output.pdf', buffer);
});
```

## Supported HTML Elements

### Block Elements
- `

`, `

`, `


` to `

`
- `

`, ``, ``, ``, ``, ``, ``
- `