Страница 1 из 1

Парсинг pdf в текст

Добавлено: 10 сен 2024, 11:35
ya
Парсинг pdf в текст. Результат выводит на экран

Установить библиотеку

Код: Выделить всё

sudo apt-get install libpoppler-cpp-dev
pdf.cpp

Код: Выделить всё

#include <poppler-document.h>
#include <poppler-page.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>

int main(int argc, char** argv) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <pdf-file>" << std::endl;
        return 1;
    }

    std::string pdf_file = argv[1];
    // Open the PDF document
    auto doc = poppler::document::load_from_file(pdf_file);
    if (!doc) {
        std::cerr << "Error: unable to open PDF file." << std::endl;
        return 1;
    }

    // Extract text from each page
    int total_pages = doc->pages();
    for (int i = 0; i < total_pages; ++i) {
        auto p = doc->create_page(i);
        if (p) {
            // Convert poppler::ustring to std::string correctly
            poppler::ustring ustring_text = p->text();
            std::vector<char> vec = ustring_text.to_utf8(); // This should work correctly now
            std::string text(vec.begin(), vec.end()); // Corrected from std::string str to std::string text
            std::cout << "Page " << (i + 1) << ":\n" << text << std::endl;
        }
    }

    // No need to explicitly delete doc or p as they are managed automatically
    return 0;
}

Компиляция

Код: Выделить всё

#!/bin/bash

g++ -std=c++11 -o pdf pdf.cpp $(pkg-config --cflags --libs poppler-cpp)
Запуск программы:

Код: Выделить всё

./pdf имя_файла.pdf

Re: Парсинг pdf в текст

Добавлено: 10 сен 2024, 12:10
ya
Заменен вывод на экран записью в файл

Код: Выделить всё

#include <poppler-document.h>
#include <poppler-page.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <fstream> // Для работы с файлами

int main(int argc, char** argv) {
    if (argc != 3) {
        std::cerr << "Usage: " << argv[0] << " <pdf-file> <output-file>" << std::endl;
        return 1;
    }

    std::string pdf_file = argv[1];
    std::string output_file = argv[2];
    
    // Открываем PDF-документ
    auto doc = poppler::document::load_from_file(pdf_file);
    if (!doc) {
        std::cerr << "Error: unable to open PDF file." << std::endl;
        return 1;
    }

    // Открываем файл для записи
    std::ofstream ofs(output_file);
    if (!ofs.is_open()) {
        std::cerr << "Error: unable to open output file." << std::endl;
        return 1;
    }

    // Извлекаем текст с каждой страницы
    int total_pages = doc->pages();
    for (int i = 0; i < total_pages; ++i) {
        auto p = doc->create_page(i);
        if (p) {
            // Преобразуем poppler::ustring в std::string
            poppler::ustring ustring_text = p->text();
            std::vector<char> vec = ustring_text.to_utf8();
            std::string text(vec.begin(), vec.end());
            
            // Записываем текст в файл
            ofs << "Page " << (i + 1) << ":\n" << text << "\n";
        }
    }

    // Закрываем файл (автоматически произойдет при уничтожении ofs)
    return 0;
}
Компиляция

Код: Выделить всё

#!/bin/bash

g++ -std=c++11 -o pdf pdf.cpp $(pkg-config --cflags --libs poppler-cpp)
Запуск программы с двумя аргументами: входной файл pdf и выходной файл txt

Код: Выделить всё

/bin/bash pdf имя_файла.pdf имя_выходного_файла.txt