#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;
}