1.3.0 • Published 3 years ago

quill-to-word v1.3.0

Weekly downloads
74
License
MIT
Repository
github
Last release
3 years ago

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

QuillToWord

Simple Description: Turn the content in your QuillJS editor into a downloadable Microsoft Word document.

Technical Description: Convert a QuillJS delta object into a .docx file.

Check out a live Angular demo on Stackblitz.

How to Install

Install using npm:

npm i quill-to-word --save

How Do I Use It?

Pass a QuillJS delta object to the generateWord() function of the quill-to-word package. Be sure to await the generateWord() function, because it returns a Promise.

const quillDelta = quillInstance.getContents();
const doc = await quillToWord.generateWord(quillDelta);

quillInstance refers to the object created by calling new Quill().

What Does the Package Do?

This package creates a Microsoft Word DOCX file from a QuillJS Delta. In short, this package will allow you to download the contents of your QuillJS in-browser editor as a Word document.

How Does It Work?

QuillJS stores its content in a delta format. QuillToWord parses a Quill delta object into a paragraph format using QuillJSParser. Then, QuillToWord generates a Word document, using the popular DOCX package.

How Can I Download the Word Document from the Browser?

You can download the Word document created by QuillToWord by using the FileSaver package. You'll need to install file-saver.

npm i file-saver --save

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

Here is an example of downloading from a browser.

import { saveAs } from 'file-saver';
import * as quillToWord from 'quill-to-word';
import * as quill from 'quilljs';

const quillInstance = new Quill();
const delta = quillInstance.getContents();
const quillToWordConfig = {
    exportAs: 'blob'
};
const docAsBlob = await quillToWord.generateWord(delta, quillToWordConfig);
saveAs(docAsBlob, 'word-export.docx');

How Can I Configure QuillToWord?

QuillToWord supports two configurable options: export configuration and style configuration.

Export Configuration

By default, QuillToWord will return a docx object from a call to generateWord(). QuillToWord is also capable of returning a Blob, a Buffer, or a base64 string. To obtain one of these alternative objects, pass a configuration object as the second argument to generateWord(). See the example below.

const quill_delta = quillInstance.getContents();
const configuration = {
    exportAs: 'blob' // could also be 'buffer', 'base64', or 'doc'
}

const docx_blob = await quillToWord.generateWord(quill_delta, configuration); // returns Promise<Blob>

Style Configuration

As you are likely aware, Microsoft Word documents enable users to specify style formats for various types of text within a document. For instance, specific fonts, sizes, and spacing can be set for headings, normal text, block quotes, and so forth.

QuillToWord is prepackaged with default styles for several types of text commonly used within a quill editor: normal, heading 1, heading 2, lists, code blocks, and block quotes. If you prefer to specify your own styling for these types of text, you can pass a configuration object as the second argument to generateWord(). The configuration object should satisfy the following interface:

interface Configuration {
    paragraphStyles: {
        normal?: {  // this is the name of the text type that you'd like to style
            paragraph?: {
                spacing?: {
                    line?: number;
                    before?: number;
                    after?: number;
                },
                alignment?: AlignmentType // from docx package
                indent?: {
                    left?: number;
                    hanging?: number;
                    right?: number;
                }
            },
            run?: {
                font?: string;
                size?: number;
                bold?: boolean;
                color?: string; // as hex value e.g., ffaaff
                underline?: {
                    type?: UnderlineType; // from docx package
                    color?: string // just use 'auto'
                }
                italics?: boolean;
                highlight?: string; // must be named values accepted by Word, like 'yellow'
            }
        }
    }
}

Note that all of the same properties shown for normal here can be set for the following: heading_1, heading_2, list_paragraph, code_block, and block_quote.

Also, be aware that most of the formats are based on the normal format. As a result, modifying the normal format could cause changes in the other formats as well.

Now, let's see an example of configuring the header format. The configuration object in the example below will override the default styling for heading level one.

const quill_delta = quillInstance.getContents();
const config: Config = {
    paragraphStyles: {
        header_1: {
            paragraph: {
                spacing: {
                    before: 1200,
                    after: 1000
                }
            },
            run: {
                size: 64,
                bold: true,
                color: 'ff88bb'
            }
        }
    }
};
const doc = await quillToWord.generateWord(quill_delta, config);
1.3.0

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.30

3 years ago

0.0.29

3 years ago

0.0.28

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.25

3 years ago

0.0.24

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.10

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago