#ENOENT Error with puppeteer when creating .pdf

9 messages · Page 1 of 1 (latest)

hollow acorn
#

I am using puppeteer in my express server app to create a .pdf file from a html template. It works fine locally and throws this error when I upload the server app to railway.
(from railway deploy log):

tempPath /app/create-invoice/temp/invoice_pi_3NERfyItN31KXN7T2Os3CniD.pdf
[Error: ENOENT: no such file or directory, open '/app/create-invoice/temp/invoice_pi_3NERfyItN31KXN7T2Os3CniD.pdf'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/app/create-invoice/temp/invoice_pi_3NERfyItN31KXN7T2Os3CniD.pdf'
}

This is the code that I'm using in my node express server:

    getTemplateHtml().then(async (res) => {
    const template = hb.compile(res, { strict: true });
    const result = template(data);
    const html = result;
    const browser = await puppeteer.launch({
      headless: true,
      args: ['--no-sandbox', '--disable-setuid-sandbox'],
    });
    const page = await browser.newPage()
    // set the page content as the generated html by handlebars
    await page.setContent(html)
    //  use pdf function to generate the pdf
    const tempPath = path.join(__dirname, '.', 'temp', 'invoice_' + data.payment_intent + '.pdf')
    console.log('tempPath', tempPath)  
    await page.pdf({ path: tempPath, format: 'A4' })
    await browser.close();
    return true
      }).catch(err => {
        console.error(err)
      });
    }

The console.log('tempPath', tempPath) is the first line in the error message. The next line (pdf creation) seems to create the error.:

await page.pdf({ path: tempPath, format: 'A4' })

Help would be much appreciated.

rich lionBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

hexed pythonBOT
#

Project ID: N/A

hollow acorn
#

N/A

pastel ember
#

does temp exist?

#

if you dont have anything in it and checked it into git its likely git ignored that dir. you may need to add a .gitkeep in that directory with no contents or maybe try using /tmp/ instead of a local temp directory?

hollow acorn
#

Wow, you're 100% correct. It didn't exist in my git repo because it's empty. Never realized git would ignore empty folders. Thanks a lot @pastel ember !

pastel ember
rich lionBOT
#

Thank you for marking this question as solved!

Question Message ID

1114467774736121896

Solution Message ID

1114483635815464960