3
votes

I'm using javascript library jsPDF to create PDFs and I'd like to add a font to use it as text. This is how to call the function:

var doc = new jsPDF();

doc.addFont(PostScriptName, fontName, fontStyle, encoding);

And here is the info from the library:

  • @property id {String} PDF-document-instance-specific label assinged to the font.
  • @property PostScriptName {String} PDF specification full name for the font
  • @property encoding {Object} Encoding_name-to-Font_metrics_object mapping.
  • @name FontObject

What is PostScriptName and where to find it? I was looking for some information but can't find anything good. How can I use this function?

What i already tried was this:

var doc = new jsPDF(); doc.addFont("Arial", "Arial", "Regular","Ansi");

And i get and error:

TypeError: Object #

EDIT: ok, what I also find out that encoding must be type object. Does anyone know what kind of object should i put there?

2

2 Answers

0
votes

I have been looking at this library myself recently and I don't think the addFont method is publicly exposed.

If you open the source code in the jsPDF.js file and look at the addFonts() method it shows exactly how this method is called internally though.

addFont('Helvetica-Bold', 'helvetica', 'bold', 'StandardEncoding');
0
votes

Add this under Public API in jsPDF.js to expose the private addFont method:

API.addFont = function(postScriptName, fontName, fontStyle) {
  addFont(postScriptName, fontName, fontStyle, 'StandardEncoding');
}

Now in your script you can set it like this:

//Add custom font
doc.addFont('arial', 'arial', 'normal');
doc.setFont("arial");