Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF file creation using html-pdf is not working in my deployment server?

I am using html-pdf npm module to create a pdf file, everything is working fine in my local environment (mac, windows). But when we deployed the same code in our amazon ec2 server create() of html-pdf is not able to create file nor giving me any error. I almost tried all possible ways by exception handling, absolute path etc. None of them work. Can anyone help me on this please. my code is

function generatePdf(content, options, callback) {
    var fileName = new Date().getTime() + Math.random() + '.pdf';
    pdf.create(content, options).toFile('../uploads/' + fileName, function(error, response) {
        if (error) {
            callback(error);
        } else {
            callback({
                fileName: fileName,
                filePath: response.filename
            });
        }                                
    });
}

Here error is {} and the response is {}

like image 307
Syam Danda Avatar asked May 21 '16 13:05

Syam Danda


3 Answers

There is absolutely nothing wrong with the above code. However, the reason why html-pdf lib is not throwing any error nor creating a file while pdf.create(...).toFile is due to the errors in html-pdf installed version in your environment. We can determine it by checking

npm list html-pdf and if returns anything like npm ERR! code 1 then do

npm uninstall html-pdf and then do again
npm install html-pdf 

and repeat above step to make sure there are no errors in current installed version of html-pdf.

like image 72
Sridhar Gudimela Avatar answered Nov 16 '22 19:11

Sridhar Gudimela


I'd the same problem and after searching on google, following through the posts on StackOverflow & GitHub threads doesn't work out for me.

Later I realize and checked for Phantomjs and got that was missing on the server.

I installed using the below command on ubuntu server.

sudo apt-get install phantomjs

Have fun if this works for you.

like image 29
Tulsi Ram Avatar answered Nov 16 '22 20:11

Tulsi Ram


I had a same issue. We were using docker for the deployment.i.e, "node:12.17.0-alpine"

I added following on Dockerfile

 ENV PHANTOMJS_VERSION=2.1.1
 ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
 ENV PATH=$PATH:/home/node/.npm-global/bin
 RUN apk update && apk add --no-cache fontconfig curl curl-dev && \
    cd /tmp && curl -Ls https://github.com/dustinblackman/phantomized/releases/download/${PHANTOMJS_VERSION}/dockerized-phantomjs.tar.gz | tar xz && \
    cp -R lib lib64 / && \
    cp -R usr/lib/x86_64-linux-gnu /usr/lib && \
    cp -R usr/share /usr/share && \
    cp -R etc/fonts /etc && \
    curl -k -Ls https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-${PHANTOMJS_VERSION}-linux-x86_64.tar.bz2 | tar -jxf - && \
    cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs

like image 1
Narayan Shrestha Avatar answered Nov 16 '22 21:11

Narayan Shrestha