最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add and set a font in JsPDF? - Stack Overflow

programmeradmin0浏览0评论

I've been trying to set the Helvetica font in different ways but nothing seems to work. I understand the addFont and setFont function but I'm having problems using them.

This is what I have:

doc.addFont('Helvetica', 'Helvetica', 'normal')
doc.setFont('Helvetica')

Apparently the postscript name for Helvetica is 'Helvetica' so I don't know what it could be.

I've been trying to set the Helvetica font in different ways but nothing seems to work. I understand the addFont and setFont function but I'm having problems using them.

This is what I have:

doc.addFont('Helvetica', 'Helvetica', 'normal')
doc.setFont('Helvetica')

Apparently the postscript name for Helvetica is 'Helvetica' so I don't know what it could be.

Share Improve this question edited Jan 23, 2020 at 19:23 Kev Lozano asked Jan 23, 2020 at 19:07 Kev LozanoKev Lozano 811 gold badge2 silver badges7 bronze badges 2
  • 1 stackoverflow./a/26981550/8637561 – Vincenzo Ninni Commented Jan 23, 2020 at 20:11
  • 1 Does this answer your question? Custom font faces in jsPDF? – Andrew L Commented Jan 23, 2020 at 21:46
Add a ment  | 

2 Answers 2

Reset to default 4

you can convert your font

Example PTSans-normal.js

import jsPDF from 'jspdf' jsPDF - Importing fonts in React.js / ES6 style

Using a new font

import 'assets/fonts/PTSans-normal.js';
import jsPDF from 'jspdf';

const doc = new jsPDF();

doc.setFont('PTSans'); // set custom font
doc.setFontSize(12); // you can change size
doc.text('Hello', x, y) // and you can write your text

console.log('Show all font in jsPDF',doc.getFontList());

No solution worked for me but the following...

Step 1: I Converted .ttf file to base64 .txt file. I used this link to achieve that https://www.base64encode/

Step 2: I added .txt file to vfs using addFileToVFS method

enter code here
doc.addFileToVFS('./fonts/verdana-normal.txt', 'verdana')
doc.addFileToVFS('./fonts/verdana-bold.txt', 'verdana')
doc.addFileToVFS('./fonts/verdanai.txt', 'verdana')
doc.addFileToVFS('./fonts/verdana-bold-italic.txt', 'verdana')

Step 3: Then I added to the font list

doc.addFont('./fonts/verdana-normal.ttf', 'Verdana', 'normal')
doc.addFont('./fonts/verdana-bold.ttf', 'Verdana', 'bold')
doc.addFont('./fonts/verdanai.ttf', 'Verdana', 'italic')
doc.addFont('./fonts/verdana-bold-italic.ttf', 'Verdana', 'italicbold')

You can check the font list if the is added:

console.log(doc.getFontList())

Please feel free to check out my github repo for this projects and more!! jsPdf cutom font

发布评论

评论列表(0)

  1. 暂无评论