npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sharebox/chromiumly-with-config

v2.6.1

Published

A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.

Downloads

128

Readme

Chromiumly With Config

build coverage vulnerabilities maintainability npm downloads licence

A lightweight Typescript library that interacts with Gotenberg's different modules to convert a variety of document formats to PDF files.

Install

Using pnpm:

pnpm install @sharebox/chromiumly-with-config

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:8

Get Started

Modules

Chromiumly introduces different classes that serve as wrappers to Gotenberg's modules. These classes encompass methods featuring an input file parameter, such as html, header, footer, and markdown, capable of accepting inputs in the form of a string (i.e. file path), Buffer, or ReadStream.

Chormium

There are three different classes that come with a single method (i.e.convert) which calls one of Chromium's routes to convert html and markdown files, or a url to a buffer which contains the converted PDF file content.

URL

import { Chromiumly, UrlConverter } from "@sharebox/chromiumly-with-config";

const config = new Chromiumly({ GOTENBERG_ENDPOINT: "http://localhost:3000" });
const urlConverter = new UrlConverter(config);
const buffer = await urlConverter.convert({
    url: "https://www.example.com/",
});

HTML

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

import { Chromiumly, HtmlConverter } from "@sharebox/chromiumly-with-config";

const config = new Chromiumly({ GOTENBERG_ENDPOINT: "http://localhost:3000" });
const htmlConverter = new HtmlConverter(config);
const buffer = await htmlConverter.convert({
    html: "path/to/index.html",
});

Markdown

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

import {
    Chromiumly,
    MarkdownConverter,
} from "@sharebox/chromiumly-with-config";

const config = new Chromiumly({ GOTENBERG_ENDPOINT: "http://localhost:3000" });
const markdownConverter = new MarkdownConverter(config);
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)
    omitBackground?: boolean; // Hide the default white background and allow generating PDFs with transparency (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
};

PDF Engine

The PDFEngine combines the functionality of both Gotenberg's PDF Engines and LibreOffice modules to manipulate different file formats.

convert

This method interacts with LibreOffice module to convert different documents to PDF files. You can find the file extensions accepted here.

import { Chromiumly, PDFEngine } from "@sharebox/chromiumly-with-config";

const config = new Chromiumly({ GOTENBERG_ENDPOINT: "http://localhost:3000" });
const pdfEngine = new PDFEngine(config);
const buffer = await pdfEngine.convert({
    files: ["path/to/file.docx", "path/to/file.png"],
});

Similarly to Chromium's module convert method, this method takes the following optional parameters :

  • properties: changes how the PDF generated file will look like.
  • pdfFormat: PDF format of the conversion resulting file (i.e. PDF/A-1a, PDF/A-2b, PDF/A-3b).
  • merge: merge all the resulting files from the conversion into an individual PDF file.

merge

This method interacts with PDF Engines module which gathers different engines that can manipulate and merge PDF files such as: PDFtk, PDFcpu, QPDF, and UNO.

import { Chromiumly, PDFEngine } from "@sharebox/chromiumly-with-config";

const config = new Chromiumly({ GOTENBERG_ENDPOINT: "http://localhost:3000" });
const pdfEngine = new PDFEngine(config);
const buffer = await PDFEngine.merge({
    files: ["path/to/file.docx", "path/to/file.png"],
});

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.

Snippet

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

import { PDFEngine, UrlConverter } from "@sharebox/chromiumly-with-config";

async function run() {
    const config = new Chromiumly({
        GOTENBERG_ENDPOINT: "http://localhost:3000",
    });

    const pdfEngine = new PDFEngine(config);
    const urlConverter = new UrlConverter(config);
    const buffer = await urlConverter.convert({
        url: "https://gotenberg.dev/",
    });

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

run();