# node-html-transformers

> A helper function that lets you transform html to string, pdf and image. This tool lets you dynamically insert content into any HTML file.

Latest version **1.0.4** (published 2024-06-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install node-html-transformers
pnpm add node-html-transformers
yarn add node-html-transformers
bun add node-html-transformers
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2024-06-28 |
| First published | 2024-01-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 46.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Ayomikun Faluyi |
| Keywords | node functions, helper, utils, html to image, html-transformer, backend |

## Links

- npm: https://www.npmjs.com/package/node-html-transformers
- Repository: https://github.com/dcoder-x/node-HTML-transformer
- Homepage: https://github.com/dcoder-x/node-HTML-transformer#readme
- Issues: https://github.com/dcoder-x/node-HTML-transformer/issues
- npm.io page: https://npm.io/package/node-html-transformers

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2024-06-28
- 1.0.3 — 2024-06-28
- 1.0.2 — 2024-01-31
- 1.0.1 — 2024-01-30
- 1.0.0 — 2024-01-30

## README

# Node HTML transformers

The `Node HTML transformers` class is designed to facilitate the conversion of HTML content to other formats, specifically to PDF and image. It provides methods to replace placeholders in the HTML content and then convert the processed HTML to a PDF file or an image.

## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
- [Class Methods](#class-methods)
  - [toString](#tostring)
  - [toPDF](#topdf)
  - [toImage](#toimage)
- [Class Properties](#class-properties)
- [Dependencies](#dependencies)
- [Contributing](#contributing)
- [License](#license)

## Installation

Make sure you have Node.js and npm installed. Install the required dependencies using the following command:

```bash
npm install node-html-transformers
```

## Usage

```typescript
import HtmlTransformer from 'node-html-transformers';

// Create an instance of the HTMLTransformer class
const converter = new HtmlTransformer('path/to/html/file.html', 'utf-8', { variable1: 'value1', variable2: 'value2' });

// Convert HTML to PDF
const pdfContent = await converter.toPDF('output.pdf');
console.log('Base64-encoded PDF Content:', pdfContent);

// Convert HTML to Image
const imageContent = await converter.toImage('output.png');
console.log('Base64-encoded Image Content:', imageContent);

```

## Templating with HTML

You can create placeholders in HTML documents and then Dynamically pass in data.
To do this you should surround the placeholder variable with double curly braces e.g `{{placeholderVariable}}`. 

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Document</title>
</head>
<body>
    <h1>
        Sample template in html
    </h1>
    <p>
        My name is {{name}}, I am {{age}} years old.
    </p>
<img src={{imageUrl}} />
</body>
</html>
```



Then, pass the same variable name as a field to the content `{placeholderVariable:value}` argument when creating an instance of the HtmlTransformer class e.g 

```typescript
import HtmlTransformer from 'node-html-transformers';

// Create an instance of the HTMLTransformer class
const converter = new HtmlTransformer('path/to/html/file.html', 'utf-8', { name: 'Ayomikun', age: '20' });


```



## Class Methods


### toString

Converts the HTML content to a string with replaced placeholders.
returns a string

```typescript
  const htmlString = await converter.toString();

```

### toPDF
Converts the HTML content to a PDF file.
-- returns a base64 encoded PDF string

```typescript
const pdfContent = await converter.toPDF('output.pdf');
console.log('Base64-encoded PDF Content:', pdfContent);

```

### toImage


Converts the HTML content to an image.
-- returns a base64 encoded Image string

```typescript
const imageContent = await converter.toImage('output.png');
console.log('Base64-encoded Image Content:', imageContent);
```

## Class Properties
- filePath: The file path of the HTML file to be converted.
- encoding: The encoding of the HTML file. Defaults to 'utf-8'.
- content: An object containing key-value pairs for replacing placeholders in the HTML content.
- Dependencies
- puppeteer: Headless Chrome browser automation library.
  
## Contributing
Feel free to contribute by opening issues or submitting pull requests. Contributions are welcome!

## License
This project is licensed under the MIT License.

#### made with 💘 by Ayomikun Faluyi

---
_Source: https://npm.io/package/node-html-transformers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
