Handlebars loading png images

Viewed 456

I'm building a pdf report with handlebars and puppeteer to save into my db. The pdf works fine except that I can't seem to get images that I have stored in an assets directory to load. I am console logging the filePath that I am traversing to when attempting to get the images and they come through appropriately. Just not sure why the images are not loading, any help appreciated.

Here is my code so far.

const puppeteer = require("puppeteer");
const hbs = require("handlebars");
const fs = require("fs-extra");
const path = require("path");

// compiles the handlebars docs
const compile = async (templateName, data) => {
  const filePath = path.join(__dirname, "templates", `${templateName}.hbs`);
  if (!filePath) {
    throw new Error(`Could not find ${templateName}.hbs in generatePDF`);
  }
  const html = await fs.readFile(filePath, "utf-8");
  return hbs.compile(html)(data);
};

// helper for getting the images
hbs.registerHelper("getIntro", (context, idx) => {
  const filePath = path.join(
    __dirname,
    "assets",
    `${context}_intro_${idx}.png`
  );
  return filePath;
});


// use puppeteer to take in compiled hbs doc and create a pdf
const generatePDF = async (fileName, data) => {
  const preparedData = prepareDataForPDF(data);
  const browser = await puppeteer.launch({
    args: ["--no-sandbox"],
    headless: true
  });
  const page = await browser.newPage();
  const content = await compile(fileName, preparedData);
  await page.goto(`data: text/html;charset=UTF-8, ${content}`, {
    waitUntil: "networkidle0"
  });
  await page.setContent(content);
  await page.emulateMedia("screen");
  await page.waitFor(100);

  const pdf = await page.pdf({
    format: "A4",
    printBackground: true
  });
  return pdf;
};

module.exports = generatePDF;
<!-- handlebars partial that handles image loading -->
    {{#loop 14}}
        <div class="page">
          <img src="{{getIntro assessmentType this}}" class="intro-page-content">
        </div>
    {{/loop}}

<!-- used like this in my main file -->
    {{> intro assessmentType="Leadership"}}

0 Answers
Related