JSPM

  • Created
  • Published
  • Downloads 10053
  • Score
    100M100P100Q129206F
  • License MIT

A lightweight Typescrpit library which interacts with Gotenberg's Chromium module to convert HTML documents to PDF.

Package Exports

  • chromiumly
  • chromiumly/dist/main.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 (chromiumly) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Chromiumly

A lightweight Typescrpit library which interacts with Gotenberg's Chromium module to convert HTML documents to PDF.

Prerequisites

Before attempting to use Chromiumly, be sure you install Docker if you have not already done so.

After that, you can start a default Docker container of Gotenberg as follows:

docker run --rm -p 3000:3000 gotenberg/gotenberg:7

Get Started

Configuration

Chromiumly supports both dotenv and config configuration libraries to add Gotenberg endpoint to your project.

dotenv

GOTENBERG_ENDPOINT=localhost:3000

config

{
  "gotenberg": {
    "enpdoint": "localhost:3000"
  }
}

Usage

Chromiumly introduces different classes that serve as wrappers to Gotenberg's Chromium routes.

Each class comes with two methods :

convert

This method takes either a url, an html file path or html and markdown file paths and returns a buffer which contains the converted PDF file content.

generate

It is just a generic complementary method that takes the buffer returned by the convert method, and a chosen filename to generate the PDF file.

Please note that all the PDF files can be found __generated__ folder in the root folder of your project

Modules

URL

import { UrlConverter } from "chromiumly";

const urlConverter = new UrlConverter();
const buffer = await urlConverter.convert({
    url: "https://www.example.com/",
});

HTML

The only requirement is that the file name should be index.html.

import { HtmlConverter } from "chromiumly";

const htmlConverter = new HtmlConverter();
const buffer = await htmlConverter.convert({
    html: "path/to/index.html",
});

Markdown

This route accepts an index.html file plus a markdown file.

import { MarkdownConverter } from "chromiumly";

const markdownConverter = new MarkdownConverter();
const buffer = await markdownConverter.convert({
    html: "path/to/index.html",
    markdown: "path/to/file.md",
});

Customization

convert() method takes an optional properties parameter of the following type which dictates how the PDF generated file will look like.

type PageProperties = {
  size?: {
    width: number; // Paper width, in inches (default 8.5)
    height: number; //Paper height, in inches (default 11)
  };
  margins?: {
    top: number; // Top margin, in inches (default 0.39)
    bottom: number; // Bottom margin, in inches (default 0.39)
    left: number; // Left margin, in inches (default 0.39)
    right: number; // Right margin, in inches (default 0.39)
  };
  preferCssPageSize?: boolean; // Define whether to prefer page size as defined by CSS (default false)
  printBackground?: boolean; // Print the background graphics (default false)
  landscape?: boolean; // Set the paper orientation to landscape (default false)
  scale?: number; // The scale of the page rendering (default 1.0)
  nativePageRanges?: { from: number; to: number }; // Page ranges to print
};

Snippet

The following is a short snippet of how to use the library.

import { UrlConverter } from "chromiumly";

async function run() {
  const urlConverter = new UrlConverter();
  const buffer = await urlConverter.convert({
    url: "https://gotenberg.dev/",
  });

  await urlConverter.generate("gotenberg.pdf", buffer);
}

run();