-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Use custom font (via vfs_fonts.js) #33
Comments
vfs_fonts.js assigns a "virtual-file-system" object to Each key of this object is a filename, while value contains base64 encoded file content. When you run
a new vfs_fonts.js is created, containing all files from I'm not sure if LICENSE.txt is required or not for Roboto. I've included it just to make sure I don't break the license. To use custom fonts, 3 steps are required:
1. create vfs_fonts.js containing your fonts Copy your fonts to examples/fonts directory run Use your new build/vfs_fonts.js on your web page BTW - the above action dumps all files from examples/fonts (not only fonts), which means you could put images there, run grunt dump_dir and reference them by name in doc-definition-object 2. define font family before calling pdfMake.createPdf(docDefinition) set pdfMake.fonts to the following object:
keys are font-family names you can later use in doc-definition Each font-family defines 4 properties: normal, bold, italics and bolditalics referring to appropriate files (by default these are file paths relative to examples/fonts/) By default pdfMake uses the following structure:
3. change font family in doc-definition-object Currently pdfmake uses 'Roboto' as default family name, so in order to use your font, you should change it. The easiest way is to do it globally in defaultStyle
I know this it is overly complicated at the moment. I'm going to change it in 0.2.0 |
sorry for replying so late - youe explanation works perfectly (not complicated at all) Kind Regards ! |
Hi bpampuch, Thanks, |
hi, can you show how to set the code for client use? |
Dear Muhaimin, Date: Wed, 15 Oct 2014 00:29:05 -0700 hi, can you show how to set the code for client use? — |
This is a great library, does this support Asian characters? |
After including the Font into the vfs_fonts.js like descripted above, this worked fine: pdfMake.fonts = {
myFont: {
normal: 'myFont-Regular.ttf',
bold: 'myFont-Medium.ttf',
italics: 'myFont-Italic.ttf',
bolditalics: 'myFont-Italic.ttf'
}
}
var docDefinition = {
content: (...),
defaultStyle: {
font: 'myFont'
}
}
pdfMake.createPdf(docDefinition).open(); |
Absolutely great library and I have managed to get it working in Dart via JsObject. Problem is the 'bold' and 'italics' rendering is either all or nothing. eg. taking bold in this case, below renders everything bold: content: {
table: {
body: [
[{text:'Embolden', style:'embolden'}, {text:'Embolden2', style:'embolden2'}],
['1', '2']
]
},
bold:false
},
styles: {
embolden: {
fontSize:18,
bold:true,
},
embolden2: {
fontSize:12,
bold:false
},
},
defaultStyle: {
fontSize:36,
bold:false
}, UPDATE: I have got a workaround by setting each font style as a new font ie.
|
I can generate the pdf with whatever font I choose but when I open the file in Illustrator, the characters are converted to gibberish. I need to be able to generate the pdf and run through rip software for printing from Illustrator. Is there a workaround anyone knows of? |
@bpampuch, May I please suggest to use Ubuntu font by default as it contains larger character-set? |
+1 On 04/04/2015 06:04 AM, Alok Shukla wrote:
|
I would vote strongly against it for two reasons:
There is a valid way if you need a different font! (and I know what I'm talking about. We're using Arial in our app - without any problems). |
Hello Guys, Here, I copied the font files I need to examples/fonts directory. After that i have to "run grunt dump_dir", but I don't understand where to run this command? |
You need a running grunt environement, this means, an installation of
THAN you can "compile" / DUMP the font to use in Javascript. |
Hi Svobi, Thanks for your help. I did based on what you mentioned here. I installed grunt and plugins in dependancy of package.json. after that i tried to install grunt-dum-dir , using following command, |
Well it seems only to be a warning. |
Thanks I was able to create the vfs_font.js. |
I created PDF but fonts are not reflecting in PDF. It Still shows the same old font. As step 2 a & 3 I wrote following code. pdfMake.fonts = { docDef = { |
When i converted my generated PDF to word doc, from an online converter and open doc file, the font were the same which i wanted to have. So, In PDF Fonts are not reflecting, but if I am converting that PDF to word. I am getting the fonts which i wanted.Pl. help in this |
Hello, I chage default font to Myanmar (Burmese) Unicode Font. It is working but unicode char display is not show correctly. Please see this error link http://prntscr.com/93nxfm. |
Hi. I’m using pdfMake and it seems great so far. However, I am struggling to get Times New Roman text into my PDF. In my project folder I’ve got a folder pdfMakeMaster, as well as my index file, my javascript file, and a bunch of other files I won’t mention. In pdfMakeMaster, I’ve got: build [ folder ] … and a bunch of other files I won’t mention. LICENSE.txt You list three steps for getting a new font into my pdfMake document; I believe that I have successfully completed Step 1: creating a new version of vfs_fonts.js that includes the Times New Roman ttf files. When I change my directory via cd to my pdfMakeMaster folder and run grunt dump_dir I get the output: Running "dump_dir:fonts" (dump_dir) task Done, without errors. …and there’s a (new) file vfs_fonts.js in my build folder with a Date Created of 1 minute ago. It’s slightly larger (4.4 MB) in size than the vfs_fonts.js file resulting a grunt dump_dir run with only the Roboto ttf files (and no Times ttf files) in pdfMakeMaster/examples/fonts. So far, so good. In my index file: < script src='pdfMakeMaster/pdfmake.js'></script> In my javascript file: function pdfTest(){ When pdfTest() is called, a new tab opens displaying a PDF with the word “Test” in the Roboto font; there are no errors. (Notice that Step 3 is already completed: the defaultStyle is set to font: Times. Although it does not error, it does not use Times font.) Now, I try to to work in Step 2: defining my font family. So now in my javascript file, I’ve got: function pdfTest(){ When pdfTest() is called, I receive the console error: “Uncaught Error: No unicode cmap for font — pdfmake.js: 30835” …which is the result of the snippet: if (!this.font.cmap.unicode) { throw new Error('No unicode cmap for font'); } … from pdfmake.js. Interestingly, I receive the same error when attempting to define my fonts with only Roboto (without Times): function pdfTest(){ There was some discussion here [ https://github.com//issues/18 ] that this error resulted from using pdfmake.min.js, but could be avoided if one used the non-min’ed version: pdfmake.js. Notice that I am using pdfmake.js (which I got from the build folder) and am still encountering the error. I would love for a little help getting through this issue as I have been stuck on it for some time. Please let me know of any additional information you need to help me to identify the root of my problem. |
@jacksnose I've been also encoutering the same error. Does anyone has a workaround on this? Thanks |
@Joeper214 Our problem turned out to be that our font filepaths were incorrect. They should not start with examples/fonts. For example, 'examples/fonts/Roboto-Regular.ttf', should be 'Roboto-Regular.ttf'. Hope this helps! |
@ewarrenG . Got it! Thanks for the help. |
I had gone through step 1: create vfs_fonts.js
now using it like : {font:'FontAwesome', text:'300.00'+'[]'} // '[]' = unicode for ₹ |
@AmitKannaujiya the same error, no chinese words show: chrome devtools no error show,the pdf can not show Chinese like below: |
@askie: Thanks , i had done same thing, used my custom font . |
@AmitKannaujiya when I change another ttf font file , all is ok ! so try some fonts again and again ! |
Hey guys, Im busy trying to add a custom font to pdfMake, am using it through amCharts.js. Can someone show me what the gruntfile.js should look like?
When I run grunt dum_dir I get the following: Done, without errors Nothing else happens? |
I didn't have to use any tools to add a custom font. I added the huge 22MB arialuni.ttf. I don't care about download size because I am using it on LAN only. I was surprised that copying and pasting this large amount took roughly 3 seconds. Adding wingdings.ttf kept giving errors in pdfmake. |
Hello, I come across one issue. I already customized the font. I generated new vfs_fonts.js that contains Roboto and simfang (chinese font). The problem is the file is about 15MB and the system loads very slow for the first time because it takes much time loading that font (as I check in network tab). I have to wait for long (on production). Does anybody have solution for this ? |
Thank you bpampuch |
In fact you don't even need the pdfmake source code or to include vfs_fonts.js if you better like to download the fonts from the server (that probably has them already in cache if you use the same fonts in the HTML page and the PDF). We do it this way (the language used is TypeScript but you can easily convert this to standard Javascript using promises instead of
This works on all "modern" browsers and IE 10 & 11 with the 'fetch' polyfill. |
I tried all of the above but it still did not change |
After reading the following post describing the need for a minimum of 4 fonts I was wondering. If you need to use a font like OCR-B for optical character recognition and this font comes in only 1 style, how can I make vfs_fonts.js work with only 1 font? If this has not yet been implemented I understand. |
Does pdfMake support Unicode ? |
Just in-case anyone else has a question like @suchislife801, if you are using OCR-B like fonts, which come only in one format, then the way I got it to work was following this instruction was to copy that 1 ocr font 4 times and give then a name like; ocr-bold, ocr-normal, orc-bolditalics and ocr-italics, after that just pass pdfMake.fonts = {
ocr_demo: {
normal: 'OCR-normal.TTF',
bold: 'OCR-bold.TTF',
italics: 'OCR-italics.TTF',
bolditalics: 'OCR-bolditalics.TTF',
}
} |
Does anyone else encounter the issue of their custom-made vfs_fonts.js dissappearing every now and then...? |
@nickbae91 yeah, it happened to me last time, everyone who forks your lib needs to rebuild them otherwise if the push without the fonts you'll lose them too |
Hi @jthoenes , Can you please tell how to handle large size fonts (I am using Arial Unicode, and it takes around a minute to load fonts only.) How did you handle this in your app? |
@suhasbhattu There's no . way around this. Large font are slow. Sorry :-/ |
I've occurred the same issue especially the font is unicode supported. In my case, I run the build |
It seems that the vfs_fonts.js file contains the binary font data of the Roboto font but somehow encoded (data URI / base64 ?).
could you explain how to generate the required JS file for other fonts (and: is the LICENSE.txt required ?)
btw: great work - thanks, seems quite promising !
kind regards,
matthias
The text was updated successfully, but these errors were encountered: