JSPM

html-conversion-node

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q29530F
  • License ISC

package to covert html code to any other formats

Package Exports

  • html-conversion-node
  • html-conversion-node/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (html-conversion-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

html-conversion-node

npm version License: MIT

html-conversion-node is a Node.js module that allows you to convert HTML content to PDF and plain text formats using Puppeteer and html-to-text libraries. It provides simple and straightforward functions to make the conversion process easy.

Installation

To use html-conversion-node, you need to have Node.js and npm installed on your machine. If you don't have them, you can download and install them from the official Node.js website (https://nodejs.org).

To install the package, run the following command in your project directory:

npm install html-conversion-node

Usage

convertHtmlToPdf(htmlContent, outputPath, options)

Converts HTML content to a PDF file and saves it to the specified output path.

Parameters:

  • htmlContent (string): The HTML content or the path to the HTML file to convert to PDF.
  • outputPath (string, optional): The path where the generated PDF will be saved. If not provided, the PDF buffer will be returned.
  • options (object, optional): Additional options for Puppeteer's page.pdf function. For available options, refer to the Puppeteer documentation.

Returns:

  • If outputPath is provided, the function returns a Promise that resolves with the PDF buffer if the conversion is successful.
  • If outputPath is not provided, the function returns a Promise that resolves with the PDF buffer.

Example:

const { convertHtmlToPdf } = require('html-conversion-node');

const htmlContent = '<html><body><h1>Hello, world!</h1></body></html>';
const outputPath = './output.pdf';

convertHtmlToPdf(htmlContent, outputPath)
    .then(() => {
        console.log('Conversion to PDF successful.');
    })
    .catch((error) => {
        console.error('Error occurred:', error.message);
    });

convertHtmlToTxt(htmlContent, outputPath)

Converts HTML content to plain text and saves it to the specified output path.

Parameters:

  • htmlContent (string): The HTML content or the path to the HTML file to convert to plain text.
  • outputPath (string, optional): The path where the generated text file will be saved. If not provided, the plain text content will be returned.

Returns:

  • If outputPath is provided, the function returns a Promise that resolves with the plain text content if the conversion is successful.
  • If outputPath is not provided, the function returns a Promise that resolves with the plain text content.

Example:

const { convertHtmlToTxt } = require('html-conversion-node');

const htmlContent = '<html><body><h1>Hello, world!</h1></body></html>';
const outputPath = './output.txt';

convertHtmlToTxt(htmlContent, outputPath)
    .then(() => {
        console.log('Conversion to text successful.');
    })
    .catch((error) => {
        console.error('Error occurred:', error.message);
    });

Note

This module uses Puppeteer to launch a headless browser to render the HTML content and convert it to PDF. Ensure that you have enough memory and processing power to run Puppeteer.in some ubuntu systems there will not be support for this package it will be supported in ubuntu 20.4 or above if the you want to use this package bellow that ubuntu verison you need to install certain packaged

sudo apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev