node-html-pdf icon indicating copy to clipboard operation
node-html-pdf copied to clipboard

.toFile() did not work in docker environment

Open KhairulHabibSamaon opened this issue 3 years ago • 2 comments

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

KhairulHabibSamaon avatar Oct 20 '21 02:10 KhairulHabibSamaon

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

ChenMatsu avatar Dec 27 '21 02:12 ChenMatsu

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

Sushant-Borsania avatar May 10 '22 00:05 Sushant-Borsania