热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

IsitpossibletoincludecustomfontsinjsPDF?Withthebasiclibrary,ifIconsolelogdoc.getFontList()

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.



推荐阅读
author-avatar
周啸夫_919
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有