question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

custom font for svg text,tspan

See original GitHub issue

I wrote following code to support cyrillic and chinese characters when using doc.text

    const robotoRegularBase64 = await import('./roboto.ttf'); // with webpack url-loader
    const robotoRegular = b64arraybuffer.decode(
      robotoRegularBase64.split('data:font/ttf;base64,')[1]
    ); // turn base64 into arraybuffer

    doc.registerFont('Roboto-Regular', robotoRegular); // pass roboto regular as arraybuffer
    doc.font('Roboto-Regular'); // use the font

This works quite fine when using doc.text, but I would also like the svg elements to use that font, what is the recommended way of doing this? Maybe incuding css/style? <text> and <tspan> elements don’t seem to use the doc.font defined font family by default.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
alafrcommented, Mar 8, 2018

If you want to use the same font for the whole svg it’s quite simple: SVGtoPDF(doc, svg, x, y, { fontCallback: () => 'Roboto-Regular' });

2reactions
akaleeroycommented, Apr 17, 2018

To support several fonts

To convert your.svg first put all the fonts you’re going to need in a path like ./fonts

function listFonts(dir = 'fonts/') {
  return Array.from(fs.readdirSync(dir).filter(file => /\.ttf$/.test(file)));
}
const availableFonts = listFonts();

const doc = new PDFDocument();
const svg = fs.readFileSync('your.svg', 'utf-8');

SVGtoPDF(doc, svg, 0, 0, {
  fontCallback: (family, bold, italic, fontOptions) => {
    // => ex 'Montserrat Light', false, false, { fauxItalic: false, fauxBold: false }
    // Normalize font-family string
    let face = family.replace(/["']/g, '').replace(/-/g, ' ');
    if (bold && italic) face = `${face} Bold Italic`;
    if (bold) face = `${face} Bold`;
    if (italic) face = `${face} Italic`;
    let match = availableFonts.find(fontObj => {
      let re = new RegExp(`${face}( Regular)?$`);
      return re.test(fontObj.fullName);
    });
    if (match !== undefined) {
      doc.registerFont(face, match.filePath);
      return face;
    } else {
      return 'Helvetica';
    }
  },
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

font-family - SVG: Scalable Vector Graphics - MDN Web Docs
The font-family attribute indicates which font family will be used to render the text, specified as a prioritized list of font family names...
Read more >
How to specify font-family in SVG - Stack Overflow
1. Generate the embedded font url as base64 ... Download the font file you want to use under .ttf extension. We will need...
Read more >
How do I use a custom font in an SVG image on my site?
Show activity on this post. I created a custom Facebook icon using Inkscape and saved it as an svg. It uses the same...
Read more >
Using Fonts in SVG - Vecta.io
The usage is done by embedding glyph information into SVG when rendered. SVG fonts are defined using <font> element. <font id="Font1" horiz ...
Read more >
SVG `text` and Small, Scalable, Accessible Typographic ...
but it even works for SVG-as-<img> (or background-image , presumably) as well, as long as the font-family references the custom font by the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found