由于 poppler,在 CentOS 上的 Python 3.6 中安装 pdftotext 时出现问题

Issue installing pdftotext in Python 3.6 on CentOS due to poppler

我在 CentOS 的 Python 3.6 (Anaconda 5.1.0) 中安装 pdftotext 时遇到一些问题。

先做一些快速说明:

我按照 github 存储库中的说明进行了操作,并且已经尝试过这一步:

Fedora、Red Hat 和朋友:

sudo yum install gcc-c++ pkgconfig poppler-cpp-devel python-devel redhat-rpm-config

但问题似乎出在 poppler-cpp-devel 上。我在 yum search poppler:

中没有看到那个包裹
============================= N/S Matched: poppler =============================
poppler-devel.i686 : Libraries and headers for poppler
poppler-devel.x86_64 : Libraries and headers for poppler
poppler-glib.i686 : Glib wrapper for poppler
poppler-glib.x86_64 : Glib wrapper for poppler
poppler-qt.i686 : Qt3 wrapper for poppler
poppler-qt.x86_64 : Qt3 wrapper for poppler
poppler-qt4.i686 : Qt4 wrapper for poppler
poppler-qt4.x86_64 : Qt4 wrapper for poppler
poppler.i686 : PDF rendering library
poppler.x86_64 : PDF rendering library
poppler-data.noarch : Encoding files
poppler-glib-devel.i686 : Development files for glib wrapper
poppler-glib-devel.x86_64 : Development files for glib wrapper
poppler-qt-devel.i686 : Development files for Qt3 wrapper
poppler-qt-devel.x86_64 : Development files for Qt3 wrapper
poppler-qt4-devel.i686 : Development files for Qt4 wrapper
poppler-qt4-devel.x86_64 : Development files for Qt4 wrapper
poppler-utils.x86_64 : Command line utilities for converting PDF files

我的 IT 小组向我提供了他们尝试过的说明,我尝试安装 poppler-develpoppler-glib。但每次我尝试 pip install pdftotext 我都会得到以下输出:

[root@localhost stack]# pip install pdftotext
Collecting pdftotext
  Using cached https://files.pythonhosted.org/packages/21/35/60094dbadd9de2035873390b1cac25e01da605844eba6a07a53a82fa4adc/pdftotext-2.1.1.tar.gz
Building wheels for collected packages: pdftotext
  Building wheel for pdftotext (setup.py) ... error
  Complete output from command /root/anaconda3/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-1mu2f1n2/pdftotext/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-khm9zova --python-tag cp36:
  /root/anaconda3/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
    warnings.warn(msg)
  running bdist_wheel
  running build
  running build_ext
  building 'pdftotext' extension
  creating build
  creating build/temp.linux-x86_64-3.6
  gcc -pthread -B /root/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPOPPLER_CPP_AT_LEAST_0_30_0=0 -I/root/anaconda3/include/python3.6m -c pdftotext.cpp -o build/temp.linux-x86_64-3.6/pdftotext.o -Wall
  cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
  pdftotext.cpp:3:42: error: poppler/cpp/poppler-document.h: No such file or directory
  pdftotext.cpp:4:40: error: poppler/cpp/poppler-global.h: No such file or directory
  pdftotext.cpp:5:38: error: poppler/cpp/poppler-page.h: No such file or directory
  pdftotext.cpp:20: error: ‘poppler’ has not been declared
  pdftotext.cpp:20: error: ISO C++ forbids declaration of ‘document’ with no type
  pdftotext.cpp:20: error: expected ‘;’ before ‘*’ token
  pdftotext.cpp: In function ‘void PDF_clear(PDF*)’:
  pdftotext.cpp:26: error: ‘struct PDF’ has no member named ‘doc’
  pdftotext.cpp:27: error: ‘struct PDF’ has no member named ‘doc’
  pdftotext.cpp: In function ‘int PDF_create_doc(PDF*)’:
  pdftotext.cpp:66: error: ‘struct PDF’ has no member named ‘doc’
  pdftotext.cpp:66: error: ‘poppler’ has not been declared
  pdftotext.cpp:67: error: ‘struct PDF’ has no member named ‘doc’
  pdftotext.cpp: In function ‘int PDF_unlock(PDF*, char*)’:
  pdftotext.cpp:75: error: ‘struct PDF’ has no member named ‘doc’
  pdftotext.cpp: In function ‘int PDF_init(PDF*, PyObject*, PyObject*)’:
  pdftotext.cpp:105: error: ‘struct PDF’ has no member named ‘doc’
  pdftotext.cpp: In function ‘PyObject* PDF_read_page(PDF*, int)’:
  pdftotext.cpp:119: error: ‘poppler’ has not been declared
  pdftotext.cpp:119: error: expected initializer before ‘*’ token
  pdftotext.cpp:120: error: ‘poppler’ has not been declared
  pdftotext.cpp:120: error: expected ‘;’ before ‘layout_mode’
  pdftotext.cpp:123: error: ‘page’ was not declared in this scope
  pdftotext.cpp:123: error: ‘struct PDF’ has no member named ‘doc’
  pdftotext.cpp:129: error: ‘poppler’ has not been declared
  pdftotext.cpp:129: error: expected initializer before ‘rect’
  pdftotext.cpp:130: error: ‘rect’ was not declared in this scope
  pdftotext.cpp:133: error: ‘layout_mode’ was not declared in this scope
  pdftotext.cpp:133: error: ‘poppler’ has not been declared
  pdftotext.cpp:135: error: ‘poppler’ has not been declared
  pdftotext.cpp:137: error: ‘poppler’ has not been declared
  pdftotext.cpp:138: error: type ‘<type error>’ argument given to ‘delete’, expected pointer
  error: command 'gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for pdftotext
  Running setup.py clean for pdftotext
