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.55k stars 544 forks source link

stream (.toStream) not generated after deploying to k8s #694

Closed rohan-baranwal closed 1 year ago

rohan-baranwal commented 1 year ago

This is the code I am using on my local to get a pdf stream and then upload it right away. Problem is that it is working fine on my local but after deployment there is nothing getting generated in .toStream() extension method and not showing any proper either. In err I am getting {} after deployment (which doesn't tell me anything - and so far I have only deduced that it is not generating any stream) and null on local.

Using following versions node - v16.15.1 npm - v8.11.0 html-pdf - v3.1.0

codefile.ts

const pdf = require('html-pdf')
const htmlContent: string = '<........>' // some html content is there
const options = {
    border: {
        bottom: '.7in', left: '.5in', right: '.5in', top: '.5in',
    },
    footer: {
        contents: {
            default: '<font face="Verdana" color="black" size= "1">'
                + '<div style="margin-left:0px"><span>Page: {{page}}</span>/<span>{{pages}}</span></div></font>',
            },
            height: '10mm',
        },
        format: 'Letter',
        height: '10.5in',
        width: '8in',
        timeout: 5000
    };
    const result = pdf.create(htmlContent, options).toStream((err: any, stream: any) => {
        if (err) {
            reject(err);
        } else {
            const formData = {
            file: {
                options: {
                    contentType: 'multipart/form-data;boundary=123456',
                    filename: 'abc.pdf',
                },
                value: stream,
            },
        };
        resolve(formData);
    }
})

Dockerfile

FROM abc.com/prd/abc-node:14.16.1 AS build-container
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install
RUN npm test
RUN npm prune --production

FROM abc.com/prd/abc-node:14.16.1
USER node
WORKDIR /usr/src/app
COPY package.json /usr/src/app
COPY --from=build-container /usr/src/app/node_modules /usr/src/app/node_modules 
COPY --from=build-container /usr/src/app/dist /usr/src/app/dist
CMD ["/bin/bash", "-c", "npm start"]

Anyone knows how can I debug/fix with this?

phil-warner commented 1 year ago

Hi @rohan-baranwal. Did you figure out what the problem is? I have the same thing (i.e. no errors, but I get "Failed to load PDF Document" in the browser). It works fine on my test site but not on the production server.

rohan-baranwal commented 1 year ago

@phil-warner Yes, I added

pdf.create(html, {
  childProcessOptions: {
    env: {
      OPENSSL_CONF: '/dev/null',
    },
  }
});

in options of codefile.ts.

Please refer this link for more details.