Is it possible to add other language support other than English (UTF-8) in pdfmake library? something like,
var dd = {
content: [
'ಕನ್ನಡ', //Kannada
'മലയാളം' //Malayalam
]
}
any news regarding this?
I do not know these languages, but:
LTR languages are supported (is need a font file that supports this language)
RTL languages are not supported - pending issue https://github.com/bpampuch/pdfmake/issues/184
Can you give me an example of how to accept for example English an another one? Thanks!!!
Manual of how to use custom font: https://github.com/bpampuch/pdfmake/wiki/Custom-Fonts---client-side
Its working for few fonts but Lohith-kannada.ttf font not working and gives blank spaces. Also baraha-kannada.ttf font gives jumbled words.
Myself asked a question in Stack overflow here.
@liborm85 , Myself tried with BARAHAKN.TTF, Nudi_05_k.ttf and lohit_kn.ttf Kannada fonts but each fonts giving jumbled sentences. Any workarounds to solve the issue?
Kannada currently not supported (see test report). Implementation is required to fontkit library, which pdfmake uses.
@liborm85 Find the referenced issue above.
@mpsbhat That's good. As soon as the new version of fontkit, it will work in pdfmake also.
@liborm85 , can we test it before release of new version of fontkit? Myself searching within the pdfmake files but didn't found the fontkit library call.
@mpsbhat pdfmake calls only the pdfkit library. Calling the fontkit library is up to pdfkit library.
Sorry, I don't know how to build fontkit from source code... :(
Ok @liborm85 Thank you for the info.
Finally succeeded build :)
pdfmake + fontkit-dev with kannada/indic support:
pdfmake.zip
I don't know if it is correct build, but it looks like it works. :) I try it with this font: https://github.com/devongovett/fontkit/blob/indic/test/data/NotoSans/NotoSansKannada-Regular.ttf
The above build gives blank pdf :(
We included above pdfmake.js and created a new vfs-fonts.js using the mentioned font.
The base64data as follows:
blob:null/096f394c-61cb-42d6-a3c1-58f9545286d5
Here is my full test script: pdfmake-indic-test.zip
This works for me, Try it.
Splendid...!!!

Thanks @liborm85 :)
How can we use both English & Kannada fonts simultaneously?
You create vfs_fonts.js file with multiple fonts. One for English and other for Kannada. See https://github.com/bpampuch/pdfmake/wiki/Custom-Fonts---client-side
fontkit 1.7 has full support for indic scripts, including Kannada and 9 others. See https://github.com/devongovett/fontkit/pull/99 for details.
New version pdfmake released.
Uncaught Error: Font 'Roboto' in style 'normal' is not defined in the font section of the document definition.
When using new build.
On playground generating pdf works ok. Attach any sample script for reproduce problem.
<html>
<head>
<script type="text/javascript" src="pdfmake.js"></script>
<script type="text/javascript" src="vfs_fonts.js"></script>
</head>
<body>
<button onclick="clickme();">Click</button>
<script type="text/javascript">
function clickme() {
pdfMake.fonts = {
mfont: {
normal: 'NotoSansKannada-Regular.ttf',
bold: 'NotoSansKannada-Regular.ttf',
italics: 'NotoSansKannada-Regular.ttf',
bolditalics: 'NotoSansKannada-Regular.ttf'
}
};
var dd = {
content: [
{
text:'Test'
},
{
text : 'ಕನ್ನಡ ',
font: 'mfont'
}
]
};
pdfMake.createPdf(dd).open();
}
</script>
</body>
</html>
This could not work in an older version.
Default font in pdfmake is Roboto. And here is Roboto font required:
{
text:'Test'
},
But in pdfMake.fonts Roboto isn't defined, therefore the error.
We have used the latest build of pdfmake.
Do we use like this?
{
text : 'Test ',
font: 'Roboto'
}
after defining
Roboto: {
normal: 'Roboto-Regular.ttf',
bold: 'Roboto-Medium.ttf',
italics: 'Roboto-Italic.ttf',
bolditalics: 'Roboto-Italic.ttf'
}
Also tried setdefaultstyle attribute. But no use.
Roboto is default font is no need to define it in text element.
Here is my test script: test.zip.gz Try it.
If you want to use another default font, use defaultStyle (is shown in zip) and edit pdfMake.fonts (details here)
Thank you @liborm85 ..!!! Got it...!!!
But if we give pdfMake.createPdf(dd).open(); it will generate blank pdf.
One more we are trying to have pdf with a text something like
{
text:'Test ಕನ್ನಡ'
},
That is combination of two fonts which coming from server side through ajax call. What will be the work around for this combination?
open() is only supported in some browsers, see https://github.com/bpampuch/pdfmake/issues/800
text combination with two fonts:
{
text : [
{
text: 'Test '
},
{
text : 'ಕನ್ನಡ ',
font: 'mfont'
}
]
},
We just wondering if there is something text like say Test ಕನ್ನಡ Test which is coming dynamically from server as a combination of two or more fonts.
We will try it out the above solutions and let you know if any hurdles.
Thanks a lot @liborm85 and @devongovett for great support...!!!
@mpsbhat did you found any solution for multiple language in same sentence?
I have to same issue, i have 'Comments' field coming from server, and it can be in any langauge.
@mpsbhat did you found any solution for multiple language in same sentence?
I have to same issue, i have 'Comments' field coming from server, and it can be in any langauge.
Yes, we followed the steps given above by @liborm85 for custom fonts and it's working fine by updating vfs_fonts.js file with required fonts. Also we have to refer the font in doc definition object as mentioned based on language like {text: [{text: "ENGLISH", font: font1}, {text: "NON ENGLISH", font: font2}]};
@mpsbhat could u please show ur working result ? I also faced this issue as well when multiple languages is in the same sentence like this : Khmer ខ្មែរ
@mpsbhat could u please show ur working result ? I also faced this issue as well when multiple languages is in the same sentence like this : Khmer ខ្មែរ
@noeurphireak Did you guys find a solution that would work for multiple language text in the same sentence?
@mpsbhat For a static Text your approach would work as you are defining font word by word. But how would you know which language a user has entered in case of dynamic content? without detecting each word's language cannot specify the desired font.
@rumman0786 @noeurphireak
We are ended with something like below. We used to check ASCII character by splitting each string by charachter. One drawback of this is, it is impacting on rendering time for long pages as individual characters are converted into object with font name. Please suggest if someone found better options.
Note: we had created vfs-font.js file as explained and initialized the font reference at the beginning of javascript.
var test = "ಕನ್ನಡ... ಜೈ ಭಾರತಾಂಬೆ...";
var test2 = "ಕನ್ನಡEnglishಒಟ್ಟಿಗೆ... ಜೈ ಭಾರತಾಂಬೆ I'm Bharatheeya...";
var dd = {
content: [
{text: test+ '\n\n', style: 'defaultStyle'}, //////////// This is pure kannada
{text: 'Only english\n\n'}, //////////// Only english
{text: test2+ '\n\n'}, //////////// Mix without parsing
{text: check_font(test2+ '\n\n')}, /////////// Mix with kannada parsing - U can use check_font function wherevere required to parse kannada font, just pass the string to function and it will return the object by adding fonts based on ASCII character.
],
styles: {
defaultStyle: {
fontSize: 14,
font: 'font_kn'
}
}
};
function download_pdf() {
console.log(dd);
pdfMake.createPdf(dd).print();
//pdfMake.createPdf(dd).download("test.pdf");
}
function check_font(str) {
var parts = str.split('');
arr = [];
for (var i = 0; i < parts.length; i++) {
var s = str.charAt(i);
// while the next char is not a swara/vyanjana or previous char was a virama
while (((i + 1) < str.length && str.charCodeAt(i + 1) < 0xC85 || str.charCodeAt(i + 1) > 0xCB9 || str.charCodeAt(i) === 0xCCD) && str.charAt(i + 1).match(/[a-z]/i) === null) {
s += str.charAt(i + 1);
i++;
}
if (s.match(/[a-z]/i)) {
arr.push({
text: s
});
} else {
arr.push({
text: s,
font: 'font_kn'
});
}
}
return arr;
}
Most helpful comment
fontkit 1.7 has full support for indic scripts, including Kannada and 9 others. See https://github.com/devongovett/fontkit/pull/99 for details.