Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDFKit doesn't work with ESM #1491

Open
laszlo1337 opened this issue Jan 15, 2024 · 5 comments
Open

PDFKit doesn't work with ESM #1491

laszlo1337 opened this issue Jan 15, 2024 · 5 comments

Comments

@laszlo1337
Copy link

laszlo1337 commented Jan 15, 2024

Bug Report

Description of the problem

In modern projects that rely on ES modules PDFKit cannot work due to missing __dirname global variable.
During development of my SvelteKit app I could use PDFKit library, but once in production mode the app will crash with:

ReferenceError: __dirname is not defined
    at Object.Helvetica (file:///xxx/app/build/server/chunks/_server-9e0bc332.js:89564:37)      
    at new StandardFont (file:///xxx/app/build/server/chunks/_server-9e0bc332.js:89609:56)      
    at Function.open (file:///xxx/app/build/server/chunks/_server-9e0bc332.js:89997:18)         
    at PDFDocument.font (file:///xxx/app/build/server/chunks/_server-9e0bc332.js:90071:33)      
    at PDFDocument.initFonts (file:///xxx/app/build/server/chunks/_server-9e0bc332.js:90034:12) 
    at new PDFDocument (file:///xxx/app/build/server/chunks/_server-9e0bc332.js:93049:11)       
    at file:///xxx/app/build/server/chunks/_server-9e0bc332.js:93533:17  

Code sample

import PDFDocument from 'pdfkit'

export async function POST({ request, locals }) {
// endpoint code
		const pdf = new PDFDocument({ size: [totalSize, totalSize] })

		pdf.moveTo(0, 0)
// rest of the code
}

Your environment

  • pdfkit version: 0.14.0
  • Node version: 18.12.0
  • Browser version (if applicable): none
  • Operating System: Windows 10
@lramos15
Copy link

I recommend just downloading a font from somewhere and passing it to the constructor.

  const doc = new PDFDocument({
    font: path.join('./app/fonts/Satoshi-Black.ttf')
  });

This allowed me to at least be unblocked on using this library since there's not really any other good options.

@laszlo1337
Copy link
Author

I just switched to pdf-lib. It works.

@manuel-en-arg
Copy link

I recommend just downloading a font from somewhere and passing it to the constructor.

  const doc = new PDFDocument({
    font: path.join('./app/fonts/Satoshi-Black.ttf')
  });

This allowed me to at least be unblocked on using this library since there's not really any other good options.

This "hack" from @lramos15 worked for me. Its not the best solution but way better than no solution. I was already getting crazy with this problem. Thank you very much for this! Hope this library gets a support or workaround for ESM someday..

@lramos15
Copy link

lramos15 commented Feb 1, 2024

I settled on https://pdfme.com/ in case people are looking for other suggestions too. It's more of a template filler than a full blown pdf kit but works decently well and is updated

@blikblum
Copy link
Member

For node, currently it does not work with esm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants