Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

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

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.   doc.pipe(fs.createWriteStream("output.pdf"))

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?

Solved Solved
0 2 1,332
1 ACCEPTED SOLUTION

Hello @devVL!

Welcome to the Google Cloud Community!

The reason why you're getting the error "no such file or directory" is because you are trying to access or write into a directory that doesn't exist in the context of Cloud Function's execution environment

The error: Error: ENOENT: no such file or directory, open '/home/companynamehere/thefunctionnamehere/output.pdf'

 This works in the terminal because you are executing it from a local. Meanwhile, Cloud Function is a serverless environment so it doesn't know what directory you are referring to. 

Take a look at this Stack Overflow post for additional explanation as well as a workaround for your issue.

If the above options don't work, you can contact Google Cloud Support to further look into your case. Let me know if it helped, thanks!

View solution in original post

2 REPLIES 2

Hello @devVL!

Welcome to the Google Cloud Community!

The reason why you're getting the error "no such file or directory" is because you are trying to access or write into a directory that doesn't exist in the context of Cloud Function's execution environment

The error: Error: ENOENT: no such file or directory, open '/home/companynamehere/thefunctionnamehere/output.pdf'

 This works in the terminal because you are executing it from a local. Meanwhile, Cloud Function is a serverless environment so it doesn't know what directory you are referring to. 

Take a look at this Stack Overflow post for additional explanation as well as a workaround for your issue.

If the above options don't work, you can contact Google Cloud Support to further look into your case. Let me know if it helped, thanks!

I created the file in google storage bucket and everything works fine. Thank you!

Top Solution Authors