将pdf2image与node.js和centos一起使用
我正在使用pdf2image
来构建一个node.js应用程序,该应用程序将pdf文件转换为png。 如官方repo 说,依赖项:ghostscript和GraphicsMagick。两者安装在我本地的Windows机器中。
现在,我可以使用此代码将PDF文件的缓冲区转换为PNG图像的缓冲区:
const fromBuffer = require('pdf2pic').fromBuffer;
convertPdfToImg = async (pdfBuffer) => {
const pdf2picOptions = {
format: 'png',
width: 4000,
height: 5176,
density: 330,
savePath: './output',
};
const convert = fromBuffer(pdfBuffer, pdf2picOptions);
const pageOutput = await convert(1, true);
const pngBuffer = Buffer.from(pageOutput.base64, 'base64');
return pngBuffer;
};
一切正常,每个人都很高兴! (目前)
现在的问题是我需要在Linux环境(CentOS流)中将应用程序部署到生产中,因此我在服务器上安装了依赖项,请确保使用在本地Windows机器上安装的相同版本(<<代码> Ghostscript 9.52 和GraphicsMagick 1.3.35 2020-02-23 Q16
)。但是,上面提到的代码片段将不再起作用,并返回一个空的缓冲区。
经过一些调试后,我注意到pageOutput.base64
是空的,这意味着可能无法正确安装ghostscript和GraphicsMagick(因为我在Windows中没有依赖项的代码测试了代码,并且给我空了pageOutput。 base64
)。
我再次检查了是否通过键入gs -version
将GhostScript安装在CentOS中,并给出了9.52
作为输出。
另外,我通过键入gm版本
来检查GraphicsMagick,这给了我以下输出:
GraphicsMagick 1.3.35 2020-02-23 Q16 http://www.GraphicsMagick.org/
Copyright (C) 2002-2020 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.
Feature Support:
Native Thread Safe yes
Large Files (> 32 bit) yes
Large Memory (> 32 bit) yes
BZIP no
DPS no
FlashPix no
FreeType no
Ghostscript (Library) no
JBIG no
JPEG-2000 no
JPEG no
Little CMS no
Loadable Modules no
Solaris mtmalloc no
Google perftools tcmalloc no
OpenMP yes (201511 "4.5")
PNG no
TIFF no
TRIO no
Solaris umem no
WebP no
WMF no
X11 no
XML yes
ZLIB yes
Host type: x86_64-pc-linux-gnu
Configured using the command:
./configure '--with-quantum-depth=16'
Final Build Parameters:
CC = gcc
CFLAGS = -fopenmp -g -O2 -Wall -pthread
CPPFLAGS = -I/usr/include/libxml2
CXX = g++
CXXFLAGS = -pthread
LDFLAGS =
LIBS = -llzma -lxml2 -lz -lm -lpthread
请注意,这两个依赖关系是直接从源安装的。
所以我的问题是: 如何使pdf2image
与部署在Linux系统(特别是CentOS流)上的Node.js一起使用? CentOS中的依赖性安装有问题吗?
谢谢。
I'm using pdf2image
to build a Node.js application that convert PDF file to PNG.
As the readme of the official repo says, pdf2image
requires two external dependencies: Ghostscript and GraphicsMagick. The two are installed in my local Windows machine.
Now I'm able to convert a buffer of PDF file to a buffer of PNG image with this code:
const fromBuffer = require('pdf2pic').fromBuffer;
convertPdfToImg = async (pdfBuffer) => {
const pdf2picOptions = {
format: 'png',
width: 4000,
height: 5176,
density: 330,
savePath: './output',
};
const convert = fromBuffer(pdfBuffer, pdf2picOptions);
const pageOutput = await convert(1, true);
const pngBuffer = Buffer.from(pageOutput.base64, 'base64');
return pngBuffer;
};
Everything works fine and everyone is happy! (For now)
The problem right now is that I need to deploy the application to production in a Linux environment (CentOS Stream), so I installed the dependencies on the server making sure to use the same versions installed on my local Windows machine (Ghostscript 9.52
and GraphicsMagick 1.3.35 2020-02-23 Q16
). However, the code snippet mentioned above won't work anymore and returns an empty buffer.
After some debugging, I noticed that pageOutput.base64
is empty, which means probably Ghostscript and GraphicsMagick are not installed correctly (because I tested the code without the dependencies in Windows and gives me empty pageOutput.base64
as well).
I checked again whether Ghostscript is installed in Centos by typing gs --version
and it gave 9.52
as output.
Also, I checked GraphicsMagick by typing gm version
which gave me the following output:
GraphicsMagick 1.3.35 2020-02-23 Q16 http://www.GraphicsMagick.org/
Copyright (C) 2002-2020 GraphicsMagick Group.
Additional copyrights and licenses apply to this software.
See http://www.GraphicsMagick.org/www/Copyright.html for details.
Feature Support:
Native Thread Safe yes
Large Files (> 32 bit) yes
Large Memory (> 32 bit) yes
BZIP no
DPS no
FlashPix no
FreeType no
Ghostscript (Library) no
JBIG no
JPEG-2000 no
JPEG no
Little CMS no
Loadable Modules no
Solaris mtmalloc no
Google perftools tcmalloc no
OpenMP yes (201511 "4.5")
PNG no
TIFF no
TRIO no
Solaris umem no
WebP no
WMF no
X11 no
XML yes
ZLIB yes
Host type: x86_64-pc-linux-gnu
Configured using the command:
./configure '--with-quantum-depth=16'
Final Build Parameters:
CC = gcc
CFLAGS = -fopenmp -g -O2 -Wall -pthread
CPPFLAGS = -I/usr/include/libxml2
CXX = g++
CXXFLAGS = -pthread
LDFLAGS =
LIBS = -llzma -lxml2 -lz -lm -lpthread
Note that the two dependencies are installed directly from source.
So my question is:
How to make pdf2image
works with Node.js deployed on linux system (specifically CentOS Stream)?
Is there something wrong with my dependencies installation in CentOS?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终使用 pdf-to-to-t-to-png-converter 依赖项和在Windows和CentOS中都可以完美地工作。
这是我的做法:
我希望我早些时候知道这个很棒的公用事业。
I ended up using pdf-to-png-converter, It requires 0 external dependencies and works perfectly in both Windows and CentOS.
Here is how I did it:
I wish I knew this awesome utility earlier.