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.

How can I use custom font in Angular 6?

See original GitHub issue

Hi, For more than 6 weeks I try to install this in my Angular project. I started 2 attempts but did not get any further with both. 1.) I worte a default_vfs.js file with: (function (jsPDFAPI) { "use strict"; jsPDFAPI.addFileToVFS('PTSans-Regular.ttf', base64 encoded ttffile); } I save it in src/assests/js My problem here with is unknown, how I integrate this file into Angular and how I must call these, so that the fonts are loaded.

2.) My 2 idea is to create a service that inserts the fonts into the document. `import * as jsPDF from ‘jspdf’;

@Injectable() export class JsPDFFontService { AddFont(doc: jsPDF) { doc.addFileToVFS(‘PTSans-Regular.ttf’, base64); } }` Here I unfortunately get the message “addFileToVFS is not present in the type jsPDF”.

Can someone help me here?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
IngoMantheycommented, Jan 23, 2019

Hi, here the code: import { Injectable } from ‘@angular/core’;

import * as jsPDF from ‘jspdf’;

@Injectable() export class JsPDFFontService { regularNormal = ‘font in base64’; regularBold = ‘font in base64’; AddFontArimo(doc: jsPDF) { (doc as any).addFileToVFS(‘arimo.regular-normal.ttf’, this.regularNormal); (doc as any).addFileToVFS(‘arimo.regular-bold.ttf’, this.regularBold); doc.addFont(‘arimo.regular-normal.ttf’, ‘arimo’, ‘normal’); doc.addFont(‘arimo.regular-bold.ttf’, ‘arimo’, ‘bold’); doc.setFont(‘arimo’); } }

in my component import { JsPDFFontService } from ‘…/…/tip-services/jsPdf-font.service’; import * as jsPDF from ‘jspdf’;

export class printTest {}

doc: jsPDF;

constructor( private jsPDFFontService: JsPDFFontService) { }

print() {
	this.doc = new jsPDF('p', 'mm', 'a4');
	this.jsPDFFontService.AddFontArimo(this.doc);
}

}

I hope its help you.

regards Ingo

1reaction
bufkecommented, Oct 21, 2018

I’ve never been able to get custom fonts to work. I believe it is intended to work like this

doc.addFileToVFS('PTSans-Regular.ttf', base64);
doc.addFont('PTSans-Regular.ttf', 'PTSans', 'normal')

If using Typescript just ignore the missing function declaration like this (doc as any).addFileToVFS...

This will fix your VFS error, however I just get No unicode cmap for font next. Perhaps with your font it will work however.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to import a new font into a project - Angular 5
You need to put the font files in assets folder (may be a fonts sub-folder within assets) and refer to it in the...
Read more >
How to add fonts to an Angular app - Reactgo
Open the angular app in your favorite code editor. · Create a new folder called fonts inside the assets folder. · Download the...
Read more >
Import fonts in an Angular App — THE EASY / RIGHT WAY!
Step 1: Download the font · Step 2: Unleash the beast! · Step 3: Transfonter — Generate @font-face · Step 4: Putting your...
Read more >
Customizing Typography - Angular Material
The easiest way to load Roboto, or any other custom font, is by using Google Fonts. The following snippet can be placed in...
Read more >
Angular - Add Custom Font - Abhith Rajan
To add custom fonts to your Angular project, put the font files under src . Here I am following the folder pattern which...
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