Giter Club home page Giter Club logo

cpdfy's Introduction

cPDFY Logo

HTML to PDF converter with wkhtmltopdf native library. Run on both Win/Linux operating system.

cPDFY is not a precompiled library. It will be compiled from source code in your machine.
Before install cPDFY see node-gyp prerequisite from here

Windows prerequisite

npm install --global windows-build-tools

Linux OS prerequisite

Python v2.7, v3.5, v3.6, v3.7, or v3.8
sudo apt install make
sudo apt install g++
sudo apt install libfontconfig1 libxrender1

How to install ?

npm install cpdfy --save

Required For Linux OS

In Linux OS make sure libwkhtmltox.so.0 file exists in /usr/lib/. If libwkhtmltox.so.0 not found in /usr/lib/, please copy it from /node_module/cpdfy/lib/**/libwkhtmltox.so.0 to /usr/lib/.

How to use it ?

/*import cPdfy instance*/
const { Cpdfy } = require('cpdfy');

Working with stream

01# This example create pdf from url

Cpdfy.createStream({ from_url: "https://wkhtmltopdf.org/" }, (err, stream) => {
    if (err) {
        console.log(err);
        return;
    }
    const fst = fs.createWriteStream(path.resolve(`./test_output/1_test_${Math.floor((0x999 + Math.random()) * 0x10000000)}.pdf`));
    stream.pipe(fst);
});

02# This example create pdf from html string

const html = fs.readFileSync('./test_output/test.html', { encoding: "utf-8" }).replace(/^\uFEFF/, '');
Cpdfy.createStream(html, (err, stream) => {
    if (err) {
        console.log(err);
        return;
    }
    const fst = fs.createWriteStream(path.resolve(`./test_output/2_test_${Math.floor((0x999 + Math.random()) * 0x10000000)}.pdf`));
    stream.pipe(fst);
});

03# This example create pdf from html string and write to fs.WriteStream

const file = fs.createWriteStream(path.resolve(`./test_output/3_test_${Math.floor((0x999 + Math.random()) * 0x10000000)}.pdf`));
Cpdfy.createStream(file, html, (err) => {
    if (err instanceof Error) {
        console.log(err);
    } else {
        console.log("Done...");
    }
});

04# This example create pdf from string with ICPdfConfig and write to WriteStream

const html = `
<!DOCTYPE html>
    <html lang="es">
    <head>
        <title>Test PDF</title>
    </head>
    <body>
        <h1 style="color:red;">Hello World....</h1>
    </body>
</html>
`;
const file4 = fs.createWriteStream(path.resolve(`./test_output/4_test_${Math.floor((0x999 + Math.random()) * 0x10000000)}.pdf`));
Cpdfy.createStream(file4, {
    global_settings: {
        documentTitle: "This is printed copy",
        orientation: "Landscape",
        size: {
            paperSize: "Legal"
        },
        margin: {
            top: "1.27cm",
            bottom: "1.27cm",
            left: "1.27cm",
            right: "1.27cm",
        }
    }
}, html);

05# This example create pdf from url and write to ServerResponse

controller.get('/pdf-test', (ctx, match) => {
    const url = help.mayBeString(ctx.req.query["url"]);
    if (!url) {
        return ctx.res.status(200).type("text").send("Invalid request...");
    }
    ctx.res.status(200).noCache();
    Cpdfy.createStream(ctx.res, { from_url: decodeURIComponent(url) }, (err) => err && ctx.handleError(err, () => { }));
});

06# This example create pdf from url and write to ServerResponse with callback

controller.get('/pdf', (ctx, match) => {
    const url = help.mayBeString(ctx.req.query["url"]);
    if (!url) {
        return ctx.res.status(200).type("text").send("Invalid request...");
    }
    Cpdfy.createStream({ from_url: decodeURIComponent(url) }, (err, stream) => {
        ctx.handleError(err, () => {
            Cpdfy.setHeader(ctx.res);
            ctx.res.status(200).noCache().type("pdf");
            stream.pipe(ctx.res);
        });
    });
});

07# This example create pdf from url and write to ouput path

Cpdfy.generatePdf({ from_url: "https://wkhtmltopdf.org/", out_path: path.resolve('./from_url.pdf') });

08# This example create pdf from url and return Buffer

Cpdfy.generatePdf({ from_url: "https://wkhtmltopdf.org/" });

Note: This version supports win32/win64/linux64

cpdfy's People

Contributors

rajibchy avatar ovi99 avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.