车把未渲染

时间:2018-07-30 19:49:59

标签: node.js handlebars.js puppeteer

我的服务中包含以下代码:

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

const compile = async (fileName, data) => {

  const filePath = path.join(process.cwd(), 'src/templates', `${fileName}.hbs`);
  const html = await fs.readFile(filePath, 'utf-8');
  return await hbs.compile(html)(data);
}

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

    var d = {
      "mes": "julho",
      "author": "Felipe Paz"
    }

    let content = await compile('folha_ponto', d);
    console.log(content);

    await page.setContent(content);
    await page.emulateMedia('screen');
    await page.pdf({
      path: 'dist/pdf/teste.pdf',
      format: 'A4',
      printBackground: true
    })

    console.log('done');
    await browser.close();

  } catch (error) {
    console.log('Errors => ', error);
  }
}; 

在我的控制器内部,我称之为:

servicconst folhaPonto = const folhaPonto = require('../../../services/folha_ponto');

exports.folhaPonto = async (req, res, next) => {
  let d = await folhaPonto.folhaPonto();
  res.status(200).send('gerada folha ponto');
}

但是我的hbs文件未呈现任何内容。我可以创建PDF文件,但只能使用静态HTML。这里有人可以告诉我哪里出了问题吗?

0 个答案:

没有答案