foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.8k stars 1.14k forks source link

I keep getting "Error: ENOENT: no such file or directory" regarding default font files while trying to use pdfkit in a lambda function. #1441

Open Wet-Bones opened 1 year ago

Wet-Bones commented 1 year ago

When I try to generate a pdf, my console returns:

ERROR Error: ENOENT: no such file or directory, open 'C:\Users\Me\Project\.sst\artifacts\dev-app-ApiStack-api-Lambda_POST_-doc\services\functions/data/Helvetica.afm'

The only thing I've specified in the code as far as font is to use Helvetica and Helvetica-Bold at certain locations. Does the issue have to do with my __dirname?

RobCabrera commented 1 year ago

@Wet-Bones

Try using the environment variable in lambda - ${process.env.LAMBDA_TASK_ROOT} . It gives you the path to your lambda function.

Source: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

Example: const logo = ${process.env.LAMBDA_TASK_ROOT}/pdfhandler/assets/logo.png;

SimtecConsult commented 1 year ago

I'm getting the same error trying to generate a new PDF in a NextJS 13 API route. Did you ever find a resolution to this issue?

Wet-Bones commented 1 year ago

I'm getting the same error trying to generate a new PDF in a NextJS 13 API route. Did you ever find a resolution to this issue?

So I ended up downloading fonts as ttf files and storing them in the project directory. In order for them to be deployed properly, you have to configure that API route to copy the files to your assets bucket. The nodejs format must also be in cjs I believe.

SimtecConsult commented 1 year ago

That's good to know, I ended up putting the API route under the pages directory and it seems to work.

Finkes commented 11 months ago

I'm getting the same error trying to generate a new PDF in a NextJS 13 API route. Did you ever find a resolution to this issue?

Same here... any ideas how to resolve this? Switching to pages is not an option for me.

laszlo1337 commented 8 months ago

This is probably the same problem as in my case: https://github.com/foliojs/pdfkit/issues/1491 and it's due to PDFKit not being compatible with ESM