ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 384 forks source link

How to compile hbs and json to set as puppeteer content? #260

Closed leomarques3 closed 5 years ago

leomarques3 commented 5 years ago

Hi guys, I was developing an app to generate an pp using puppeteer and express-handlebars, but i can't figure out how to compile the json and the template using express-handlebars. const express = require('express'); const exphbs = require('express-handlebars'); const bodyParser = require('body-parser'); const path = require('path');

const app = express();

app.engine('hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' })); app.set('view engine', 'hbs'); app.use(bodyParser.json()); app.use(express.static(path.join(__dirname, 'public')))

I also have a json file that i want to compile together and then use it in puppeteer: app.get('/export/pdf', async (req, res) => { try { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.setContent(html, data); const buffer = await page.pdf({ format: 'A4', printBackground: true }); res.type('application/pdf'); res.send(buffer) await browser.close()

I know how to do this using 'handlebars' only, but i didn't find how to do the same with express-handlebars.

UziTech commented 5 years ago

To use express-handlebars to render a handlebars file you would use res.render('filename', config)

You can look at the basic example for rendering a handlebars file.