Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Other languages in pdfmake #562

Closed
mpsbhat opened this issue Apr 19, 2016 · 38 comments
Closed

Other languages in pdfmake #562

mpsbhat opened this issue Apr 19, 2016 · 38 comments

Comments

@mpsbhat
Copy link

mpsbhat commented Apr 19, 2016

Is it possible to add other language support other than English (UTF-8) in pdfmake library? something like,

var dd = {
    content: [
        'ಕನ್ನಡ',              //Kannada
        'മലയാളം'       //Malayalam
    ]
}
@Gal-Brenner
Copy link

any news regarding this?

@liborm85
Copy link
Collaborator

liborm85 commented Jan 31, 2017

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 #184

@Gal-Brenner
Copy link

Can you give me an example of how to accept for example English an another one? Thanks!!!

@liborm85
Copy link
Collaborator

Manual of how to use custom font: https://github.com/bpampuch/pdfmake/wiki/Custom-Fonts---client-side

@mpsbhat
Copy link
Author

mpsbhat commented Jan 31, 2017

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.

@mpsbhat
Copy link
Author

mpsbhat commented Mar 1, 2017

@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?

@liborm85
Copy link
Collaborator

liborm85 commented May 7, 2017

Kannada currently not supported (see test report). Implementation is required to fontkit library, which pdfmake uses.

@mpsbhat
Copy link
Author

mpsbhat commented May 21, 2017

@liborm85 Find the referenced issue above.

@liborm85
Copy link
Collaborator

@mpsbhat That's good. As soon as the new version of fontkit, it will work in pdfmake also.

@mpsbhat
Copy link
Author

mpsbhat commented May 21, 2017

@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.

@liborm85
Copy link
Collaborator

@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... :(

@mpsbhat
Copy link
Author

mpsbhat commented May 21, 2017

Ok @liborm85 Thank you for the info.

@liborm85
Copy link
Collaborator

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

@mpsbhat
Copy link
Author

mpsbhat commented May 21, 2017

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

@liborm85
Copy link
Collaborator

Here is my full test script: pdfmake-indic-test.zip
This works for me, Try it.

@mpsbhat
Copy link
Author

mpsbhat commented May 22, 2017

Splendid...!!!
screenshot from 2017-05-22 11 28 53

Thanks @liborm85 :)

@mpsbhat
Copy link
Author

mpsbhat commented May 27, 2017

How can we use both English & Kannada fonts simultaneously?

@liborm85
Copy link
Collaborator

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

@devongovett
Copy link
Contributor

fontkit 1.7 has full support for indic scripts, including Kannada and 9 others. See foliojs/fontkit#99 for details.

@liborm85
Copy link
Collaborator

New version pdfmake released.

@mpsbhat
Copy link
Author

mpsbhat commented May 31, 2017

Uncaught Error: Font 'Roboto' in style 'normal' is not defined in the font section of the document definition.

When using new build.

@liborm85
Copy link
Collaborator

On playground generating pdf works ok. Attach any sample script for reproduce problem.

@mpsbhat
Copy link
Author

mpsbhat commented May 31, 2017

<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>

@liborm85
Copy link
Collaborator

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.

@mpsbhat
Copy link
Author

mpsbhat commented May 31, 2017

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.

@liborm85
Copy link
Collaborator

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)

@mpsbhat
Copy link
Author

mpsbhat commented May 31, 2017

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?

@liborm85
Copy link
Collaborator

liborm85 commented May 31, 2017

open() is only supported in some browsers, see #800

text combination with two fonts:

    {
      text : [
        {
          text: 'Test '
        },
        {
          text : 'ಕನ್ನಡ ',
          font: 'mfont'
        }
      ]
    },

@mpsbhat
Copy link
Author

mpsbhat commented May 31, 2017

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...!!!

@niravparsana94
Copy link

@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
Copy link
Author

mpsbhat commented Feb 26, 2020

@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}]};

@noeurphireak
Copy link

@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 ខ្មែរ

@rumman0786
Copy link

@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.

@mpsbhat
Copy link
Author

mpsbhat commented Dec 27, 2020

@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;
}

@imPrashik
Copy link

try using "Arial_Unicode_MS", this font supports many languages.

WikiPedia

@nagashreehr
Copy link

nagashreehr commented May 4, 2024

@mpsbhat @liborm85 I followed all the steps that are given in the pdfmake documentation, I want to generate a pdf in Kannada and I have used the Noto Sans Kannada font file and I'm getting an issue as
"Error: Uncaught (in promise): File 'NotoSansKannada.ttf' not found in virtual file system"

@mpsbhat
Copy link
Author

mpsbhat commented May 10, 2024

The default vfs file cannot be used directly for custom fonts. You should build one as mentioned in the documentation.

@nagashreehr
Copy link

nagashreehr commented May 10, 2024

@mpsbhat yes I have Executed the command node build-vfs.js "./examples/fonts to build the font file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants