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 545 forks source link

.toFile() did not work in docker environment #653

Open KhairulHabibSamaon opened 2 years ago

KhairulHabibSamaon commented 2 years ago
FROM mhart/alpine-node:16.4.2

WORKDIR /srv
ADD . .
RUN npm install

EXPOSE 3000
CMD ["node", "index.js"]

this is how i set up the docker file.

        const content = await compile(template, context)
        pdf.create(content,
            { format: 'Letter',
                footer: { contents: footer, height: '20mm' },
                header: { content: '', height: '6mm' },
                timeout: 540000 })
            .toFile(path, (err, response) => {
            if (err) {
                fs.unlinkSync(path)
                return console.log(err);
            }
            const data = fs.readFileSync(path)
            res.setHeader('Content-Type', 'application/pdf')
            res.setHeader('Content-Length', fs.statSync(path).size + 200)
            res.send(data)
            return fs.unlinkSync(path)
          });

and this i how i manage create pdf, just to mention i've tried .toFile and .toBuffer both work in my local, but when build docker image it's not work for both with this below error

Error: spawn Unknown system error -8

at ChildProcess.spawn (node:internal/child_process:415:11)

at Object.spawn (node:child_process:698:9)

at PDF.PdfExec [as exec] (/srv/node_modules/html-pdf/lib/pdf.js:89:28)

at PDF.PdfToFile [as toFile] (/srv/node_modules/html-pdf/lib/pdf.js:85:8)

at exports.renderPdf (/srv/service/PdfRenderer.js:27:14)

at processTicksAndRejections (node:internal/process/task_queues:96:5)

at async /srv/routes/Report.js:63:16 {

errno: -8,

  code: 'Unknown system error -8',

  syscall: 'spawn'

}

i've tried to install phantom prebuild and adding phantomPath but it's still return the same error. is there any way that i can do to fix this problem

ChenMatsu commented 2 years ago

Try using image:node:16 instead of a alpine version of node.

Sushant-Borsania commented 2 years ago

hi, @KhairulHabibSamaon any resolution/alternate you found on this issue?