Failed to build pdftotext
Installing collected packages: pdftotext
  Running setup.py install for pdftotext ... error
    Complete output from command /root/anaconda3/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-1mu2f1n2/pdftotext/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-ghuhvuhl/install-record.txt --single-version-externally-managed --compile:
    /root/anaconda3/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    running install
    running build
    running build_ext
    building 'pdftotext' extension
    creating build
    creating build/temp.linux-x86_64-3.6
    gcc -pthread -B /root/anaconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DPOPPLER_CPP_AT_LEAST_0_30_0=0 -I/root/anaconda3/include/python3.6m -c pdftotext.cpp -o build/temp.linux-x86_64-3.6/pdftotext.o -Wall
    cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
    pdftotext.cpp:3:42: error: poppler/cpp/poppler-document.h: No such file or directory
    pdftotext.cpp:4:40: error: poppler/cpp/poppler-global.h: No such file or directory
    pdftotext.cpp:5:38: error: poppler/cpp/poppler-page.h: No such file or directory
    pdftotext.cpp:20: error: ‘poppler’ has not been declared
    pdftotext.cpp:20: error: ISO C++ forbids declaration of ‘document’ with no type
    pdftotext.cpp:20: error: expected ‘;’ before ‘*’ token
    pdftotext.cpp: In function ‘void PDF_clear(PDF*)’:
    pdftotext.cpp:26: error: ‘struct PDF’ has no member named ‘doc’
    pdftotext.cpp:27: error: ‘struct PDF’ has no member named ‘doc’
    pdftotext.cpp: In function ‘int PDF_create_doc(PDF*)’:
    pdftotext.cpp:66: error: ‘struct PDF’ has no member named ‘doc’
    pdftotext.cpp:66: error: ‘poppler’ has not been declared
    pdftotext.cpp:67: error: ‘struct PDF’ has no member named ‘doc’
    pdftotext.cpp: In function ‘int PDF_unlock(PDF*, char*)’:
    pdftotext.cpp:75: error: ‘struct PDF’ has no member named ‘doc’
    pdftotext.cpp: In function ‘int PDF_init(PDF*, PyObject*, PyObject*)’:
    pdftotext.cpp:105: error: ‘struct PDF’ has no member named ‘doc’
    pdftotext.cpp: In function ‘PyObject* PDF_read_page(PDF*, int)’:
    pdftotext.cpp:119: error: ‘poppler’ has not been declared
    pdftotext.cpp:119: error: expected initializer before ‘*’ token
    pdftotext.cpp:120: error: ‘poppler’ has not been declared
    pdftotext.cpp:120: error: expected ‘;’ before ‘layout_mode’
    pdftotext.cpp:123: error: ‘page’ was not declared in this scope
    pdftotext.cpp:123: error: ‘struct PDF’ has no member named ‘doc’
    pdftotext.cpp:129: error: ‘poppler’ has not been declared
    pdftotext.cpp:129: error: expected initializer before ‘rect’
    pdftotext.cpp:130: error: ‘rect’ was not declared in this scope
    pdftotext.cpp:133: error: ‘layout_mode’ was not declared in this scope
    pdftotext.cpp:133: error: ‘poppler’ has not been declared
    pdftotext.cpp:135: error: ‘poppler’ has not been declared
    pdftotext.cpp:137: error: ‘poppler’ has not been declared
    pdftotext.cpp:138: error: type ‘<type error>’ argument given to ‘delete’, expected pointer
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/root/anaconda3/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-1mu2f1n2/pdftotext/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-ghuhvuhl/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-1mu2f1n2/pdftotext/

我假设这里的问题是它正在寻找 C++ 编译的文件,而我只能得到 glib?

我可以研究什么?

pdftotext 应该在 poppler-utils 中,所以试试 yum install poppler-utils

编辑: 嗯。在 EPEL 存储库中有一个名为 pypoppler 的软件包可用于 CentOS 6,它自己描述为 "Python bindings for the Poppler PDF rendering library." 我没有看到任何迹象表明它包含 poppler/cpp /{anything},但您可以尝试一下。 (您可能需要先安装 pycairo。)

否则,您可以尝试安装 pdftotextearlier version(例如 pip install pdftotext==1.0.0)以找到与 CentOS 6 兼容的版本。最早的版本来了不过,2017 年 6 月上映,所以这可能无济于事。

我想你对升级到 CentOS 7 不感兴趣?

我找到了解决方法。按照从 link 安装 libpoppler-cpp 的说明,我能够成功安装 pdftotext.

按照 this repo 的说明进行操作:

On CentOS

On CentOS the libpoppler-cpp library is not included with the system so we need to build from source. Note that recent versions of poppler require C++11 which is not available on CentOS, so we build a slightly older version of libpoppler.

# Build dependencies
yum install wget xz libjpeg-devel openjpeg2-devel

# Download and extract
wget https://poppler.freedesktop.org/poppler-0.47.0.tar.xz
tar -Jxvf poppler-0.47.0.tar.xz
cd poppler-0.47.0

# Build and install
./configure
make
sudo make install

By default libraries get installed in /usr/local/lib and /usr/local/include. On CentOS this is not a default search path so we need to set PKG_CONFIG_PATH and LD_LIBRARY_PATH to point R to the right directory:

export LD_LIBRARY_PATH="/usr/local/lib"
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig"