Skip to content

SofianD/from-data-to-pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Visit from-data-to-pdf.

Installation

$ npm i from-data-to-pdf

Usage

getPdf(targets, save, path?)

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

main();

async function main() {
    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'
    //     }
    // ]
}

fromHtmlFileToPdf(files, save, path?)

  • 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.

const dataToPdf = require("from-data-to-pdf");

main();

async function main() {
    const data = [
        {
            projectName: "Mon Fichier",
            fileName: "project1.html"
        }
    ];

    // If this is my first time using this package and I don't have a custom path to get templates.
    // I use the function below once:
    await dataToPdf.initDefaultFolder();
    // Now, I move my html files in the created folder: my-app/temp/target/
    // Then:
    const listOfSavedPDF = await dataToPdf.fromHtmlFileToPdf(data, true);
    // listOfSavedPDF:
    // [
    //     {
    //         name: 'Test',
    //         pathOfsavedFile: 'my-app/temp/generatedPDF/Mon-Fichier1561654165.pdf'
    //     }
    // ]


    // Else if I have custom path:
    const listOfSavedPDF = await dataToPdf.fromHtmlFileToPdf(data, true, {
        toGetFiles: 'C:/Users/Me/Documents/MyTemplates/',
        toSaveFiles: 'C:/Users/Me/Documents/MyPDF/'
    });
    // listOfSavedPDF:
    // [
    //     {
    //         name: 'Test',
    //         pathOfsavedFile: 'C:/Users/Me/Documents/MyPDF/mon-fichier1561654165.pdf'
    //     }
    // ]
}

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

Visit puppeteer.PDFOptions for more informations.

About

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

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published