I am using pdfmake to create PDF's on the client side. We have a WYSIWYG editor that allows the users to created a pdf. This is then parsed to work with the pdfmake.
However, i cannot get normal fonts to work. The plugin uses vfs_fonts.js to create the font on the PDF, default is Roboto.
I am trying to get it to work with the likes of Times New Roman etc etc.
I have tried to alter the file like this:
window.pdfMake = window.pdfMake || {};
window.pdfMake.vfs = {
Roboto: {
"Roboto-Italic.ttf": "BASE 64 HERE",
"Roboto-Medium.ttf": "BASE 64 HERE",
"Roboto-Regular.ttf": "BASE 64 HERE"
},
TimesNewRoman: {
"Roboto-Italic.ttf": "BASE 64 HERE",
"Roboto-Medium.ttf": "BASE 64 HERE",
"Roboto-Regular.ttf": "BASE 64 HERE"
}
}
I have used the same fonts as the Roboto as a test but it still doesn't work. Here is the error I get back
Uncaught Error: No unicode cmap for font
Here is my code below. You paste this string into the pdf tester here and see the result
{
"content":[
{
"stack":[
{
"text":[
{
"text":""
}
]
},
{
"text":"ygjjkjgjkhjkjghk",
"style":"style_2",
"lineHeight":"1"
}
],
"style":"style_1"
},
{
"stack":[
{
"text":[
{
"text":""
}
]
},
{
"text":" ",
"style":"style_4",
"lineHeight":"1"
}
],
"style":"style_3"
},
{
"stack":[
{
"text":[
{
"text":""
}
]
},
{
"text":"",
"style":"style_7",
"font":"TimesNewRoman",
"lineHeight":"1"
},
{
"text":"sdfghfdghfghdgfgfh",
"style":"style_8",
"font":"TimesNewRoman",
"lineHeight":"1"
}
],
"style":"style_5"
}
],
"styles":{
"style_1":{
"opacity":"1"
},
"style_2":{
"opacity":"1"
},
"style_3":{
"opacity":"1"
},
"style_4":{
"opacity":"1"
},
"style_5":{
"opacity":"1"
},
"style_6":{
"opacity":"1"
},
"style_7":{
"font":"TimesNewRoman",
"opacity":"1"
},
"style_8":{
"opacity":"1"
}
},
"pageSize":"A4",
"pageOrientation":"portrait",
"pageMargins":[
40,
20,
40,
20
]
}
Has anyone else used this library? If so, did you use custom fonts, and how did you get them to work? I can post more code if needed, thanks
Based on @spm answer I build a github repository with the Times New Roman font javascript file, and instructions on how to use it!
PDFMake-Fonts: I plan on adding new fonts eventually.
Pdfmake documentation on how to use custom fonts on client-side here.
The vfs_fonts.js file format is something like:
Therefore, you should define it like the following:
After that, you still need to assign pdfMake.fonts:
Then, you can use both
Robot
andTimesNewRoman
as font on your pdf definition as you are doing now: