2

I use the html-pdf component to generate pdf reports in my nodejs app and in order to properly embed custom fonts in the pdf document, these fonts must be installed in the server, in this case, in the IBM Cloud nodejs runtime. How do I do that?

Leo
  • 751
  • 4
  • 29
  • This is a guess. If the environment is based on debian/ubuntu then onecan install local fonts in $USER/.fonts, if it's a heavily restricted environment, then in your nodejs startup issues shell commands along the lines of 'mkdir -p ~/.fonts', copy the fonts from your app directory to the font directory, child_process.exec fc-cache -f -v, and then continue... I'd added this as a comment as untested and commands aren't exact – Mâtt Frëëman May 31 '19 at 14:32

2 Answers2

2

You either make them static content and push them with your app or you download them. Downloading can be dynamic or, in your package.json, as a dependecy. There are many font packages for that purpose.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • These fonts must be installed in the ibm cloud runtime (which is basically a VM). Just adding the fonts to the node.js app path does not do the trick. – Leo May 23 '19 at 14:05
  • Not sure I understand. When you push the app, the static content is pushed with the app (css, images, etc.). Fonts can be part of it. For IBM Plex font, there is a package https://www.npmjs.com/package/@ibm/plex that your code could depend on. – data_henrik May 23 '19 at 14:15
  • html-pdf relies on phantomjs. Phantomjs reads a html that contains a css that refers to a custom font, but when it generates the pdf, it does not embed the font if the font is not installed in the OS. So just adding the font as a node dependency does not work because basically phantomjs and html-pdf are broken. So the only workaround here is trying to automate the font installation on the IBM Cloud runtime (which I suppose is just some default docker image) after the VM is instantiated. The problem is that IBM Cloud runtimes seem not to have this kind of flexibility. – Leo May 23 '19 at 18:52
  • Instead, I'd have to create my own node docker image containing the font in the right place, or maybe, I could try something like https://docs.npmjs.com/misc/scripts to add some hook after the VM is ready to trigger the installation of the missing font in the server, but I feel it's not the right way to do it. – Leo May 23 '19 at 18:53
  • Are you talking Cloud Foundry on IBM Cloud, Kubernetes / Container or VM(ware)? All are different – data_henrik May 23 '19 at 19:16
  • That is Cloud Foundry – data_henrik May 24 '19 at 13:18
0

As far as I understand, you are trying to generate pdf from static html files which uses fonts that are already installed on a system, making them dependable on the system's available fonts. Why don't you fix the fonts in css and embed them as base64 encoded fonts? I once used this website to create that: https://transfonter.org

pegasuspect
  • 991
  • 4
  • 15