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包不支持等,依赖太多了

相关推荐
景彡先生6 分钟前
C++17 并行算法:std::execution::par
开发语言·c++
JiaJZhong17 分钟前
力扣.最长回文子串(c++)
java·c++·leetcode
oioihoii44 分钟前
C++随机打乱函数:简化源码与原理深度剖析
开发语言·c++·算法
WZF-Sang2 小时前
计算机网络基础——1
网络·c++·git·学习·计算机网络·智能路由器
默凉2 小时前
C++ 虚函数(多态,多重继承,菱形继承)
开发语言·c++
小庞在加油2 小时前
《重构项目》基于Apollo架构设计的项目重构方案(多种地图、多阶段、多任务、状态机管理)
c++·重构·apollo架构
让我们一起加油好吗3 小时前
【基础算法】贪心 (四) :区间问题
c++·算法·贪心算法·洛谷
双叶8364 小时前
(C++)任务管理系统(正式版)(迭代器)(list列表基础教程)(STL基础知识)
c语言·开发语言·数据结构·c++·list
七七七七074 小时前
类与对象【下篇】-- 关于类的其它语法
c语言·开发语言·c++
黄皮の电气鼠4 小时前
C++:继承
开发语言·c++·算法