marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 541 forks source link

static file imports are not working properly with pug #627

Open johann-su opened 3 years ago

johann-su commented 3 years ago

I am using pug as the templating engine for the html of the pdf. I import a few files (css, images etc.) inside the pug file but they don't show up in the template. I am using the base to point to the static files directory.

Here is my code: app.ts

app.post('/create-handout', async (req, res) => {
    const handout = pug.renderFile('./src/documents/handout.pug', req.body)
    pdf.create(handout, {
        format: 'A4',
        base: 'file://' + path.resolve('./src/documents/static') + '/'
    }).toFile('./src/result/result.pdf', (err) => {
        if (err) {
            res.send(Promise.reject());
        }
        res.send(Promise.resolve());
    });
})

handout.pug

html 
    head 
        title Handout
        link(rel='stylesheet', type='text/css', href='css/handout.css') 
    body
        header
            img(src="img/header.svg")
...
tmetade commented 2 years ago

You need to add localUrlAccess: true in your options as well as set the full file URL, file:///Users/.../img/header.svg https://github.com/marcbachmann/node-html-pdf/issues/618