pdf2image的poppler-Linux支持安装教程

文章目录

使用目的

我想要解决的问题是以最快的速度抽取PDF中的图片,再和对应文本进行关联,最终适配到LangChain上

经过调研pdf2image的covert_from_byte的sthread_count 参数,可以启动多线程会大大加快转换速度

我的系统是openEuler,命令使用和CentOS一样

下载源码

https://poppler.freedesktop.org/

我选择的是 poppler-24.01.0.tar.xz

安装依赖

直接安装的依赖

bash 复制代码
sudo yum install nss-devel fontconfig-devel libfreetype6-dev libtiff-devel mesa-libGL-devel ninja-build systemd-devel pcre2 pcre2-devel glib2 glib2-devel autoconf automake libtool harfbuzz-devel lcms2-devel libcurl-devel poppler-cpp-devel

需要编译的依赖

1、libassuan
bash 复制代码
wget https://www.gnupg.org/ftp/gcrypt/libassuan/libassuan-2.5.5.tar.bz2
tar xjf libassuan-2.5.5.tar.bz2
cd libassuan-2.5.5
./configure --prefix=/usr/local
make -j4
sudo make install
2、gpgme
bash 复制代码
wget https://www.gnupg.org/ftp/gcrypt/gpgme/gpgme-1.19.0.tar.bz2
tar xjf gpgme-1.19.0.tar.bz2
cd gpgme-1.19.0
./configure --prefix=/usr/local
make -j4
sudo make install
3、libb2
bash 复制代码
git clone https://github.com/BLAKE2/libb2.git
cd libb2
autoreconf -fi
./configure
make
sudo find / -name libb2.pc 2>/dev/null
# /home/HwHiAiUser/work/pdf_to_sql/BLAKE2/libb2/libb2.pc
export PKG_CONFIG_PATH=/path/to/libb2:$PKG_CONFIG_PATH
4、pcre2
bash 复制代码
rm /home/anaconda3/cmake/pcre2-config.cmake
rm /home/anaconda3/cmake/pcre2-config-version.cmake # 删除虚拟环境中的,防止后面干扰
rpm -ql pcre2-devel # 查看pcre2的安装位置,方便后面设置路径,参考:
# -DPCRE2_INCLUDE_DIRS=/usr/include -DPCRE2_LIBRARY=/usr/lib64/libpcre2.so
5、Qt6

下载qt-everywhere-src-6.2.4.tar.xz

bash 复制代码
tar xf qt-everywhere-src-6.2.4.tar.xz
cd qt-everywhere-src-6.2.4
vim CMakeLists.txt
# 在第4行开始加入
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;/usr/lib64;/usr/lib;/home/anaconda3/lib")
# 保存后运行
./configure -DPCRE2_INCLUDE_DIRS=/usr/include -DPCRE2_LIBRARY=/usr/lib64/libpcre2.so -DPCRE2_FIND_COMPONENTS="8BIT;16BIT"
# 运行成功后显示
# -- Build files have been written to: /home/HwHiAiUser/work/pdf_to_sql/qt-everywhere-src-6.2.4
cmake --build . --parallel
make

命令行编译命令-poppler

我无法编译成功QT,五花八门的报错

所以只能关闭QT特性

bash 复制代码
cd poppler-24.01.0.tar.xz
vim CMakeCache.txt
ENABLE_QT5:BOOL=OFF 
ENABLE_QT6:BOOL=OFF

一共4个全部从NO改为OFF

bash 复制代码
mkdir build
cd build 
cmake .. 
make 
make install

最终成功安装

bash 复制代码
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH

添加一下路径

bash 复制代码
pdfinfo

查看版本输出

