kb47 / pdf-poppler

Convert PDF files into images using Poppler with promises. It achieves 10x faster performance compared to other PDF converters.
MIT License
47 stars 20 forks source link

Getting a libcairo error on MacOS 12.6 on M1 #19

Open RobKohr opened 1 year ago

RobKohr commented 1 year ago

This is still happening after running brew install cairo:

node:internal/errors:841
  const err = new Error(message);
              ^

Error: Command failed: /Users/robertkohr/projects/bingo-pdfkit/node_modules/pdf-poppler/lib/osx/poppler-0.66/bin/pdftocairo -png -f 1 -l 1 -scale-to 1024 sets/language/animals/languages/uk/output/uk-animals-callSheet-LETTER.pdf sets/language/animals/languages/uk/callSheet
dyld[67611]: Library not loaded: '/usr/local/opt/cairo/lib/libcairo.2.dylib'
  Referenced from: '/Users/robertkohr/projects/bingo-pdfkit/node_modules/pdf-poppler/lib/osx/poppler-0.66/bin/pdftocairo'
  Reason: tried: '/usr/local/opt/cairo/lib/libcairo.2.dylib' (no such file), '/usr/local/lib/libcairo.2.dylib' (no such file), '/usr/lib/libcairo.2.dylib' (no such file)
martin0258 commented 1 year ago

@RobKohr

I figured out a workaround. In short, replace the outdated bin pdftocairo of x86_64 arch (not working for M1) with M1's.

  1. Remove the outdated bin rm /Users/robertkohr/projects/bingo-pdfkit/node_modules/pdf-poppler/lib/osx/poppler-0.66/bin/pdftocairo
  2. Point the old path to the new bin ln -s -f /opt/homebrew/bin/pdftocairo /Users/robertkohr/projects/bingo-pdfkit/node_modules/pdf-poppler/lib/osx/poppler-0.66/bin/pdftocairo
  3. It should work now 🎉

If this does not work, please check if the paths are correct. Or maybe you just haven't installed poppler for M1, which means /opt/homebrew/bin/pdftocairo does not exist.

IMO: the long-term solution should be making a PR to dynamically pick the right pdftocairo for different runtime env (arm vs x86), so that we don't need to do the workaround every time we npm install pdf-poppler on M1. However, I'm afraid the author doesn't maintain the package anymore (since there's no update since 4 years ago)?

brew install cairo won't fix the issue, because it installed M1-based lib to /opt/homebrew/lib/libcairo.2.dylib which is not compatible with x86_64-based pdftocairo. Even you point the lib with a soft link, it would show an runtime error (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64').