Cloud function: doc.pipe(fs.createWriteStream("output.pdf")) doesn't work with api request

47 Views Asked by At

I am using google cloud functions to run an api with node js. I am using pdfkit to create a pdf.

When I run node index.js (in the terminal) manually. The pdf gets generated. When I do the api request in the logs I can see the function run (the exact function which I test by typing node index.js) I can see the logs that it is going through all the steps but the file is not generated. 

const fs = require('fs');
const util = require('util');
const PDFDocument = require('pdfkit');


async function createPDFdoc() {
  console.log("PRINTING PDF");
  // create PDF doc
  const doc = new PDFDocument();
  console.log("1");
  //console.log(doc);
  doc.pipe(fs.createWriteStream('output.pdf')); // This is the line where I doubt 
 // something is wrong
  console.log("2");
  doc.font('fonts/Roboto-Black.ttf').fontSize(25).text('some text here',  {
    align: 'center'
  }); // (x,y)
  console.log("3");
  doc.end();
  console.log("PDF PRINTED");
}

createPDFdoc();

One more observation I have is if I put /home/companynamehere/functionnamehere/output.pdf

It work when I run it through terminal (node index.js)

But does not work and throws error when I run it through the api request

The error: Error: ENOENT: no such file or directory, open '/home/companynamehere/thefunctionnamehere/output.pdf' What could be the problem. How can I fix it?

One more observation I have is if I put /home/companynamehere/functionnamehere/output.pdf

inside

doc.pipe(fs.createWriteStream('output.pdf')); // like given below
doc.pipe(fs.createWriteStream('/home/companynamehere/functionnamehere/output.pdf'));

It works when I run it through terminal (node index.js)

But does not work and throws error when I run it through the api request

The error:

Error: ENOENT: no such file or directory, open '/home/companynamehere/thefunctionnamehere/output.pdf' What could be the problem. How can I fix it?

0

There are 0 best solutions below