Ubuntu下的项目需要显示pdf,虽然Qt有自己的pdf库,但是好像需要Ubuntu21.04才能不需要编译就能使用,但是对Qt的pdf库编译有没有信心,后来在网上查了资料,通过poppler可以完美方便的显示pdf,但是一样是开源的,需要自己手动编译,看网上编译方法挺简单,就想入坑试试,虽然尝试了很多方法,依旧安装失败,找不到北,最后成功解决,把安装步骤和方法写下来,供自己后期查看也希望帮助需要的人
安装环境:Ubuntu20.04
使用环境:Qt的qml
使用前提:就是先去poppler官网下载源码
我下载了最新版的 poppler-data-0.4.11.tar.gz
和适合Ubuntu20.04的 poppler-0.86.1.tar.xz
具体别的版本可以不?我也没有尝试
一、安装poppler-Data
提取(干净地解压缩):
tar -xf poppler-data-0.4.11.tar.gz
输入目录
cd poppler-data-0.4.11
然后神奇地将文件发送到正确的位置/usr/share
:
sudo make install
必须先安装第一步,然后第二步才可以进行
二.下面就是编译poppler
sudo apt-get install libopenjp2-7-dev libgdk-pixbuf2.0-dev cmake checkinstall #安装编译工具
sudo apt-get build-dep libpoppler-cpp-dev
解压
tar -xf poppler-0.86.1.tar.xz
cd poppler-0.86.1
mkdir build
cd build
cmake ..
sudo checkinstall make install
定义环境变量R_LD_LIBRARY_PATH以通知 R 有关 Poppler 库的信息:/usr/local/lib
echo "export R_LD_LIBRARY_PATH=\$R_LD_LIBRARY_PATH:/usr/local/lib" >> .bashrc
在 -shell 中编译 R 包:pdftools
R
install.packages("pdftools")
使用任何 pdf 文件从 -shell 测试它R
> pdftools::pdf_data(pdf="/usr/share/cups/data/default.pdf")
[1]]
[1] width height x y space text
<0 rows> (or 0-length row.names)
在Qml中使用
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Layouts 1.11
import org.docviewer.poppler 1.0 // <--- import
import QtQuick.Dialogs 1.3
Window {
id: win
visible: true
width: 640
height: 480
title: qsTr("Poppler Example")
function urlToPath(urlString) {
var s
if (urlString.startsWith("file:///")) {
var k = urlString.charAt(9) === ':' ? 8 : 7
s = urlString.substring(k)
} else {
s = urlString
}
return decodeURIComponent(s);
}
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
nameFilters: ["PDF files (*.pdf)", "All files (*)"]
onAccepted: timer.running = true
Component.onCompleted: visible = true
}
Timer {
id: timer
interval: 100; repeat: false
onTriggered: {
poppler.path = urlToPath(""+fileDialog.fileUrl)
view.focus = true
}
}
Poppler{
id: poppler
}
ListView{
id: view
height: parent.height
width: 100
model: poppler.numPages
delegate: Image{
id: image
width: parent.width
source: poppler.loaded? "image://poppler/page/" + (modelData+1): ""
sourceSize.width: width
MouseArea{
anchors.fill: parent
onClicked: {
image.ListView.view.currentIndex = index
image.ListView.view.focus = true
}
}
}
}
Flickable {
height: parent.height
anchors.left: view.right
anchors.right: parent.right
contentWidth: bigImage.width;
contentHeight: bigImage.height
boundsBehavior: Flickable.StopAtBounds
Image{
id: bigImage
sourceSize.width: win.width - view.width
source: (poppler.loaded && view.currentIndex >= 0)? "image://poppler/page/"+(view.currentIndex+1): ""
}
}
}
虽然编译成功,但是使用中还是提示找不到到这个组件,希望高手来指点,自己调试通过了再来更新