Skip to content

Using custom fonts with fabric in NodeJS #7065

Answered by syarkhan
ilyes24 asked this question in Q&A
Discussion options

You must be logged in to vote

You need to use canvas.registerFont to load all the fonts first and then init your canvas on nodejs and generate base64 image.

But registerFont uses file path directory to load the fonts, if your fonts are google fonts or ttf/otf files saved somewhere, you need to download all the files first and save it locally and then use registerFont, after that when you have generated base64 image delete all those fonts saved locally by calling fs.unlinkSync(filePath);

  1. loop through fonts in json
  2. download fonts and save it locally
  3. call fabric.nodeCanvas.registerFont(file.path, {family: fontName, weight: fontWeight, style: fontStyle})
  4. init static canvas
  5. load json to static canvas
  6. call toDataURL
  7. delete…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ilyes24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants