我的代码中可能有一个bug,但我还不确定。我正尝试在我的Vue应用程序中创建一个具有自定义字体的pdf。我下载了该字体,并使用github代码库中提供的/ base64 /fontConverter.html将其转换为字体。我创建了一个名为'fonts‘的新文件夹。在文件夹中,我包含了自定义字体的.ttf文件和来自字体转换器的.js文件。接下来,我创建了一个名为downloadPdf()的方法,如下所示:
const doc = new jsPDF()
doc.setFontSize(28);
doc.text(20, 30, 'This is the default font.')
doc.setFont('courier')
doc.setFontType('normal')
doc.text(20, 60, 'This is courier normal.')
//This does not seem to solve the problem.
//Also, this method is already included at the bottem of the 'Roboto-Regular-normal.js' file
doc.addFileToVFS("./fonts/Roboto-Regular.ttf", './fonts/Roboto-Regular-normal.js');
doc.addFont("./fonts/Roboto-Regular.ttf", "Roboto", "normal");
doc.setFont("Roboto");
doc.text("Reinier van der Galien", 20, 90);
console.log(doc.getFontList());
doc.save("customFonts.pdf");这将创建具有自定义字体的pdf,以便正常工作。但是,在控制台中,它仍然给我以下错误:
jsPDF PubSub错误没有用于字体的unicode cmap错误:没有用于字体的unicode cmap
和:
jsPDF PubSub错误无法读取未定义的属性'widths‘
addFileToVFS方法似乎不起作用。如有任何帮助,我们将不胜感激。
发布于 2019-10-18 21:16:18
这是我的解决方案,可能不是很理想,但它是有效的。
首先导入带字体的文件。文件只包含带有字体字符串的常量,并导出该常量。
import Roboto from '@/assets/fonts/Roboto-Regular-normal.js'
import RobotoBold from '@/assets/fonts/Roboto-Bold-bold.js'然后将其添加到jsPDF文档中。
let doc = new jsPDF()
doc.addFileToVFS('Roboto-Regular.ttf', Roboto)
doc.addFileToVFS('Roboto-Bold.ttf', RobotoBold)
doc.addFont('Roboto-Regular.ttf', 'Roboto', 'normal')
doc.addFont('Roboto-Bold.ttf', 'Roboto', 'bold')
doc.setFont('Roboto')编辑:我从fontconvertor获取字体字符串。从导出的文件中,我只获取了该字符串,其他所有内容我都删除了。
https://stackoverflow.com/questions/58449153
复制相似问题