jspdf添加宋体,jsPDF中的自定义字体?

jspdf添加宋体,jsPDF中的自定义字体?IsitpossibletoincludecustomfontsinjsPDF?Withthebasiclibrary,ifIconsolelog’doc.getFontList()’Iget:Courier,Helvetica,Times,courier,helvetica,timesBut,sayIwanttouse’ComicSa…

大家好,欢迎来到IT知识分享网。

jspdf添加宋体,jsPDF中的自定义字体?

Is it possible to include custom fonts in jsPDF ?

With the basic library, if I console log ‘doc.getFontList()’ I get:

Courier, Helvetica, Times, courier, helvetica, times

But, say I want to use ‘Comic Sans’ ( not that I would ;o) ) can it be done ?

Even better, could I use a font is locally stored and has been declared in the site with @font-face ?

解决方案

I found this was possible by modifying jsPDF.js to expose the existing addFont method in the public API.

In jsPDF.js, look for:

//—————————————

// Public API

Add the following:

API.addFont = function(postScriptName, fontName, fontStyle) {

addFont(postScriptName, fontName, fontStyle, ‘StandardEncoding’);

};

I put this method near other font methods for clarity – API.setFont, API.setFontSize, API.setFontType, etc.

Now in your code, use:

doc.addFont(‘ComicSansMS’, ‘Comic Sans’, ‘normal’);

doc.setFont(‘Comic Sans’);

doc.text(50,50,’Hello World’);

This works for me with @font-face fonts included with css before loading jsPDF, as well as system fonts. There’s probably a better way to do this using jsPDF’s plugin framework, but this quick and dirty solution should at least get you going.

Note that doc.getFontList() will not show added fonts:

// TODO: iterate over fonts array or return copy of fontmap instead in case more are ever added.

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/16273.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信