poppler qt5是poppler的擴展模塊,使用qt圖形庫的開發者對於開發pdf插件來講,這個開源庫無疑是很是方便的。本篇介紹如何使用這個庫,而且配置開發環境。c++
1.在poppler官網下載源碼,解壓到本地目錄後切換到這個根目錄,打開命令行:ide
$./configurespa
$make插件
$sudo make install命令行
2.編譯安裝完後,咱們用命令行查一下包,而且安裝對應的開發庫:code
$apt-cache search poppler-qt5
orm$sudo apt-get install libpoppler-qt5-dev
3.安裝完了咱們就能夠子qt中使用這個庫了,若是不肯定有沒這個包,能夠經過一下命令檢查一下:接口$pkg-config --cflags poppler-qt5圖片
4.在qt 的工程文件中添加一下配置
ci這時候就能使用poppler qt5的庫了,它提供了很方便的接口。寫一個例子獲取pdf文件的第一張截圖:
QPixmap FilePreviewIconProvider::getPlainTextPreviewIcon(QString fileUrl){ QFile file(fileUrl); if(!file.exists()) return QPixmap(QSize(PIXMAP_WIDTH,PIXMAP_HEIGHT)); if(!file.open(QIODevice::ReadOnly)) return QPixmap(QSize(PIXMAP_WIDTH,PIXMAP_HEIGHT)); file.waitForBytesWritten(200); QString text = file.readAll(); file.close(); QStringList strs = text.split("\n"); QImage img(QSize(PIXMAP_WIDTH,PIXMAP_HEIGHT),QImage::Format_ARGB32); QPainter painter(&img); QFont font; font.setPixelSize(12); painter.setFont(font); QPen pen; pen.setColor(QColor(0,0,0)); painter.setPen(pen); int counter = 0; for(auto line : strs){ QString str; QStringList subStrs; for(int i = 0;i<line.length();i++){ if(QFontMetrics(font).width(str)<(PIXMAP_WIDTH-15)) str += line[i]; else{ subStrs<<str; str =""; } } for(auto subline:subStrs){ painter.drawText(5,12+counter*15,subline); counter ++; } painter.drawText(5,12+counter*15,str); counter ++; } return QPixmap::fromImage(img); } QPixmap FilePreviewIconProvider::getPDFPreviewIcon(QString fileUrl){ Poppler::Document* document = Poppler::Document::load(fileUrl); if((!document || document->isLocked())){ delete document; qDebug()<<"file reading error...."; return QPixmap(QSize(PIXMAP_WIDTH,PIXMAP_HEIGHT)); } qDebug()<<"pdf document read successfully!"; if(document == 0){ qDebug()<<"pdf page is null"; return QPixmap(QSize(PIXMAP_WIDTH,PIXMAP_HEIGHT)); } Poppler::Page* pdfPage = document->page(0); //Document start at page 0 QImage img = pdfPage->renderToImage(72,72,0,0,pdfPage->pageSize().width(),pdfPage->pageSize().height()); if(img.isNull()){ qDebug()<<"render error"; return QPixmap(QSize(PIXMAP_WIDTH,PIXMAP_HEIGHT)); } img = img.scaled(QSize(PIXMAP_WIDTH,PIXMAP_HEIGHT),Qt::KeepAspectRatio,Qt::SmoothTransformation); delete pdfPage; return QPixmap::fromImage(img); }