marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.55k stars 545 forks source link

How to pass data to html before export to pdf #511

Open JohnSalard opened 5 years ago

Aathi commented 5 years ago

@JohnSalard You could do something like this


const AWS = require("aws-sdk");
const pdf = require("html-pdf");
const uuidv4 = require('uuid/v4');
const htmlTemplate = require('./templates/supplierOrderListTemplet')

const options = {
    format: "A4",
    orientation: "portrait",
    header: {
        height: "10mm"
    },
    footer: {
        height: "10mm"
    }
};

const htmlTemplate = (order, allSupplierProducts, customer) => {
return `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    // Your code goes here
</body>
</html>
`
}

AWS.config.update({
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_KEY
});

module.exports = {
    createPdf: (order, allSupplierProducts, customer, callback) => {
        pdf
            .create(htmlTemplate(order, allSupplierProducts, customer), options)
            .toBuffer((err, buffer) => {
                if (err) {
                    console.log("Error: ", err);
                } else {
                    const s3 = new AWS.S3();
                    const fileName = `${uuidv4()}.pdf`;
                    const params = {
                        Bucket: "aws-bucket",
                        Key: fileName,
                        Body: buffer,
                        ContentType: "application/pdf"
                    };
                    s3.putObject(params, (err, data) => {
                        if (err) console.log("Function Error: ", err);
                        return callback(data);
                    });
                }
            });
    }
};
sanjay147654 commented 5 years ago

please specify where you want to store the data.If you want to store it on some cloud storage service like s3,you can follow @Aathi instructions.If you want to save it to local storage,do something like this

const pdf = require('html-pdf');

var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) { if (err) return console.log(err); console.log(res); // { filename: '/app/businesscard.pdf' } });

here html is the html string you want to convert to pdf.you can use @Aathi answer to generate a html string with your own params

Serhioromano commented 5 years ago

@Aathi in your example you define with one parameter

htmlTemplate = (params)

but you call it with 3

htmlTemplate(order, allSupplierProducts, customer)

How that works?

deepansh946 commented 4 years ago

@Serhioromano It should be

htmlTemplate({ order, allSupplierProducts, customer })
Aathi commented 4 years ago

@Serhioromano , sorry it was my bad. it was a quick example so I didn't notice. it has been updated now. or you could do like how @deepansh946 has suggested, pass as an object to the respective function and destructure it

Serhioromano commented 4 years ago

@Aathi suggestion of @deepansh946 is not valid JS

htmlTemplate({ order, allSupplierProducts, customer })

There is no such syntax for objects.

Also in your code, you create

const htmlTemplate = require('./templates/supplierOrderListTemplet')

and then you define this constant again after options.

deepansh946 commented 4 years ago

@Serhioromano You can check this sandbox: https://codesandbox.io/s/javascript-nscdr

Aathi commented 4 years ago

@Serhioromano Please check this es6 basic destructuring assignment syntax is a JavaScript in order to understand how it works. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/

https://codeburst.io/es6-destructuring-the-complete-guide-7f842d08b98f

https://exploringjs.com/es6/ch_destructuring.html

Serhioromano commented 4 years ago

I see. ES6.