-
Notifications
You must be signed in to change notification settings - Fork 14
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
File 'data/Helvetica.afm' not found in virtual file system #1
Comments
What version of pdfkit are you using? Try: import Helvetica from 'pdfkit/js/data/Helvetica.afm'
import fs from 'fs'
//some debug:
console.log(Helvetica)
console.log(typeof Helvetica)
fs.writeFileSync('data/Helvetica.afm', Helvetica) |
I'm using the same version as in your example: pdfkit v.0.9.1 I've tried your snippet with the following results: console.log(Helvetica)
// Output: /static/media/Helvetica.d6455828.afm
console.log(typeof Helvetica)
// Output: string The error changed to:
In comparison to the last one, now the error is happening while trying to create the AFMFont invoked by StandardFont. This line from the source causes the error: this.bbox = this.attributes['FontBBox'].split(/\s+/).map(function (e) {
return +e;
}); The font is still not inlined... /***/ "./node_modules/pdfkit/js/data/Helvetica.afm":
/*!***************************************************!*\
!*** ./node_modules/pdfkit/js/data/Helvetica.afm ***!
\***************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__.p + "static/media/Helvetica.d6455828.afm";
/***/ }), |
Some other loader is taking precedence over raw-loader. From raw-loader docs try:
|
|
Thanks for your quick response. I think we are now on good way! I did some research in the code: class AFMFont {
static open(filename) {
return new AFMFont(fs.readFileSync(filename, 'utf8')); // this normally should return the raw content
}
constructor(contents) {
this.contents = contents;
this.attributes = {};
this.glyphWidths = {};
this.boundingBoxes = {};
this.kernPairs = {};
this.parse(); // here the raw content should be parsed and set to the properties above
this.charWidths = new Array(256);
for (let char = 0; char <= 255; char++) {
this.charWidths[char] = this.glyphWidths[characters[char]];
}
this.bbox = this.attributes['FontBBox'].split(/\s+/).map(e => +e); // this fails because FontBBox is undefined, attributes must be empty
// ...
} So why does webpack not forward the raw source to the static opener? I'm really stuck, any ideas? |
The config added by CRA is somehow interfering
No
No. Because would work with direct import
No. It should display the afm file content:
Try:
An then run webpack with --display-modules option should get something like: |
Sorry for the late answer. |
Hi @thetw I am facing the same problem. Could you please explain above sentence. |
@GO-gyan You don't need to use the import fs from 'fs'
import Helvetica from '!!raw-loader!pdfkit/js/data/Helvetica.afm'
fs.writeFileSync('data/Helvetica.afm', Helvetica) This will add the raw content of the .afm font to the Virtual file system of PDFKit. Please mention, that you have to register a custom font in order to make another fonts working with PDFKit. I did this with fetching an external font file in order to keep bundle size small and then passing the response ArrayBuffer to the |
Thanks for your quick reply. It is working fine now. |
Hi, I am also facing this issue and have little experience with Webpack. My setup uses CRA and Apologies for my lack of understanding and thank you for the content so far - the work around has helped greatly. |
@wworrall Make certain that the raw-loader and all other loaders are placed above the |
Just a heads up for people who struggle to implement
By doing it this way, you will avoid having problems with server-side rendering. Thanks everyone, hope it helps. |
i have problem to find file to put above code, and never use the i just import
|
You must use webpack. See the code in this repo |
BTW: this repository is for pdfkit, not pdfmake |
I went through this problem and now I am trying to add my custom font. What I tried is adding the font with fs, just like the Helvetica one, but from a local folder. It obviously didn't work - the error I got was: I know I have to use arrayBuffer somehow, but I have no idea what I should do. Please help! EDIT: |
If somebody comes here with this issue on AWS Lambda and your error looks like |
I managed to solve this properly for I used CRACO and the following const webpack = require("webpack");
module.exports = {
webpack: {
configure: (webpackConfig, { env, paths }) => {
// Modifying webpack.module.rules with CRACO is only possible using the `configure` function,
// not using the `configure` object literal.
// See supported config for webpack: https://craco.js.org/docs/configuration/webpack/
webpackConfig.module.rules = [
...webpackConfig.module.rules,
// bundle and load afm files verbatim
{ test: /\.afm$/, type: 'asset/source' },
// bundle and load binary files inside static-assets folder as base64
{
test: /src[/\\]static-assets/,
type: 'asset/inline',
generator: {
dataUrl: content => {
return content.toString('base64');
},
},
},
// load binary files inside lazy-assets folder as an URL
{
test: /src[/\\]lazy-assets/,
type: 'asset/resource'
},
// convert to base64 and include inline file system binary files used by fontkit and linebreak
{
enforce: 'post',
test: /fontkit[/\\]index.js$/,
loader: 'transform-loader',
options: {
brfs: {}
},
},
{
enforce: 'post',
test: /linebreak[/\\]src[/\\]linebreaker.js/,
loader: 'transform-loader',
options: {
brfs: {}
},
},
];
webpackConfig.resolve.alias = {
...webpackConfig.resolve.alias,
// maps fs to a virtual one allowing to register file content dynamically
fs: 'pdfkit/js/virtual-fs.js',
// iconv-lite is used to load cid less fonts (not spec compliant)
'iconv-lite': false,
};
webpackConfig.resolve.fallback = {
...webpackConfig.resolve.fallback,
// crypto module is not necessary at browser
crypto: false,
// fallbacks for native node libraries
process: require.resolve("process/browser"),
zlib: require.resolve("browserify-zlib"),
stream: require.resolve("stream-browserify"),
util: require.resolve("util"),
buffer: require.resolve("buffer"),
assert: require.resolve("assert"),
};
webpackConfig.plugins = [
...webpackConfig.plugins,
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
process: "process/browser",
}),
];
return webpackConfig;
}
}
} Then I followed the webpack example, copied the |
in next.js I have an issue
|
Commenting here in case it helps anyone else. I was having the same problem and for me it boiled down to this line in my webpack.config.js:
this was the result of me copying and pasting webpack configs from moldy blogs, documentation, or stack overflow threads that warned:
However, in modern webpack this is no longer necessary, and even used to throw an error warning you not to do that anymore (webpack/webpack#3043) (webpack migration notes) in modern webpack versions, it seems the empty string in resolve.extensions can cause errors like these and afaik you can just remove it and it should all work 👍 |
I've used your webpack example to extend my create-react-app setup.
I've added your rules to my webpack.config.js but there must a problem with the asset inlining.
in fact this does nothing in my generated webpack chunk:
{ loader: 'raw-loader', test: /\.afm$/ }
this is applied to the webpack output, which indicates that this a problem on the loader side and not a problem with your
registerAFMFonts
:But when i double check the chunk contents there are no inlined glyphs.
That's a pity because i really appreciated the approach with kicking out all the unnecessary embedded fonts. In fact everything up the stack trace works well until the PDFDocument constructor tries to load the standard Helvetica font file, then the app crashes with an uncaught error...
The only thing i modified is the pattern which chooses the font variants. I only want the standard Helvetica to be inlined, like this:
Here's the trace:
Any ideas how to ensure that the file will be inlined into the chunk or safely referenced in the virtual file system?
Cheers and thx in advance
The text was updated successfully, but these errors were encountered: