首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jspdf-autotable插件的自定义字体

jspdf-autotable插件的自定义字体
EN

Stack Overflow用户
提问于 2016-11-08 10:54:24
回答 3查看 6.4K关注 0票数 5

有没有人可以提供一个为jspdf-autotable设置自定义字体的示例

我尝试了以下几种方法

代码语言:javascript
复制
var doc = new jsPDF('p', 'pt');
doc.setFont("rotobo");      ----> font face name that I declared in my css file
doc.autoTable(columns, data);
doc.save("table.pdf");

在我尝试之后,PDF中的字体没有改变。

如有任何建议,我们将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2016-11-15 14:19:26

试试这样的东西。

代码语言:javascript
复制
doc.autoTable(columns, data, {styles: {font: "rotobo"}});

您可以参考有关自定义样式示例here的更多信息,文档README.md会显示所有样式。

票数 7
EN

Stack Overflow用户

发布于 2017-09-25 15:39:41

您可以使用

代码语言:javascript
复制
console.log(doc.getFontList());

检查它们支持哪些字体。看起来他们只支持这些字体:

信使时代信使

如果您希望在PDF文件中显示复选标记,则此插件可能没有此功能...

票数 1
EN

Stack Overflow用户

发布于 2019-05-31 15:55:12

要解决此问题,您需要执行以下操作: 1.下载jspdf-autotable,以便在本地工作。2.在jspdf-autotable中,有两次调用text(...)函数,因此您需要添加(在调用文本函数之前) "setFont“

您需要更改的代码:

代码语言:javascript
复制
jsPDF.API.autoTableText = function (text, x, y, styles) {
styles = styles || {};
var FONT_ROW_RATIO = 1.15;
if (typeof x !== 'number' || typeof y !== 'number') {
    console.error('The x and y parameters are required. Missing for text: ', text);
}
var k = this.internal.scaleFactor;
var fontSize = this.internal.getFontSize() / k;
var splitRegex = /\r\n|\r|\n/g;
var splitText = null;
var lineCount = 1;
if (styles.valign === 'middle' || styles.valign === 'bottom' || styles.halign === 'center' || styles.halign === 'right') {
    splitText = typeof text === 'string' ? text.split(splitRegex) : text;
    lineCount = splitText.length || 1;
}
// Align the top
y += fontSize * (2 - FONT_ROW_RATIO);
if (styles.valign === 'middle')
    y -= (lineCount / 2) * fontSize * FONT_ROW_RATIO;
else if (styles.valign === 'bottom')
    y -= lineCount * fontSize * FONT_ROW_RATIO;
if (styles.halign === 'center' || styles.halign === 'right') {
    var alignSize = fontSize;
    if (styles.halign === 'center')
        alignSize *= 0.5;
    if (lineCount >= 1) {
        for (var iLine = 0; iLine < splitText.length; iLine++) {
            this.text(splitText[iLine], x - this.getStringUnitWidth(splitText[iLine]) * alignSize, y);
            y += fontSize;
        }
        return this;
    }
    x -= this.getStringUnitWidth(text) * alignSize;
}
if (styles.halign === 'justify') {
    this.setFont("frank");   //---> this u need to adding
    this.setFontType("normal");
    this.text(text, x, y, { maxWidth: styles.maxWidth || 100, align: 'justify' });
}
else {
    this.setFont("frank"); //---> this u need to adding
    this.setFontType("normal");
    this.text(text, x, y);
}
return this;

};

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40478179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档