bdrtsky
bdrtsky

Reputation: 233

How to point Puppeteer to local images/fonts?

I want to generate images from the HTML string using Puppeteer. For now I have something like this:

const html = _.template(`
<html>
<body>
  <div class="testing">
    <h1>Hello {{ test }}!</h1>
    <img src="./1.jpg" alt="alt text" />
  </div>
</body>
</html>
`)

const browser = await puppeteer.launch()
const page = await browser.newPage()
const data = html({
  test: 'World'
})
await page.setContent(data)
const take = await page.$('.testing')
await take.screenshot({
  path: 'screenshot.png',
  omitBackground: true
})

The problem is, Puppeteer doesn't load image, and I am not sure how to point it to him? The image is located in the same directory as a script.

Beside image, I want to load custom font, how to do that?

Upvotes: 23

Views: 17431

Answers (3)

  HoneyJohny
HoneyJohny

Reputation: 1

you need to create fake html that will download your local fonts

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Order</title>
    <style>
        @font-face {
            font-family: 'Your Font';
            src: url('./fonts/YourFont_REGULAR.woff2') format('woff2');
            font-style: normal;
            font-weight: 400;
            font-display: swap;
        }
        @font-face {
            font-family: 'Your Font';
            src: url('./fonts/YourFont_BOLD.woff2') format('woff2');
            font-style: normal;
            font-weight: 600;
            font-display: swap;
        }

        body{
            font-family: 'Your Font',serif;
        }
    </style>
</head>
<body>
<div>hello world</div>
</body>
</html>

then puppeteer

await page.goto('file://' + __dirname + '/fake-font-html.html') // loads fonts
await page.setContent(htmlContent, { waitUntil: 'domcontentloaded' });

and in your real html you also need download fonts

@font-face {
            font-family: 'Your Font';
            src: url('fonts/YourFont_REGULAR.woff2') format('woff2');
            font-style: normal;
            font-weight: 400;
            font-display: swap;
        }

Upvotes: 0

volvpavl
volvpavl

Reputation: 644

Found a relevant issue on their github. To sum up, currently the options seem to be as follows:

  1. Serve your content to yourself through localhost or 3rd party server.
  2. Inline everything into HTML (base64 encode where required).
  3. When applicable, probably worth trying goto('file://...') instead of setContent(). Maybe there are chances it will work with the following:
puppeteer.launch({ args: ['--allow-file-access-from-files', '--enable-local-file-accesses'] });

But I can't confirm whether the last one would work or not. In my env, goto('file://...') constantly gives net::ERR_ABORTED.

Upvotes: 2

vsemozhebuty
vsemozhebuty

Reputation: 13812

The page URL is about:blank and Chrome does not allow to load local resources in non-local pages.

So maybe something like this?

'use strict';

const { readFileSync } = require('fs');
const puppeteer = require('puppeteer');

(async function main() {
  try {
    const browser = await puppeteer.launch({ headless: false });
    const [page] = await browser.pages();

    const html = `
      <html>
      <body>
        <div class="testing">
          <h1>Hello World!</h1>
          <img src="data:image/jpeg;base64,${
            readFileSync('1.jpg').toString('base64')
          }" alt="alt text" />
        </div>
      </body>
      </html>
    `;

    await page.setContent(html);
    const take = await page.$('.testing');
    await take.screenshot({
      path: 'screenshot.png',
      omitBackground: true
    });

    // await browser.close();
  } catch (err) {
    console.error(err);
  }
})();

Upvotes: 24

Related Questions