blikblum / pdfkit-webpack-example

Simple example of using PdfKit with webpack
25 stars 14 forks source link

registerAFMFonts not using default Helvetica font exports #2

Closed jarommadsen closed 5 years ago

jarommadsen commented 5 years ago

I was running into the same errors as this issue and was particularly confused about this error:

Uncaught (in promise) TypeError: Cannot read property 'split' of undefined
    at new AFMFont (pdfkit.es5.js:2453)
    at new StandardFont (pdfkit.es5.js:2700)
    at Function.open (pdfkit.es5.js:3096)
    at PDFDocument.font (pdfkit.es5.js:3170)
    at PDFDocument.initFonts (pdfkit.es5.js:3132)
    at new PDFDocument (pdfkit.es5.js:4890)
    at new PDFBuilder (PDFBuilder.js:42)
    at PDFCreator.createID (index.jsx:40)
    at PDFCreator.componentDidMount (index.jsx:72)

The proposed workaround was to do a direct import from raw-loader:

import Helvetica from '!!raw-loader!pdfkit/js/data/Helvetica.afm'

This was strange to me as removing the !!raw-loader! portion still seemed to work and furthermore adding !!raw-loader! to the require.context call did not fix the error.

Turns out you just needed to use the default export of the files to avoid the error:

fs.writeFileSync(`data/${match[0]}`, ctx(key).default);

Not sure if this was a pdfkit update or what but it'd be good to update that line.


"pdfkit": "^0.10.0" "webpack": "^4.39.2" "arraybuffer-loader": "^1.0.7" "raw-loader": "^3.1.0" "transform-loader": "^0.2.4"

blikblum commented 5 years ago

The raw-loader changed how data is exported see: https://github.com/webpack-contrib/raw-loader/pull/69

I updated the code accordingly