bash 复制代码
pdfinfo version 24.01.0
Copyright 2005-2024 The Poppler Developers - http://poppler.freedesktop.org
Copyright 1996-2011, 2022 Glyph & Cog, LLC
Usage: pdfinfo [options] <PDF-file>
  -f <int>             : first page to convert
  -l <int>             : last page to convert
  -box                 : print the page bounding boxes
  -meta                : print the document metadata (XML)
  -custom              : print both custom and standard metadata
  -js                  : print all JavaScript in the PDF
  -struct              : print the logical document structure (for tagged files)
  -struct-text         : print text contents along with document structure (for tagged files)
  -isodates            : print the dates in ISO-8601 format
  -rawdates            : print the undecoded date strings directly from the PDF file
  -dests               : print all named destinations in the PDF
  -url                 : print all URLs inside PDF objects (does not scan text content)
  -enc <string>        : output text encoding name
  -listenc             : list available encodings
  -opw <string>        : owner password (for encrypted files)
  -upw <string>        : user password (for encrypted files)
  -v                   : print copyright and version info
  -h                   : print usage information
  -help                : print usage information
  --help               : print usage information
  -?                   : print usage information

测试

python 复制代码
import pdfplumber
import PyPDF2
import pdf2image
from pdf2image import convert_from_path,convert_from_bytes
import tempfile
from pdf2image.exceptions import PDFInfoNotInstalledError, PDFPageCountError, PDFSyntaxError
import os
 
file_path = r'xxx.pdf' # PDF 文件路径
dir_path = r'output' # 存放图片的文件夹
 
def pdf2image2(file_path, dir_path):
    images = convert_from_path(file_path, dpi=300)
    for image in images:
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        image.save(dir_path + f'\img_{images.index(image)}.png', 'PNG')
 
pdf2image2(file_path, dir_path)

小结

也许直接安装poppler-cpp-devel就可以成功,但是我是最后才安装的

pdf2image就是需要libpoppler.so.133,我现在也不清楚/usr/local/lib64中的libpoppler.so.133是poppler-cpp-devel安装的,还是编译poppler安装的了

bash 复制代码
sudo find / -name libpoppler.so.133
/usr/local/lib64/libpoppler.so.133
/home/HwHiAiUser/work/pdf_to_sql/poppler-24.01.0/build/libpoppler.so.133
export LD_LIBRARY_PATH=/usr/local/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/home/HwHiAiUser/work/pdf_to_sql/poppler-24.01.0/build:$LD_LIBRARY_PATH

如果上述命令无效

bash 复制代码
vim /etc/ld.so.conf
# 添加:/usr/local/lib64
sudo ldconfig

编译QT6时出现了报错error: macro names must be identifiers

意义:宏名称必须是标识符,C++和C中命名变量时不能以数字开头,定义宏时也不能

在qt-everywhere-src-6.2.4/qtbase/src/3rdparty这个文件这里,就是数字开头,我尝试修改,无效

还有的报错是说磁盘空间不够,xxx包不支持等,依赖太多了

相关推荐
hold?fish:palm1 小时前
kv存储主从复制的设计与实现
c++·redis·后端
蓝创工坊Blue Foundry1 小时前
个人藏书太多怎么整理?用 OCR 字段提取汇总成电子书目
pdf·ocr·excel·文心一言·paddlepaddle·paddle
啦啦啦啦啦zzzz2 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法
charlie1145141912 小时前
现代C++工程实践 WeakPtr 实战(三):WeakPtrFactory 与「最后成员」惯用法
开发语言·c++·开源项目·现代c++
JetComXCpp4 小时前
给 Win32 窗口加上毛玻璃效果——纯 D3D11 实现,零依赖
c++
ysa0510305 小时前
【板子】二分答案(最大最小?)
c++·笔记·算法·板子
stolentime6 小时前
SP8549 MAIN75 - BST again题解
c++·算法·二叉树·深度优先·图论·记忆化搜索·组合数学
ziguo11226 小时前
C/C++ 错误处理全解:从 errno 到 C++ 异常
linux·c语言·c++·windows·visual studio
stolentime7 小时前
AT_pakencamp_2020_day1_k Gcd of Sum题解
c++·算法
2601_956121977 小时前
map_计蒜客T1271 完美K倍子数组
c++·算法