Skip to content
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

Documentation: Missing information on font integration #429

Open
vresetnikov opened this issue Mar 24, 2023 · 1 comment
Open

Documentation: Missing information on font integration #429

vresetnikov opened this issue Mar 24, 2023 · 1 comment

Comments

@vresetnikov
Copy link

vresetnikov commented Mar 24, 2023

Trying to use JsBarcode on the backend and the documentation's font section is proving to be inadequate. There is very little written about fonts, especially on how to use them in the backend and what little there is is ambiguous:

Define the font used for the text in the generated barcode. This can be any default font or a font defined by a [@font-face](https://developer.mozilla.org/en/docs/Web/CSS/@font-face) rule.

Considering that the environment is backend, I am assuming the @font-face part can be immediately discarded in this case. So with only 1 sentence remaining mentioning the fonts, the following questions emerge:

  1. What default fonts? Apart from a couple non-standard font strings shown and the monospace font indicated as the default, they are not listed nor is it shown how to integrate them. The default one, monospace, will fail to load on Linux systems by the way.
  2. Using what little information there is, I am trying to integrate Helvetica font and I am getting the following warning:

Pango-WARNING **: 12:38:24.590: couldn't load font "Helvetica Bold Not-Rotated 40px", falling back to "Sans Bold Not-Rotated 40px", expect ugly output.

@apss-pohl
Copy link

Hi,
i am also struggling with the font. Did you find a solution?
I want to add it to the svg and use it but is has no effect. My code looks like this:

  const xmlSerializer = new XMLSerializer();
  const document = new DOMImplementation().createDocument('http://www.w3.org/1999/xhtml', 'html');
  const styleElement = document.createElement('style');
  
  const location =
    '/public/fonts/DejaVuSansMono.ttf';
  const contents = fs.readFileSync(location, { encoding: 'base64' });

  // Append the style element to the document's head
  const headElement = document.createElement('head');
  document.documentElement.appendChild(headElement);
  headElement.appendChild(styleElement);

  styleElement.textContent =
    "@font-face { font-family: 'DejaVuSansMono'; src: url(data:font/ttf;base64," +
    contents +
    ") format('truetype'); }";

  const svgNode = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  const defaultOptions = {
    xmlDocument: document,
    height: 30,
    displayValue: false,
    margin: 0,
    fontSize: 14,
    font: 'DejaVuSansMono',
  };
  options = { ...defaultOptions, ...options };

  JsBarcode(svgNode, barcodeText, options);

  return 'data:image/svg+xml;base64,' + btoa(xmlSerializer.serializeToString(svgNode));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants