I’m trying to generate html to pdf using html-pdf npm. Please check below code
const invoicePath = path.resolve(path.join(__dirname, ‘../../../views/1.hbs’));
const templateHtml = fs.readFileSync(invoicePath, ‘utf8’);
const template = hb.compile(templateHtml);
const result = template({ data: body });
const milis = new Date().getTime(); pdf.create(result).toFile(‘receipt.pdf’, function (err, res) {
if (err) {
console.log(err);
}
else {
console.log(‘res’, res);
const data = fs.readFileSync(res.url).toString(‘base64’);
}
});
Can you kindly advise if you are getting this error when working locally or when deploying into a Google Cloud Product. Are you using Docker when building your application?
I believe that you are using the html-pdf [1] package for your project. It could be that you are missing a package on your Dockerfile, can you kindly check the following post which had a similar issue and error [2]. Lastly, since you are using a third party package, we provide best effort support and guide you to any external resources that would provide assistance [3].
With Cloud Functions and App Engine, you can only write files under the in-memory temporary (“/tmp”) filesystem. May using os.tmpdir() - or equivalent, I’m not a Node.js dev - as the root folder solve your issue?
Based on what I have researched, the package itself uses the phantomjs-prebuilt which will then install phantomjs for you based on your OS/architecture. Additionally it is important that the node_modules doesn’t get copied over to the docker image but rather get installed on the docker file itself. On your .dockerignore file, can you try to add mode_modules/ to it.
Lastly, it seems that your post looks like a technical issue and considering the amount of information required to analyze it, it would be best to submit a tech case to the GCP support [1].