Ejemplo n.º 1
1
void RenderThread::run()
{
    double factor = qBound(0.01, m_zoomFactor, 10.0);
            
    if (!QFile::exists(m_pdfUrl)) return;

    Poppler::Document* document = Poppler::Document::load(m_pdfUrl);
    if (!document || document->isLocked()) {
        delete document;
        return;
    }

    // Access page of the PDF file
    document->setRenderHint(Poppler::Document::Antialiasing, true);
    document->setRenderHint(Poppler::Document::TextAntialiasing, true);
    Poppler::Page* pdfPage = document->page(0);  // Document starts at page 0
    if (pdfPage == 0) {
        return;
    }

    // Generate a QImage of the rendered page
    QImage image = pdfPage->renderToImage(factor*200.0, factor*200.0);
    emit previewReady(image);
    
    delete pdfPage;
    delete document;
}
Ejemplo n.º 2
0
WINPrint::~WINPrint() {
  if (!doit) return;
  Poppler::Document* document = Poppler::Document::load(file);
  if (document) {
    document->setRenderHint(Poppler::Document::Antialiasing);
    document->setRenderHint(Poppler::Document::TextAntialiasing);

    int nbpages=document->numPages(), nextpage=nbpages-1;
    QPainter Paint;
    if(Paint.begin(Prt)) {
      QImage image;
      QRect rect (0, 0,
                  Prt->paperRect (QPrinter::DevicePixel).width(),
                  Prt->paperRect(QPrinter::DevicePixel).height());
      double rres= Prt->resolution();
      Paint.setRenderHint (QPainter::Antialiasing);
      for (int pg=0;pg < nbpages;pg++) {
        Poppler::Page* pdfPage = document->page (pg);  
        if (pdfPage) {
          image= pdfPage->renderToImage (rres, rres);
          delete pdfPage;
        }
        if (!image.isNull()) {
          Paint.drawImage (rect, image);
          if (pg != nextpage) Prt->newPage();
        }
        else convert_error << "Fail to create image at "
                           << rres << " dpi resolution\n";
      }
      Paint.end();
    }
    delete (document);
  }
}
Ejemplo n.º 3
0
// Index any PDFs that are attached.  Basically it turns the PDF into text and adds it the same
// way as a note's body
void NoteIndexer::indexPdf(qint32 reslid) {

    NSqlQuery sql(global.db);
    if (reslid <= 0)
        return;

    QString file = global.fileManager.getDbaDirPath() + QString::number(reslid) +".pdf";

    QString text = "";
    Poppler::Document *doc = Poppler::Document::load(file);
    if (doc == NULL || doc->isEncrypted() || doc->isLocked())
        return;

    for (int i=0; i<doc->numPages(); i++) {
        QRectF rect;
        text = text + doc->page(i)->text(rect) + QString(" ");
    }

    // Add the new content.  it is basically a text version of the note with a weight of 100.
    sql.prepare("Insert into SearchIndex (lid, weight, source, content) values (:lid, :weight, :source, :content)");
    sql.bindValue(":lid", reslid);
    sql.bindValue(":weight", 100);
    sql.bindValue(":source", "recognition");
    sql.bindValue(":content", text);
    sql.exec();
}
Ejemplo n.º 4
0
// Index any PDFs that are attached.  Basically it turns the PDF into text and adds it the same
// way as a note's body
void IndexRunner::indexPdf(qint32 lid, Resource &r) {
    if (!keepRunning || pauseIndexing) {
        indexTimer->start();
        return;
    }
    ResourceTable rtable(&db->conn);
    qint32 reslid = rtable.getLid(r.guid);
    if (lid <= 0) {
        indexTimer->start();
        return;
    }
    QString file = global.fileManager.getDbaDirPath() + QString::number(reslid) +".pdf";

    QString text = "";
    Poppler::Document *doc = Poppler::Document::load(file);
    if (doc == NULL) {
        indexTimer->start();
        return;
    }
    for (int i=0; keepRunning && !pauseIndexing && i<doc->numPages(); i++) {
        QRectF rect;
        text = text + doc->page(i)->text(rect) + QString(" ");
    }
    IndexRecord *rec = new IndexRecord();
    rec->content = text;
    rec->source = "recognition";
    rec->weight = 100;
    rec->lid = lid;
    if (indexHash->contains(lid)) {
        delete indexHash->value(lid);
        indexHash->remove(lid);
    }
    indexHash->insert(lid, rec);
}
Ejemplo n.º 5
0
void TestLinks::checkDests_xr01()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load(TESTDATADIR "/unittestcases/xr01.pdf");
    QVERIFY( doc );

    Poppler::Page *page = doc->page(0);
    QVERIFY( page );

    QList< Poppler::Link* > links = page->links();
    QCOMPARE( links.count(), 2 );

    {
    QCOMPARE( links.at(0)->linkType(), Poppler::Link::Goto );
    Poppler::LinkGoto *link = static_cast< Poppler::LinkGoto * >( links.at(0) );
    const Poppler::LinkDestination dest = link->destination();
    QVERIFY( !isDestinationValid_pageNumber( &dest, doc ) );
    QVERIFY( isDestinationValid_name( &dest ) );
    QCOMPARE( dest.destinationName(), QString::fromLatin1("section.1") );
    }

    {
    QCOMPARE( links.at(1)->linkType(), Poppler::Link::Goto );
    Poppler::LinkGoto *link = static_cast< Poppler::LinkGoto * >( links.at(1) );
    const Poppler::LinkDestination dest = link->destination();
    QVERIFY( !isDestinationValid_pageNumber( &dest, doc ) );
    QVERIFY( isDestinationValid_name( &dest ) );
    QCOMPARE( dest.destinationName(), QString::fromLatin1("section.2") );
    }

    delete doc;
}
void NCSAFindBar::performSearch()
{
  
  if(wordSpottingUtil == NULL)
  {
    this->preprocessDocument();
  }
     
     QPixmap search_input(display->size());
     display->render(&search_input);     
     
     searchResult = wordSpottingUtil->search(search_input, 10);
     
     
     QString filepath =  getFilePath();
     Poppler::Document *pdf = Poppler::Document::load(filepath);  
     
     ////////displaying results
     resultComboBox->clear();
     for(int i = 0; i < searchResult.size(); i++)
     {
       NCSAWordInfo* wordInfo = searchResult[i];
       QImage returnedPage = pdf->page(wordInfo->pagenum)->renderToImage(150,150); //TODO: get rid of having to read the file again
       //QImage returnedPage = *(wordInfo->page);
       const QImage word = returnedPage.copy(wordInfo->box->x, wordInfo->box->y, wordInfo->box->w, wordInfo->box->h);

       resultComboBox->addItem(QPixmap::fromImage(word), "", -1);
       returnedPage.height();
       returnedPage.width();

     }
     QSize size(100,30);
     resultComboBox->setIconSize(size);
     
}
Ejemplo n.º 7
0
//TODO think about storing the documents instead of the filenames in a list
//Would avoid creation of the documents for each search but probably take up more memory
bool MainWindow::searchPDFPostResults(const QFileInfo &fi, QString searchString)
{
    bool nHitsExceeded = false;
    Poppler::Document *document = Poppler::Document::load(fi.absoluteFilePath());
    if (!document || document->isLocked()) {
      //Oops, document empty => error message
      QMessageBox mbox;
      mbox.setText(tr("Could not read the file \n'") + fi.fileName() +
                   tr("\ntherefore, possible matches might be missing"));
      mbox.exec();
      delete document;
      return false;
    }
    //Get document title
    QString title = document->info("Title");
    //If no title specified => use filename instead
    if(title == "")
        title = fi.fileName();
    QString shortTitle = QString(title);
    if (shortTitle.length() > 16)
    {
        shortTitle = "..." + shortTitle.right(16);
    }
    ui->statusBar->showMessage("Durchsuche PDF: " + shortTitle);
    qDebug() << "Searching pdf: " << title;
    //Search the document for the searchString
    int hitCounter = 0;
    for (int page = 0; page < document->numPages(); page++) {
        //Parameters 1,2,3,4 of the search function
        //will contain the rectangle coordinates of where the text was found.
        //Since we are not interested in these coordinates, we just pass a single variable
        double coordinate = 0.0;
        if (document->page(page)->search(searchString, coordinate,coordinate,coordinate,coordinate,
            Poppler::Page::NextResult,
            Poppler::Page::CaseInsensitive)) {
            hitCounter++;
            ui->statusBar->showMessage(tr("Searching in ") + shortTitle +
                                       " - hit on page: " + QString::number(page));
            //Yay, we found the search String on this page
            //=> Add this page to the result list
            addDocNPageToResultList(title, fi, page);
            // TODO make this limit a variable
            if(hitCounter >= maxHits)
            {
                QMessageBox mbox;
                mbox.setText(tr("Search has already reached the maximum of ") + QString::number(maxHits)
                             + tr(" hits please choose a more specific criterion"));
                mbox.exec();
                nHitsExceeded = true;
                break;
            }
        }
    }

    //Don't forget to delete the document
    delete document;
    //Return whether the max number of hits was reached
    return nHitsExceeded;
}
void guFolderInspector::extractIsbnsFromPdf(QString fileName, QList<QString> &ISBNList)
{
    //PDFDoc *doc;
    //doc = new PDFDoc()
    //qDebug() << "file:" << fileName;
    Poppler::Document* document;
    document = Poppler::Document::load(fileName,0,0);


    //Poppler::Document::
    if (!document || document->isLocked()) {

        // ... error message ....
        delete document;
        return;
    }
    // Paranoid safety check
    if (!document) {
        // ... error message ...
        return;
    }

    Poppler::Page* pdfPage;
    isbnMethods find;

    // Access page of the PDF file
    int numOfPages = document->numPages(); //количество страниц
    int numOfSearchPages = 15;
    //qDebug() << "num of pages " << numOfPages;
    if(numOfPages < numOfSearchPages)
    {
        numOfSearchPages = numOfPages;
    }
    for ( int pageNumber = 0 ; pageNumber < numOfSearchPages ; pageNumber++)
    {
        pdfPage = document->page(pageNumber);  // Document starts at page 0
        if (!pdfPage) {
            // ... error message ...
            continue;
        }

        //QtMsgHandler qInstallMsgHandler ( QtMsgHandler handler )
        QString pageContent;
        pageContent = pdfPage->text(QRectF(QPointF(0,0),pdfPage->pageSizeF()));

        find.findIsbns(pageContent, ISBNList);

        // ... use image ...
        //qDebug() << pageContent;
    }

    // after the usage, the page must be deleted
    delete pdfPage;

    //Finally, don't forget to destroy the document:

    delete document;
}
int main( int argc, char **argv )
{
    QApplication a( argc, argv );               // QApplication required!

    QTime t;
    t.start();

    QDir directory( argv[1] );
    foreach ( const QString &fileName, directory.entryList() ) {
        if (fileName.endsWith("pdf") ) {
	    qDebug() << "Doing" << fileName.toLatin1().data() << ":";
	    Poppler::Document *doc = Poppler::Document::load( directory.canonicalPath()+"/"+fileName );
	    if (!doc) {
		qWarning() << "doc not loaded";
	    } else if ( doc->isLocked() ) {
	        if (! doc->unlock( "", "password" ) ) {
		    qWarning() << "couldn't unlock document";
		    delete doc;
		}
	    } else {
		doc->pdfVersion();
		doc->info("Title");
		doc->info("Subject");
		doc->info("Author");
		doc->info("Keywords");
		doc->info("Creator");
		doc->info("Producer");
		doc->date("CreationDate").toString();
		doc->date("ModDate").toString();
		doc->numPages();
		doc->isLinearized();
		doc->isEncrypted();
		doc->okToPrint();
		doc->okToCopy();
		doc->okToChange();
		doc->okToAddNotes();
		doc->pageMode();

		for( int index = 0; index < doc->numPages(); ++index ) {
		    Poppler::Page *page = doc->page( index );
		    QImage image = page->renderToImage();
		    page->pageSize();
		    page->orientation();
		    delete page;
		    std::cout << ".";
		    std::cout.flush();
		}
		std::cout << std::endl;
		delete doc;
	    }
	}
    }

    std::cout << "Elapsed time: " << (t.elapsed()/1000) << "seconds" << std::endl;

}
void TestMetaData::checkSeascapeOrientation()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load("../../../test/unittestcases/orientation.pdf");
    QVERIFY( doc );

    Poppler::Page *page = doc->page(3);
    QCOMPARE( page->orientation(), Poppler::Page::Seascape );

    delete page;
    delete doc;
}
void TestMetaData::checkPageSize()
{
    Poppler::Document *doc;

    doc = Poppler::Document::load("../../../test/unittestcases/truetype.pdf");
    QVERIFY( doc );
    Poppler::Page *page = doc->page(0);
    QCOMPARE( page->pageSize(), QSize(595, 842) );
    QCOMPARE( page->pageSizeF(), QSizeF(595.22, 842) );

    delete page;
    delete doc;
}
Ejemplo n.º 12
0
int main( int argc, char **argv )
{
    QApplication a( argc, argv );               // QApplication required!

    if ( argc != 3)
    {
	qWarning() << "usage: test-password-qt4 owner-password filename";
	exit(1);
    }
  
    Poppler::Document *doc = Poppler::Document::load(argv[2], argv[1]);
    if (!doc)
    {
	qWarning() << "doc not loaded";
	exit(1);
    }
  
    // output some meta-data
    int major = 0, minor = 0;
    doc->getPdfVersion( &major, &minor );
    qDebug() << "    PDF Version: " << qPrintable(QString::fromLatin1("%1.%2").arg(major).arg(minor));
    qDebug() << "          Title: " << doc->info("Title");
    qDebug() << "        Subject: " << doc->info("Subject");
    qDebug() << "         Author: " << doc->info("Author");
    qDebug() << "      Key words: " << doc->info("Keywords");
    qDebug() << "        Creator: " << doc->info("Creator");
    qDebug() << "       Producer: " << doc->info("Producer");
    qDebug() << "   Date created: " << doc->date("CreationDate").toString();
    qDebug() << "  Date modified: " << doc->date("ModDate").toString();
    qDebug() << "Number of pages: " << doc->numPages();
    qDebug() << "     Linearised: " << doc->isLinearized();
    qDebug() << "      Encrypted: " << doc->isEncrypted();
    qDebug() << "    OK to print: " << doc->okToPrint();
    qDebug() << "     OK to copy: " << doc->okToCopy();
    qDebug() << "   OK to change: " << doc->okToChange();
    qDebug() << "OK to add notes: " << doc->okToAddNotes();
    qDebug() << "      Page mode: " << doc->pageMode();
    QStringList fontNameList;
    foreach( const Poppler::FontInfo &font, doc->fonts() )
	fontNameList += font.name();
    qDebug() << "          Fonts: " << fontNameList.join( ", " );

    Poppler::Page *page = doc->page(0);
    qDebug() << "    Page 1 size: " << page->pageSize().width()/72 << "inches x " << page->pageSize().height()/72 << "inches";

    PDFDisplay test( doc );        // create picture display
    test.setWindowTitle("Poppler-Qt4 Test");
    test.show();                            // show it

    return a.exec();                        // start event loop
}
Ejemplo n.º 13
0
void PDFDisplay::display()
{
    if (doc) {
	Poppler::Page *page = doc->page(m_currentPage);
	if (page) {
	    qDebug() << "Displaying page: " << m_currentPage;
	    image = page->renderToImage();
	    update();
	    delete page;
	}
    } else {
	qWarning() << "doc not loaded";
    }
}
Ejemplo n.º 14
0
void TestActualText::checkActualText1()
{
    Poppler::Document *doc;
    doc = Poppler::Document::load("../../../test/unittestcases/WithActualText.pdf");
    QVERIFY( doc );

    Poppler::Page *page = doc->page(0);
    QVERIFY( page );

    QCOMPARE( page->text(QRectF()), QString("The slow brown fox jumps over the black dog.") );

    delete page;

    delete doc;
}
Ejemplo n.º 15
0
void NCSAFindBar::preprocessDocument()
{
  
     wordSpottingUtil = new NCSAWordSpottingUtil();
     QString filepath =  getFilePath();
     Poppler::Document *pdf = Poppler::Document::load(filepath);  

     if(pdf == NULL)
     {
        QMessageBox::warning(0, QString("Error"), QString("Cannot locate file at path: ").append(filepath).append(" ."));
        return;
     }
     
     for(int i = 0; i < pdf->numPages(); i++)
     {
       wordSpottingUtil->addPage(pdf->page(i)->renderToImage(150, 150), i);
     }
}
Ejemplo n.º 16
0
int main(int argc, char **argv)
{
	if (argc != 2) {
		std::cout << "Usage: " << argv[0] << " PDF" << std::endl;
		return 1;
	}

	QString filename(argv[1]);
	Poppler::Document* document = Poppler::Document::load(filename);

	document->setRenderHint(Poppler::Document::TextHinting, true);
	document->setRenderHint(Poppler::Document::TextSlightHinting, true);

	Poppler::Page* pdfPage = document->page(0);

	// dpi must be high enough
	QImage image = pdfPage->renderToImage(200, 200);

	return 0;
}
void PDFDisplay::display()
{
    if (doc) {
        Poppler::Page *page = doc->page(m_currentPage);
        if (page) {
            qDebug() << "Displaying page using" << backendString << "backend: " << m_currentPage;
            image = page->renderToImage();
            qDeleteAll(textRects);
            if (showTextRects)
            {
                QPainter painter(&image);
                painter.setPen(Qt::red);
                textRects = page->textList();
                foreach(Poppler::TextBox *tb, textRects)
                {
                    painter.drawRect(tb->boundingBox());
                }
            }
            else textRects.clear();
            update();
            delete page;
        }
Ejemplo n.º 18
0
// Index any PDFs that are attached.  Basically it turns the PDF into text and adds it the same
// way as a note's body
void IndexRunner::indexPdf(qint32 lid, Resource &r) {
    ResourceTable rtable(&db->conn);
    qint32 reslid = rtable.getLid(r.guid);
    if (lid <= 0 || !keepRunning || pauseIndexing)
        return;
    QString file = global.fileManager.getDbaDirPath() + QString::number(reslid) +".pdf";

    QString text = "";
    Poppler::Document *doc = Poppler::Document::load(file);
    if (doc == NULL)
        return;
    for (int i=0; i<doc->numPages() && keepRunning && !pauseIndexing; i++) {
        QRectF rect;
        text = text + doc->page(i)->text(rect) + QString(" ");
    }
    QSqlQuery sql(db->conn);
    sql.prepare("Insert into SearchIndex (lid, weight, source, content) values (:lid, :weight, 'recognition', :content)");
    sql.bindValue(":lid", lid);
    sql.bindValue(":weight", 100);
    sql.bindValue(":content", text);
    sql.exec();

}
Ejemplo n.º 19
0
// Index any resources
void IndexRunner::indexRecognition(qint32 lid, Resource &r) {

    if (!keepRunning || pauseIndexing) {
        //indexTimer->start();
        return;
    }

    // Add filename or source url to search index
    if (r.attributes.isSet()) {
        NSqlQuery sql(db);
        ResourceAttributes a = r.attributes;
        if (a.fileName.isSet()) {
            sql.prepare("Insert into SearchIndex (lid, weight, source, content) values (:lid, :weight, :source, :content)");
            sql.bindValue(":lid", lid);
            sql.bindValue(":weight", 100);
            sql.bindValue(":source", "recognition");
            sql.bindValue(":content", QString(a.fileName));
            sql.exec();
        }
        if (a.sourceURL.isSet()) {
            sql.prepare("Insert into SearchIndex (lid, weight, source, content) values (:lid, :weight, :source, :content)");
            sql.bindValue(":lid", lid);
            sql.bindValue(":weight", 100);
            sql.bindValue(":source", "recognition");
            sql.bindValue(":content", QString(a.sourceURL));
            sql.exec();
        }
    }


    // Make sure we have something to look through.
    Data recognition;
    if (r.recognition.isSet())
        recognition = r.recognition;
    if (!recognition.body.isSet())
        return;

    QDomDocument doc;
    QString emsg;
    doc.setContent(recognition.body, &emsg);

    // look for text tags
    QDomNodeList anchors = doc.documentElement().elementsByTagName("t");
#if QT_VERSION < 0x050000
    for (unsigned int i=0; keepRunning && !pauseIndexing && i<anchors.length(); i++) {
#else
    for (int i=0; keepRunning && !pauseIndexing && i<anchors.length(); i++) {
#endif
        QApplication::processEvents();
        QDomElement enmedia = anchors.at(i).toElement();
        QString weight = enmedia.attribute("w");
        QString text = enmedia.text();
        if (text != "") {
            IndexRecord *rec = new IndexRecord();
            rec->weight = weight.toInt();
            rec->lid = lid;
            rec->content = text;
            rec->source = "recognition";
            if (indexHash->contains(lid)) {
                delete indexHash->value(lid);
                indexHash->remove(lid);
            }
            indexHash->insert(lid, rec);
        }
    }
}


// Index any PDFs that are attached.  Basically it turns the PDF into text and adds it the same
// way as a note's body
void IndexRunner::indexPdf(qint32 lid, Resource &r) {
    if (!global.indexPDFLocally)
        return;
    if (!keepRunning || pauseIndexing) {
        //indexTimer->start();
        return;
    }
    ResourceTable rtable(db);
    qint32 reslid = rtable.getLid(r.guid);
    if (lid <= 0) {
        //indexTimer->start();
        return;
    }
    QString file = global.fileManager.getDbaDirPath() + QString::number(reslid) +".pdf";

    QString text = "";
    Poppler::Document *doc = Poppler::Document::load(file);
    if (doc == nullptr || doc->isEncrypted() || doc->isLocked()) {
        //indexTimer->start();
        return;
    }
    for (int i=0; keepRunning && !pauseIndexing && i<doc->numPages(); i++) {
        QRectF rect;
        text = text + doc->page(i)->text(rect) + QString(" ");
    }
    IndexRecord *rec = new IndexRecord();
    rec->content = text;
    rec->source = "recognition";
    rec->weight = 100;
    rec->lid = lid;
    if (indexHash->contains(lid)) {
        delete indexHash->value(lid);
        indexHash->remove(lid);
    }
    indexHash->insert(lid, rec);
}




// Index any files that are attached.
void IndexRunner::indexAttachment(qint32 lid, Resource &r) {
    if (!officeFound)
        return;
    QLOG_DEBUG() << "indexing attachment to note " << lid;
    if (!keepRunning || pauseIndexing) {
        //indexTimer->start();
        return;
    }
    ResourceTable rtable(db);
    qint32 reslid = rtable.getLid(r.guid);
    if (lid <= 0) {
        //indexTimer->start();
        return;
    }
    QLOG_DEBUG() << "Resource " << reslid;
    QString extension = "";
    ResourceAttributes attributes;
    if (r.attributes.isSet())
        attributes = r.attributes;
    if (attributes.fileName.isSet()) {
        extension = attributes.fileName;
        int i = extension.indexOf(".");
	if (i != -1)
	  extension = extension.mid(i);
    }
    if (extension != ".doc"  && extension != ".xls"  && extension != ".ppt" &&
        extension != ".docx" && extension != ".xlsx" && extension != ".pptx" &&
        extension != ".pps"  && extension != ".pdf"  && extension != ".odt"  &&
        extension != ".odf"  && extension != ".ott"  && extension != ".odm"  &&
        extension != ".html" && extension != ".txt"  && extension != ".oth"  &&
        extension != ".ods"  && extension != ".ots"  && extension != ".odg"  &&
        extension != ".otg"  && extension != ".odp"  && extension != ".otp"  &&
        extension != ".odb"  && extension != ".oxt"  && extension != ".htm"  &&
        extension != ".docm")
                return;

    QString file = global.fileManager.getDbaDirPath() + QString::number(reslid) +extension;
    QFile dataFile(file);
    if (!dataFile.exists()) {
        QDir dir(global.fileManager.getDbaDirPath());
        QStringList filterList;
        filterList.append(QString::number(lid)+".*");
        QStringList list= dir.entryList(filterList, QDir::Files);
        if (list.size() > 0) {
            file = global.fileManager.getDbaDirPath()+list[0];
        }
    }

    QString outDir = global.fileManager.getTmpDirPath();

    QProcess sofficeProcess;
    QString cmd = "soffice --headless --convert-to txt:\"Text\" --outdir "
                    +outDir + " "
                    +file;

    sofficeProcess.start(cmd,
                         QIODevice::ReadWrite|QIODevice::Unbuffered);

    QLOG_DEBUG() << "Starting soffice ";
    sofficeProcess.waitForStarted();
    QLOG_DEBUG() << "Waiting for completion";
    sofficeProcess.waitForFinished();
    int rc = sofficeProcess.exitCode();
    QLOG_DEBUG() << "soffice Errors:" << sofficeProcess.readAllStandardError();
    QLOG_DEBUG() << "soffice Output:" << sofficeProcess.readAllStandardOutput();
    QLOG_DEBUG() << "return code:" << rc;
    if (rc == 255) {
        QLOG_ERROR() << "soffice not found.  Disabling attachment indexing.";
        this->officeFound = false;
        return;
    }
    QFile txtFile(outDir+QString::number(reslid) +".txt");
    if (txtFile.open(QIODevice::ReadOnly)) {
        QString text;
        text = txtFile.readAll();
        NSqlQuery sql(db);
        db->lockForWrite();
        sql.prepare("Insert into SearchIndex (lid, weight, source, content) values (:lid, :weight, 'recognition', :content)");
        sql.bindValue(":lid", lid);
        sql.bindValue(":weight", 100);

        text = global.normalizeTermForSearchAndIndex(text);
        sql.bindValue(":content", text);

        QLOG_DEBUG() << "Adding note resource to index DB";
        sql.exec();
        db->unlock();
        txtFile.close();
    }
    QDir dir;
    dir.remove(outDir+QString::number(reslid) +".txt");
}


void IndexRunner::flushCache() {
    if (indexHash->size() <= 0)
        return;
    QDateTime start = QDateTime::currentDateTimeUtc();
    NSqlQuery sql(db);
    db->lockForWrite();
    sql.exec("begin");
    QHash<qint32, IndexRecord*>::iterator i;

    // Start adding words to the index.  Every 200 sql insertions we do a commit
    int commitCount = 200;

    for (i=indexHash->begin(); keepRunning && !pauseIndexing && i!=indexHash->end(); ++i) {
        qint32 lid = i.key();
        IndexRecord *rec = i.value();
        qint32 weight = rec->weight;
        QString source = rec->source;
        QString content = rec->content;
        delete rec;

        // Delete any old content
        sql.prepare("Delete from SearchIndex where lid=:lid and source=:source");
        sql.bindValue(":lid", lid);
        sql.bindValue(":source", source);
        sql.exec();

        // Add the new content.  it is basically a text version of the note with a weight of 100.
        sql.prepare("Insert into SearchIndex (lid, weight, source, content) values (:lid, :weight, :source, :content)");
        sql.bindValue(":lid", lid);
        sql.bindValue(":weight", weight);
        sql.bindValue(":source", source);

        content = global.normalizeTermForSearchAndIndex(content);
        sql.bindValue(":content", content);

        sql.exec();
        commitCount--;
        if (commitCount <= 0) {
            sql.exec("commit");
            commitCount = 200;
        }
    }
    indexHash->clear();
    sql.exec("commit");

    sql.finish();
    db->unlock();
    QDateTime finish = QDateTime::currentDateTimeUtc();

    QLOG_DEBUG() << "Index Cache Flush Complete: " <<
                    finish.toMSecsSinceEpoch() - start.toMSecsSinceEpoch()
                    << " milliseconds.";
}



void IndexRunner::busy(bool value, bool finished) {
    iAmBusy=value;
    emit(this->indexDone(finished));
}
Ejemplo n.º 20
0
int main( int argc, char **argv )
{
    QApplication a( argc, argv );               // QApplication required!

    Q_UNUSED( argc );
    Q_UNUSED( argv );

    QTime t;
    t.start();
    QDir dbDir( QStringLiteral( "./pdfdb" ) );
    if ( !dbDir.exists() ) {
	qWarning() << "Database directory does not exist";
    }

    QStringList excludeSubDirs;
    excludeSubDirs << QStringLiteral("000048") << QStringLiteral("000607");

    const QStringList dirs = dbDir.entryList(QStringList() << QStringLiteral("0000*"), QDir::Dirs);
    foreach ( const QString &subdir, dirs ) {
	if ( excludeSubDirs.contains(subdir) ) {
	    // then skip it
	} else {
	    QString path = "./pdfdb/" + subdir + "/data.pdf";
	    std::cout <<"Doing " << path.toLatin1().data() << " :";
	    Poppler::Document *doc = Poppler::Document::load( path );
	    if (!doc) {
		qWarning() << "doc not loaded";
	    } else {
		int major = 0, minor = 0;
		doc->getPdfVersion( &major, &minor );
		doc->info(QStringLiteral("Title"));
		doc->info(QStringLiteral("Subject"));
		doc->info(QStringLiteral("Author"));
		doc->info(QStringLiteral("Keywords"));
		doc->info(QStringLiteral("Creator"));
		doc->info(QStringLiteral("Producer"));
		doc->date(QStringLiteral("CreationDate")).toString();
		doc->date(QStringLiteral("ModDate")).toString();
		doc->numPages();
		doc->isLinearized();
		doc->isEncrypted();
		doc->okToPrint();
		doc->okToCopy();
		doc->okToChange();
		doc->okToAddNotes();
		doc->pageMode();

		for( int index = 0; index < doc->numPages(); ++index ) {
		    Poppler::Page *page = doc->page( index );
		    page->renderToImage();
		    page->pageSize();
		    page->orientation();
		    delete page;
		    std::cout << ".";
		    std::cout.flush();
		}
		std::cout << std::endl;
		delete doc;
	    }
	}
    }

    std::cout << "Elapsed time: " << (t.elapsed()/1000) << std::endl;

}
Ejemplo n.º 21
0
int main( int argc, char **argv )
{
    QApplication a( argc, argv );               // QApplication required!

    if ( argc < 2 ||
        (argc == 3 && strcmp(argv[2], "-arthur") != 0) ||
        argc > 3)
    {
        // use argument as file name
        qWarning() << "usage: test-poppler-qt4 filename [-arthur]";
        exit(1);
    }
  
    Poppler::Document *doc = Poppler::Document::load(QFile::decodeName(argv[1]));
    if (!doc)
    {
        qWarning() << "doc not loaded";
        exit(1);
    }

    if (doc->isLocked())
    {
        qWarning() << "document locked (needs password)";
        exit(0);
    }
  
    if (doc->numPages() <= 0)
    {
        delete doc;
        qDebug() << "Doc has no pages";
        return 0;
    }

    QString backendString;
    if (argc == 3 && strcmp(argv[2], "-arthur") == 0)
    {
        backendString = "Arthur";
        doc->setRenderBackend(Poppler::Document::ArthurBackend);
    }
    else
    {
        backendString = "Splash";
        doc->setRenderBackend(Poppler::Document::SplashBackend);
    }
    doc->setRenderHint(Poppler::Document::Antialiasing, true);
    doc->setRenderHint(Poppler::Document::TextAntialiasing, true);
    
    for (int i = 0; i < doc->numPages(); ++i)
    {
        Poppler::Page *page = doc->page(i);
        if (page) {
            qDebug() << "Rendering page using" << backendString << "backend: " << i;
            QTime t = QTime::currentTime();
            QImage image = page->renderToImage();
            qDebug() << "Rendering took" << t.msecsTo(QTime::currentTime()) << "msecs";
            image.save(QString("test-rennder-to-file%1.ppm").arg(i));
            delete page;
        }
    }
    
    return 0;
}
Ejemplo n.º 22
0
Poppler::Page* PDFPageWidget::getNewThumbPopplerPage(){
    Poppler::Document *thumbDoc = Poppler::Document::load(oriFilePath);
    thumbDoc->setRenderHint(Poppler::Document::TextAntialiasing);
    return thumbDoc->page(pageNo);
}
Ejemplo n.º 23
0
// Modify the en-media tag into an attachment
void NoteFormatter::modifyApplicationTags(QWebElement &enmedia, QString &hash, QString appl) {
    if (appl.toLower() == "vnd.evernote.ink") {
            inkNote = true;
            readOnly = true;
            buildInkNote(enmedia, hash);
            return;
    }
    ResourceTable resTable(global.db);
    QString contextFileName;
    qint32 resLid = resTable.getLidByHashHex(note.guid, hash);
    Resource r;
    resTable.get(r, resLid, false);
    if (!r.data.isSet())
        resourceError = true;
    else {

        // If we are running the formatter and we are not generating a thumbnail
        QString mimetype = "";
        if (r.mime.isSet())
            mimetype = r.mime;
        if (mimetype == "application/pdf" && pdfPreview && !thumbnail) {
           modifyPdfTags(resLid, enmedia);
           return;
        }

        // If we are running the formatter so we can generate a thumbnail and it is a PDF
        if (mimetype == "application/pdf" && pdfPreview && thumbnail) {
            QString printImageFile = global.fileManager.getTmpDirPath() + QString::number(resLid) +QString("-print.jpg");
            QString file = global.fileManager.getDbaDirPath() + QString::number(resLid) +".pdf";
            Poppler::Document *doc;
            doc = Poppler::Document::load(file);
            if (doc == NULL)
                return;

            QImage *image = new QImage(doc->page(0)->renderToImage());
            image->save(printImageFile,"jpg");
            delete image;

            enmedia.setAttribute("src", printImageFile);
            enmedia.removeAttribute("hash");
            enmedia.removeAttribute("type");
            enmedia.setOuterXml(enmedia.toOuterXml().replace("<en-media","<img"));
            enmedia.setOuterXml(enmedia.toOuterXml().replace("</en-media>","</img>"));
            return;
        }
        QString fileDetails = "";
        MimeReference ref;
        ResourceAttributes attributes;
        if (r.attributes.isSet())
            attributes = r.attributes;
        if (attributes.fileName.isSet())
            fileDetails = ref.getExtensionFromMime(r.mime, fileDetails);

        enmedia.setAttribute("href", QString("nnres:") +global.fileManager.getDbaDirPath()+QString::number(resLid)
                             +fileDetails);
        contextFileName = global.fileManager.getTmpDirPath("")+QString::number(resLid) +global.attachmentNameDelimeter + fileDetails;

        // Setup the context menu.  This is useful if we want to do a "save as" or such
        contextFileName = contextFileName.replace("\\", "/");
        enmedia.setAttribute("onContextMenu", "window.browserWindow.resourceContextMenu('" +contextFileName +"');");
        enmedia.setAttribute("en-tag", "en-media");
        enmedia.setAttribute("lid", QString::number(resLid));

        enmedia.appendInside("<img/>");
        QWebElement newText = enmedia.lastChild();

        // Build an icon of the image
        QString fileExt;
        if (attributes.fileName.isSet())
            fileExt = attributes.fileName;
        else
            fileExt = appl;
        QString fn;
        QString mime;
        if (attributes.fileName.isSet())
            fn = attributes.fileName;
        if (r.mime.isSet())
            mime = r.mime;
        fileExt = ref.getExtensionFromMime(mime, fn);
        QString icon = findIcon(resLid, r, fileExt);
        newText.setAttribute("src", "file:///"+icon);
        if (attributes.fileName.isSet())
            newText.setAttribute("title",attributes.fileName);
        newText.setAttribute("en-tag", "temporary");
        //Rename the tag to a <a> link
        enmedia.setOuterXml(enmedia.toOuterXml().replace("<en-media","<a"));
        enmedia.setOuterXml(enmedia.toOuterXml().replace("</en-media>","</a>"));
    }
}