pdfmake icon indicating copy to clipboard operation
pdfmake copied to clipboard

Use custom font (via vfs_fonts.js)

Open matthias-001 opened this issue 12 years ago • 46 comments

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

matthias-001 avatar May 26 '14 11:05 matthias-001

vfs_fonts.js assigns a "virtual-file-system" object to pdfMake.vfs

Each key of this object is a filename, while value contains base64 encoded file content.

When you run

grunt dump_dir

a new vfs_fonts.js is created, containing all files from examples/fonts directory.

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
  2. define font family
  3. change font family in doc-definition-object

1. create vfs_fonts.js containing your fonts

Copy your fonts to examples/fonts directory

run grunt dump_dir (you can update Gruntfile.js if you'd like to change base directory or add an alternative config for dump_dir task)

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:

{
   yourFontName: {
     normal: 'fontFile.ttf',
     bold: 'fontFile2.ttf',
     italics: 'fontFile3.ttf',
     bolditalics: 'fontFile4.ttf'
   },
   anotherFontName: {
     (...)
   }
}

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:

{
        Roboto: {
                normal: 'Roboto-Regular.ttf',
                bold: 'Roboto-Medium.ttf',
                italics: 'Roboto-Italic.ttf',
                bolditalics: 'Roboto-Italic.ttf'
        }
};

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

var docDefinition = {
  content: (...),
  defaultStyle: {
    font: 'yourFontName'
  }
}

I know this it is overly complicated at the moment.

I'm going to change it in 0.2.0

bpampuch avatar Jun 11 '14 20:06 bpampuch

sorry for replying so late - youe explanation works perfectly (not complicated at all) Ii took me a bit of time till I noted that ALL 4 font-styles (normal, bold, italics and bolditalics have to be specified in order to make it work)

Kind Regards !

matthias-001 avatar Jun 22 '14 22:06 matthias-001

Hi bpampuch, I am new to pdfmake.I tried above 3 steps for custom font rendering in PDF, but i got blank space in that pdf for that particular word, i attached the PDF(converted to png document) for your reference and my code.Please check and tell what i did wrong.Please guide how to use custom font in pdfmake.Thanks in advance.

This is code,

pdfMake.font={ Roboto: { normal: 'examples/fonts/Roboto-Regular.ttf', bold: 'examples/fonts/Roboto-Medium.ttf', italics: 'examples/fonts/Roboto-Italic.ttf', bolditalics: 'examples/fonts/Roboto-Italic.ttf' }, lohit: { normal: 'examples/fonts/lohit-Regular.ttf', bold: 'examples/fonts/lohit-Medium.ttf', italics: 'examples/fonts/lohit-Italic.ttf', bolditalics: 'examples/fonts/lohit-Italic.ttf' } } var docDefinition = { content: ("Test Tamil கிருஷ்ணன் Word"), Style: { font: 'lohit' } }; pdfMake.createPdf(docDefinition).open();

Thanks, Venu

venugopalangithub avatar Sep 18 '14 05:09 venugopalangithub

Hi bpampuch, Thanks for great lib. I am wondering if this support unicode font like Khmer Unicode. I am working on generating invoice for my client. My output did not display as intended as the subscript not working. Please help guide me on how to do this I am using Khmer font "Battambang".

Thanks, Rith

sokdararith avatar Oct 13 '14 03:10 sokdararith

hi, can you show how to set the code for client use?

infacq avatar Oct 15 '14 07:10 infacq

Dear Muhaimin, Thank you for your response. I am providing with zip code of my client use. To give your more info please let me elaborate a bit more of my project and my intended use. I would love to use this to generate a report and also invoices in pdf file. Our database uses Khmer language. Khmer language has combination of supscript and superscript but in the example the subscript did not work. I generated font based on what you described in the guide by including the font on examples/fonts folder and run grunt dump_dir command. Please advise what I can do to achieve the intended result. I highly appreciated your great work and support. Sincerely, Rith

Date: Wed, 15 Oct 2014 00:29:05 -0700 From: [email protected] To: [email protected] CC: [email protected] Subject: Re: [pdfmake] Use custom font (via vfs_fonts.js) (#33)

hi, can you show how to set the code for client use?

— Reply to this email directly or view it on GitHub. =

sokdararith avatar Oct 15 '14 07:10 sokdararith

This is a great library, does this support Asian characters?

dingzhanjun avatar Oct 24 '14 16:10 dingzhanjun

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();

Svobi avatar Nov 06 '14 21:11 Svobi

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.

    'TimesNewRoman': {
        'normal': 'Times-New-Roman.ttf'
    },
    'TimesNewRomanBold': {
        'normal': 'Times-New-Roman-Bold.ttf'
    },

TheShakyCoder avatar Jan 07 '15 15:01 TheShakyCoder

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?

roundbox avatar Jan 18 '15 17:01 roundbox

@bpampuch, May I please suggest to use Ubuntu font by default as it contains larger character-set?

shuklaalok7 avatar Apr 04 '15 04:04 shuklaalok7

+1

On 04/04/2015 06:04 AM, Alok Shukla wrote:

@bpampuch https://github.com/bpampuch, May I please suggest to use Ubuntu font http://font.ubuntu.com/ by default as it contains larger character-set?

— Reply to this email directly or view it on GitHub https://github.com/bpampuch/pdfmake/issues/33#issuecomment-89497240.

luciash avatar Apr 05 '15 13:04 luciash

I would vote strongly against it for two reasons:

  • embedding a larger font by default slows down the processing significantly (using Arial Unicode made our processing 20 times slower)
  • we break backwards compatibility for everyone

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

jthoenes avatar Apr 05 '15 15:04 jthoenes

Hello Guys, I am not understanding the First Step:- 1. create vfs_fonts.js containing your fonts.

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? Pl. help in that.

Chaitanya2207 avatar Jul 06 '15 11:07 Chaitanya2207

You need a running grunt environement, this means, an installation of

  • node.js, (start here https://nodejs.org/download/ )
  • npm (Node Package Manager included in nodejs ),
  • an grunt Configuration and Installations, (learn here http://gruntjs.com/ -> install via npm)
  • and a grunt modul named "dump_dir" (install via npm "grunt-dump-dir").

THAN you can "compile" / DUMP the font to use in Javascript.

Svobi avatar Jul 13 '15 15:07 Svobi

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, "npm install grunt-dump-dir --save-dev" after running this command , i got error "D:\grunt\font\grunt-dump-dir-master>npm install grunt-dump-dir npm WARN package.json [email protected] No license field. npm WARN install Refusing to install grunt-dump-dir as a dependency of itself" Pl. help me in this

Chaitanya2207 avatar Jul 14 '15 06:07 Chaitanya2207

image

Chaitanya2207 avatar Jul 14 '15 06:07 Chaitanya2207

Well it seems only to be a warning. Did you try to grunt already ?

Svobi avatar Jul 14 '15 07:07 Svobi

Thanks I was able to create the vfs_font.js.

Chaitanya2207 avatar Jul 14 '15 10:07 Chaitanya2207

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 = { CALIBRI: { normal: 'CALIBRI.TTF', bold: 'CALIBRIB.TTF', italics: 'Calibri Italic.ttf', bolditalics: 'Calibri Bold Italic.ttf' } };

docDef = { footer: function(pageCount) { pgCnt++;
return {columns:[{image:j,"width":w,"height":h},{text:' Please refer to site observation photographs.These defects are example and might not represent the entire Pg. '+pgCnt+'',fontSize:8}]} }, defaultStyle: { font: 'CALIBRI' } };

Chaitanya2207 avatar Jul 14 '15 10:07 Chaitanya2207