简体   繁体   English

使用 html-pdf 创建 PDF 文件在我的部署服务器中不起作用?

[英]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).我正在使用 html-pdf npm 模块创建一个 pdf 文件,在我的本地环境(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.但是当我们在我们的亚马逊 ec2 服务器中部署相同的代码时,html-pdf 的 create() 无法创建文件,也没有给我任何错误。 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 {}这里的错误是 {},响应是 {}

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.但是,在pdf.create(...).toFile html-pdf lib 没有抛出任何错误或创建文件的pdf.create(...).toFile是由于您的环境中 html-pdf 安装版本中的错误。 We can determine it by checking我们可以通过检查来确定

npm list html-pdf and if returns anything like npm ERR! npm list html-pdf如果返回类似 npm ERR! code 1 then do代码1然后做

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.并重复上述步骤以确保当前安装的 html-pdf 版本没有错误。

I'd the same problem and after searching on google, following through the posts on StackOverflow & GitHub threads doesn't work out for me.我遇到了同样的问题,在谷歌上搜索后,通过 StackOverflow 和 GitHub 线程上的帖子对我来说不起作用。

Later I realize and checked for Phantomjs and got that was missing on the server.后来我意识到并检查了 Phantomjs 并发现服务器上缺少它。

I installed using the below command on ubuntu server.我在 ubuntu 服务器上使用以下命令安装。

sudo apt-get install phantomjs

Have fun if this works for you.如果这对您有用,请玩得开心。

I had a same issue.我有同样的问题。 We were using docker for the deployment.ie, "node:12.17.0-alpine"我们使用 docker 进行部署。即,“node:12.17.0-alpine”

I added following on Dockerfile我在 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

A few things you can use to debug the issue.您可以使用一些东西来调试问题。

  1. Can you use try-catch method instead of if-else?你能用 try-catch 方法代替 if-else 吗? and console.log(error) in catch.和 console.log(error) 在 catch 中。
  2. Use __dirname instead of relative file address.使用 __dirname 而不是相对文件地址。 ie;即; __dirname + '/uploads' instead of '../uploads/' __dirname + '/uploads'而不是'../uploads/'
  3. Try using fs module to create a writeable stream尝试使用fs模块创建可写流

it seems all the problem is in this line.似乎所有的问题都在这一行。 Commenting it makes the error go away:评论它会使错误消失:

# System default
openssl_conf = default_conf

/etc/ssl/openssl.cnf /etc/ssl/openssl.cnf

html-pdf is a npm library which is used to convert HTML file to pdf. html-pdf 是一个 npm 库,用于将 HTML 文件转换为 pdf。

Install html-pdf globally using this command:使用以下命令全局安装 html-pdf:

npm install -g html-pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM