ENOENT Error with puppeteer when creating .pdf

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'
}
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)
});
}
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' })
await page.pdf({ path: tempPath, format: 'A4' })
Help would be much appreciated.
Solution:
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?
Jump to solution
7 Replies
Percy
Percy2y ago
Project ID: N/A
erztemplerobba
erztemplerobbaOP2y ago
N/A
luna
luna2y ago
does temp exist?
Solution
luna
luna2y ago
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?
erztemplerobba
erztemplerobbaOP2y ago
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 @ImLunaHey !
luna
luna2y ago
remember to mark this as correct 🙂
erztemplerobba
erztemplerobbaOP2y ago
Done, cheers! 😄
Want results from more Discord servers?
Add your server