from-data-to-pdf
TypeScript icon, indicating that this package has built-in type declarations

1.0.18 • Public • Published

from-data-to-pdf

This library converts html files to PDF files or PDF buffer.

Is working for:

  • URL.
  • local html files.
  • html as string.
  • text.

Installation

$ npm i from-data-to-pdf

Usage

getPdf(targets: FileBuffer, save: string, path?: string): Promise<FileBuffer[]>

  • targets:
  • save:
    • type boolean.
    • true to save targets, false to get buffers.
  • path:
    • is optionnal.
    • type string.
async function main() {
    const dataToPdf = require("from-data-to-pdf");

    const data =[
        {
            name: 'Google',
            url: 'https://www.google.com'
        },
        {
            name: 'String of html',
            text: '<html string>'
        }
    ];

    const listOfSavedFiles = await dataToPdf.getPdf(data, true, 'C:/Users/Me/Documents/MyPDF/');

    console.log(listOfSavedFiles);
    // Display:
    // [
    //     {
    //         name: 'Google',
    //         pathOfsavedFile: 'C:/Users/Me/Documents/MyPDF/google15156514.pdf'
    //     },
    //     {
    //         name: 'String of html',
    //         pathOfsavedFile: 'C:/Users/Me/Documents/MyPDF/string-of-html15156515.pdf'
    //     }
    // ]
}
main();

fromHtmlFileToPdf(files: HTMLTarget, save: boolean, path?: Path): Promise<FileBuffer[]>

  • files:
  • save:
    • type boolean.
    • true to save targets, false to get buffers.
  • path:
    • is optionnal.
    • type Path.
    • /!\ relative path starts from the app directory.

CSS must be in your html files.

async function main() {
    const dataToPdf = require("from-data-to-pdf");

    // If I don't have custom path:
    await dataToPdf.initDefaultFolder();
    // Now, I move my html files in the created folder: my-app/temp/target/project1.html
    // Then...

    const data = [
        {
            projectName: "Mon Fichier",
            fileName: "project1.html"
        }
    ];

    const listOfSavedPDF = await dataToPdf.fromHtmlFileToPdf(data, true, {
        toGetFiles: 'C:/Users/Me/Documents/MyTemplates/',
        toSaveFiles: 'C:/Users/Me/Documents/MyPDF/'
    });

    console.log(listOfSavedPDF);
    // Display:
    // [
    //     {
    //         name: 'Test',
    //         pathOfsavedFile: 'C:/Users/Me/Documents/MyPDF/mon-fichier1561654165.pdf'
    //     }
    // ]
}

main();

Models

HTMLTarget

export interface HTMLTarget {
    projectName: string;
    fileName: string;
    pdfOptions: PdfOptions;
}

FileBuffer

export interface FileBuffer {
    name: string;
    url?: string;
    text?: string;
    buffer?: Buffer;
    options?: puppeteer.PDFOptions;
    pathOfsavedFile?: string;
    error?: any;
}

Path

export interface Path {
    toGetFiles?: string;
    toSaveFiles?: string;
}

puppeter.PDFOptions

Check puppeteer.PDFOptions for more informations.

Package Sidebar

Install

npm i from-data-to-pdf

Weekly Downloads

1

Version

1.0.18

License

ISC

Unpacked Size

351 kB

Total Files

22

Last publish

Collaborators

  • sofiand