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

quill-to-pdf

v1.0.7

Published

Create a PDF from a QuillJS delta

Downloads

4,861

Readme

npm Travis (.com) GitHub last commit npm GitHub issues NPM

QuillToPDF

Simple Description: Turn the content of your QuillJS editor into a downloadable PDF document.

Technical Description: Convert a QuillJS delta object into a .pdf BLOB.

Check out a live demo on StackBlitz.

How to Install

Install using npm:

npm i quill-to-pdf --save

How Do I Use It?

Pass a QuillJS delta object to the generatePdf() function of the pdfExporter object, which is imported from the quill-to-pdf package. Be sure to await the generatePdf() function, because it returns a Promise.

const quillDelta = quillInstance.getContents();
const pdfBlob = await pdfExporter.generatePdf(quillDelta);

The quillInstance refers to the object created by new Quill(). The pdfExporter refers to the default export of the quill-to-pdf package, which can be imported as follows:

import { pdfExporter } from 'quill-to-pdf';

What Does the Package Do?

This package creates a PDF from a QuillJS delta. In short, this package will allow you to download the contents of your QuillJS in-browser editor as a PDF document.

How Does It Work?

QuillJS stores its editor content in a "delta" format. QuillToPdf parses the Quill delta object using the quilljs-parser package and then generates a PDF from the parsed Quill delta using the PDFKit package.

How Can I Download the PDF Document from the Browser?

You can download the PDF document created by QuillToPDF by using the FileSaver package.

You'll need to install file-saver from npm first.

npm i file-saver --save

You can also install the types, if you're using TypeScript.

npm i @types/file-saver --save-dev

Here is a brief example of how to download the PDF document from the browser.

import { saveAs } from 'file-saver';
import { pdfExporter } from 'quill-to-pdf';
import * as quill from 'quilljs';

// Here is your Quill editor instance
const quillInstance = new Quill();

// Here is your export function
// Typically this would be triggered by a click on an export button
async function export() {
    const delta = quillInstance.getContents(); // gets the Quill delta
    const pdfAsBlob = await pdfExporter.generatePdf(delta); // converts to PDF
    saveAs(pdfAsBlob, 'pdf-export.pdf'); // downloads from the browser
}

Which QuillJS Formatting Features Are Supported?

Quill offers a wide range of formatting features. QuillToPDF does not currently support all Quill formatting features. This is because replicating some of the QuillJS formats using PDFKit, which this package relies on, is complicated.

The following features are fully supported:

  • Bold
  • Italic
  • Font
  • Link
  • Size
  • Strikethrough
  • Underline
  • Header 1
  • Header 2
  • List
    • Bullet
    • Ordered
  • Block Quote
  • Code Block

The following features are only partially supported:

  • Formula (the raw text of the formula will be inserted into the PDF, not the KaTex formatted version)
  • Image (the image will be inserted with absolute width and height settings)
  • Video (the link to the video will be included in the PDF document)

The following features are NOT supported:

  • Text direction (cannot be switched to right-to-left)
  • Background (cannot color the background of the text)
  • Superscript
  • Subscript
  • Color
  • Inline code
  • Indent (but indents for lists ARE supported)

How Can I Configure QuillToPDF?

Currently, QuillToPDF allows you to configure the styles used for the main text formats that appear in a Quill editor.

Six text formats are currently recognized by QuillToPDF:

  1. Normal—refers to basic text (normal)
  2. Header 1—refers to a level one heading (header_1)
  3. Header 2—refers to a level two heading (header_2)
  4. Block Quote—refers to a block quote (block_quote)
  5. Code Block—refers to a block of code (code_block)
  6. List Paragraph—refers to the text in a bulleted or ordered list (list_paragraph)

QuillToPDF provides default styles for each of these six text formats. For instance, normal text is automatically formatted as Times-Roman font and 12 points in size, whereas header_1 text is automatically formatted as Helvetica-Bold font and 16 points in size.

The default styles for the 6 text formats can be overridden by providing a style configuration object as the second argument to the generatePdf() function.

The configuration object should satisfy the following interface:

interface Config {
    styles: {
        normal?: {
            font?: string;
            fontSize?: number; // specified in points
            baseIndent?: number; // specified in points w/ 72 ppi
            levelIndent?: number; // only used for lists
            indent?: {
                left?: number;
                right?: number;
            }
        }
    }
}

The object in the styles property above can contain a key for any of the six text formats: normal, header_1, header_2, block_quote, code_block, or list_paragraph.

For example, if I want to override the default style for a level one heading, I could do the following:

// create a configuration object that satisfies the interface above
const config: Config = {
    styles: {
        header_1: {
            font: 'Times-Bold', // default is 'Helvetica-Bold'
            fontSize: 18 // default is 16
        }
    }
};

// pass the configuration object as the second argument to generatePdf()
const pdfBlob = await pdfExporter.generatePdf(quillDelta, config);

The options for font are limited to those that come pre-packaged with PDFKit:

  • 'Courier'
  • 'Courier-Bold'
  • 'Courier-Oblique'
  • 'Courier-BoldOblique'
  • 'Helvetica'
  • 'Helvetica-Bold'
  • 'Helvetica-Oblique'
  • 'Helvetica-BoldOblique'
  • 'Times-Roman'
  • 'Times-Bold'
  • 'Times-Italic'
  • 'Times-BoldItalic'

Again, check out the demo on StackBlitz.