0

I use phantomjs and puppeteer on the same system. I am using a font file to support emoji in PDF exports. phantomjs crashed when I use that font, but not chromium browser used in puppeteer. Is there any way I can load the font file specifically for chrome only without making the font apply system wide from /usr/share/fonts. I am on CentOS 7 machine.

mohsinulhaq
  • 1,059
  • 1
  • 18
  • 31
  • 1
    If the presence of font is a problem in itself, then running Chrome as a different user (with sudo/gksudo/kdesudo) and planting font in that user's `~/.fonts` directory could be a solution. Extremely ugly one, but perhaps working. – Frax Apr 19 '18 at 10:59

1 Answers1

1

You can convert the font into a web font, and then embed the font into the web page using page.addStyleTag():

await page.addStyleTag({
    'content' : `
        @font-face {
            font-family:'[font-name]';
            src:url('[font-file].woff2') format('woff2'),
                url('[font-file].woff') format('woff');
            font-weight:normal;
            font-style:normal;
        }
    `
});

Then, you can run page.pdf() to download the web page as a PDF containing your custom font.

This will allow you to use the font on a case-by-case basis without needing to install it on your machine.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165