How to Use A Custom Font In Puppeteer Running on an Ubuntu Server?

4 minutes read

To use a custom font in Puppeteer running on an Ubuntu server, you will first need to install the font on the server. You can do this by downloading the font file and placing it in a directory on the server.


Next, you will need to specify the path to the font file in your Puppeteer script. You can do this by using the font-family property in your CSS styles or by setting the font property directly in your Puppeteer code.


For example, if you have downloaded a font file named custom-font.ttf and placed it in a directory called fonts, you can specify the font in your Puppeteer script like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<style>
  @font-face {
    font-family: 'CustomFont';
    src: url('fonts/custom-font.ttf');
  }
</style>

page.addStyleTag({
  content: `
    body {
      font-family: 'CustomFont', sans-serif;
    }
  `
});


This code will load the custom font file and apply it to the body element of the page. Make sure to adjust the font family name and file path to match your specific font file and directory.


Once you have added the custom font to your Puppeteer script, you should be able to see it rendered correctly when running Puppeteer on your Ubuntu server.


How to inject custom fonts dynamically in puppeteer on ubuntu server?

To inject custom fonts dynamically in Puppeteer on an Ubuntu server, you can follow these steps:

  1. Upload the custom fonts files to the Ubuntu server. You can use an FTP client or SCP to transfer the font files to the server.
  2. Install the necessary packages on the Ubuntu server to support font rendering. You may need to install Fontconfig and other relevant font packages. You can do this by running the following command:
1
sudo apt-get install fontconfig


  1. Update the Fontconfig cache by running the following command:
1
fc-cache -f -v


  1. Use Puppeteer's addStyleTag method to inject a @font-face CSS rule that references the custom fonts. Here's an example code snippet that demonstrates how to inject custom fonts dynamically in Puppeteer:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('https://example.com');

  await page.addStyleTag({
    content: `
      @font-face {
        font-family: 'CustomFont';
        src: url('path/to/custom-font.ttf') format('truetype');
      }
      body {
        font-family: 'CustomFont', sans-serif;
      }
    `
  });

  await page.screenshot({ path: 'example.png' });

  await browser.close();
})();


Replace https://example.com with the URL of the page where you want to use the custom fonts. Update path/to/custom-font.ttf with the path to the custom font file on the Ubuntu server.

  1. Run the Puppeteer script on the Ubuntu server. You can use Node.js to execute the script.


By following these steps, you can dynamically inject custom fonts in Puppeteer on an Ubuntu server.


What is the file format for custom fonts in puppeteer?

The file format for custom fonts in Puppeteer is usually a TrueType (TTF) or OpenType (OTF) font file. These font files can be used to customize the text style and appearance when generating PDFs or taking screenshots with Puppeteer.


How to list available fonts in puppeteer on ubuntu server?

To list available fonts in Puppeteer on an Ubuntu server, you can use the following steps:

  1. First, make sure you have Puppeteer installed. If not, you can install it using npm:
1
npm install puppeteer


  1. Create a new JavaScript file and add the following code to launch Puppeteer and get the list of available fonts:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto('data:text/html,<!DOCTYPE html><html><head><title>Available Fonts</title></head><body></body></html>');

  const fonts = await page.evaluate(() => {
    const fonts = [];
    const allFonts = getComputedStyle(document.body).fontFamily.split(',');
    allFonts.forEach(font => {
      fonts.push(font.trim());
    });
    return fonts;
  });

  console.log('Available Fonts:');
  console.log(fonts);

  await browser.close();
})();


  1. Run the JavaScript file using Node.js:
1
node list_fonts.js


This script will launch Puppeteer, open a new page with no content, and then fetch the list of available fonts on that page by parsing the computed styles of the body element. The list of fonts will be printed to the console.


Please note that this script assumes that all the fonts available on the server are also available within the Puppeteer browser environment. If you are using custom fonts or have specific font requirements, you may need to ensure that those fonts are installed and accessible within the Puppeteer environment as well.

Facebook Twitter LinkedIn Telegram

Related Posts:

When encountering the error &#34;page crashed!&#34; in Puppeteer, it typically means that the page being controlled by Puppeteer has crashed, either due to a JavaScript error, a memory leak, or some other issue with the code being executed on the page.To handl...
To add custom fonts to the Quill.js editor, you can start by downloading the desired font files in various formats (such as .woff, .woff2, .ttf) from a reliable source or by generating them using online tools. Once you have the font files, you can host them on...
To deploy a React.js app in an Ubuntu server with Bitbucket pipeline, you first need to create a Bitbucket pipeline YAML file in the root directory of your project. Define the deployment script in the pipeline file, which will include building the project and ...
To change the dropdown text in Quill.js, you can modify the options in the dropdown menu by editing the configuration settings of the Quill editor. You can customize the text displayed in the dropdown menu for various formatting options such as font size, font...
To display custom taxonomy of the WooCommerce product, you can use the get_the_terms() function to retrieve the custom taxonomy terms associated with a specific product.First, you need to get the product ID using the get_the_ID() function. Then, use the get_th...