PdfMake: use system fonts

I am using pdfMake -library to generate client side PDF documents in one of my applications. My main problem when using the library is that it relies on the availability of available fonts in the virtual file system. Therefore, the fonts are sent to the client in the vfs_fonts.js JavaScript file and then loaded into the virtual filesystem and then embedded in the resulting PDF (of course, only a usable subset of the characters of the entire font).

At default settings, the library uses the Roboto font and therefore sends just over 800kb for fonts only. My main intuition is here:

Why doesn't it use system fonts and, as a result, saves traffic?

Is there a workaround to use system fonts?

My current approach to reducing traffic is to remove font styles from vfs_fonts.js that are not used in the generated documents. For example. I am removing italic, bold style, and italic / bold style using normal styles only. With this method, I was at least able to reduce the font file size by 3/4.

+3


source to share


3 answers


As the main maintainer of pdfMake pointed out, this feature is currently not achievable due to the limitations of the underlying pdfkit library.



Therefore, in order to reduce the size of the font file, your best option at this point might be to remove unused fonts and use gzip compression for your web server.

0


source


I am also using PDFmake and I faced a similar problem.

You need to add any new source via the appropriate key, for example:

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

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

}

      

Then, in the PDF content, you must add the "font" you intend to use.



    {
   stack : [
      { 
         text : this is a test text
         font  : 'OpenSans',
         italic : true
      }, { 
         text : this is another test text
         font : 'Roboto',
         bold : true
      }  
  ]
}

      

Hope you find this a good solution for people looking for a solution.

Source here. Thank you Daniel Arbiolu ( Darbiol ).

0


source


PDFKit now supports standard fonts .

# Using a standard PDF font
doc.font('Times-Roman')
   .text('Hello from Times Roman!')
   .moveDown(0.5)

      

See also this live example .

-1


source







All Articles