7#include "config-kitinerary.h"
8#include "pdfdocument.h"
9#include "pdfdocument_p.h"
10#include "pdfextractoroutputdevice_p.h"
11#include "pdfimage_p.h"
12#include "popplerglobalparams_p.h"
13#include "popplerutils_p.h"
18#include <QScopedValueRollback>
23#include <PDFDocEncoding.h>
31void PdfPagePrivate::load()
37 PopplerGlobalParams gp;
38 PdfExtractorOutputDevice device;
39 m_doc->m_popplerDoc->displayPageSlice(&device, m_pageNum + 1, 72, 72, 0,
false,
true,
false, -1, -1, -1, -1);
40 m_doc->m_popplerDoc->processLinks(&device, m_pageNum + 1);
42 const auto pageRect = m_doc->m_popplerDoc->getPage(m_pageNum + 1)->getCropBox();
43#if KPOPPLER_VERSION < QT_VERSION_CHECK(25, 1, 0)
44 std::unique_ptr<GooString> s(device.getText(pageRect->x1, pageRect->y1, pageRect->x2, pageRect->y2));
47 const auto s = device.getText(pageRect->x1, pageRect->y1, pageRect->x2, pageRect->y2);
51 m_images = std::move(device.m_images);
52 for (
auto it = m_images.begin(); it != m_images.end(); ++it) {
53 (*it).d->m_page =
this;
56 m_links = std::move(device.m_links);
57 for (
auto &link : m_links) {
58 link.convertToPageRect(pageRect);
65 : d(new PdfPagePrivate)
69PdfPage::PdfPage(
const PdfPage&) =
default;
70PdfPage::~PdfPage() =
default;
79static double ratio(
double begin,
double end,
double ratio)
81 return begin + (end - begin) * ratio;
86 PopplerGlobalParams gp;
88 const auto page = d->m_doc->m_popplerDoc->getPage(d->m_pageNum + 1);
89 const auto pageRect = page->getCropBox();
95 switch (page->getRotate()) {
97 l = ratio(pageRect->x1, pageRect->x2, left);
98 t = ratio(pageRect->y1, pageRect->y2, top);
99 r = ratio(pageRect->x1, pageRect->x2, right);
100 b = ratio(pageRect->y1, pageRect->y2, bottom);
103 l = ratio(pageRect->y1, pageRect->y2, left);
104 t = ratio(pageRect->x1, pageRect->x2, top);
105 r = ratio(pageRect->y1, pageRect->y2, right);
106 b = ratio(pageRect->x1, pageRect->x2, bottom);
109 qCWarning(
Log) <<
"Unsupported page rotation!" << page->getRotate();
113 TextOutputDev device(
nullptr,
false, 0,
false,
false);
114 d->m_doc->m_popplerDoc->displayPageSlice(&device, d->m_pageNum + 1, 72, 72, 0,
false,
true,
false, -1, -1, -1, -1);
115#if KPOPPLER_VERSION <QT_VERSION_CHECK(25, 1, 0)
116 std::unique_ptr<GooString> s(device.getText(l, t, r, b));
119 const auto s = device.getText(l, t, r, b);
127 return d->m_images.size();
133 return d->m_images[index];
136QVariantList PdfPage::imagesVariant()
const
141 std::for_each(d->m_images.begin(), d->m_images.end(), [&l](
const PdfImage& img) { l.push_back(QVariant::fromValue(img)); });
149 PopplerGlobalParams gp;
150 const auto pageRect = d->m_doc->m_popplerDoc->getPage(d->m_pageNum + 1)->getCropBox();
152 for (
const auto &img : d->m_images) {
153 if ((img.d->m_transform.dx() >= ratio(pageRect->x1, pageRect->x2, left) && img.d->m_transform.dx() <= ratio(pageRect->x1, pageRect->x2, right)) &&
154 (img.d->m_transform.dy() >= ratio(pageRect->y1, pageRect->y2, top) && img.d->m_transform.dy() <= ratio(pageRect->y1, pageRect->y2, bottom)))
165 return d->m_links.size();
171 return d->m_links[index];
174QVariantList PdfPage::linksVariant()
const
178 l.reserve(d->m_links.size());
179 std::transform(d->m_links.begin(), d->m_links.end(), std::back_inserter(l), [](
const PdfLink &
link) { return QVariant::fromValue(link); });
189 for (
const auto &
link : d->m_links) {
196 std::sort(l.begin(), l.end(), [](
const auto &lhs,
const auto &rhs) {
197 const auto lhsLink = lhs.template value<PdfLink>();
198 const auto rhsLink = rhs.template value<PdfLink>();
199 if (lhsLink.area().top() == rhsLink.area().top()) {
200 return lhsLink.area().left() < rhsLink.area().left();
202 return lhsLink.area().top() < rhsLink.area().top();
208static constexpr inline double pdfToMM(
double points)
210 return points * 25.4 / 72.0;
213int PdfPage::width()
const
215 const auto page = d->m_doc->m_popplerDoc->getPage(d->m_pageNum + 1);
216 const auto rot = page->getRotate();
217 if (rot == 90 || rot == 270) {
218 return pdfToMM(page->getCropHeight());
220 return pdfToMM(page->getCropWidth());
223int PdfPage::height()
const
225 const auto page = d->m_doc->m_popplerDoc->getPage(d->m_pageNum + 1);
226 const auto rot = page->getRotate();
227 if (rot == 90 || rot == 270) {
228 return pdfToMM(page->getCropWidth());
230 return pdfToMM(page->getCropHeight());
234PdfDocument::PdfDocument(
QObject *parent)
236 , d(new PdfDocumentPrivate)
240PdfDocument::~PdfDocument() =
default;
245 std::for_each(d->m_pages.begin(), d->m_pages.end(), [&text](
const PdfPage &p) { text += p.text(); });
249int PdfDocument::pageCount()
const
251 return d->m_popplerDoc->getNumPages();
256 return d->m_pages[index];
261 return d->m_pdfData.size();
264static QDateTime parsePdfDateTime(
const GooString *str)
276 if (!parseDateString(str, &year, &month, &day, &hour, &min, &sec, &tz, &tzHours, &tzMins)) {
280 QDate date(year, month, day);
281 QTime time(hour, min, sec);
282 if (!date.isValid() || !time.isValid()) {
286 int offset = tzHours * 3600 + tzMins * 60;
289 }
else if (tz ==
'-') {
297 std::unique_ptr<GooString> dt(d->m_popplerDoc->getDocInfoCreatDate());
301 return parsePdfDateTime(dt.get());
306 std::unique_ptr<GooString> dt(d->m_popplerDoc->getDocInfoModDate());
310 return parsePdfDateTime(dt.get());
314QString gooStringToUnicode(
const std::unique_ptr<GooString> &s)
320#if KPOPPLER_VERSION >= QT_VERSION_CHECK(24, 5, 0)
321 if (hasUnicodeByteOrderMark(s->toStr()) || hasUnicodeByteOrderMarkLE(s->toStr())) {
323 if (s->hasUnicodeMarker() || s->hasUnicodeMarkerLE()) {
325 return QString::fromUtf16(
reinterpret_cast<const char16_t*
>(s->toStr().c_str()), s->toStr().size() / 2);
328 std::unique_ptr<const char[]> utf16Data(pdfDocEncodingToUTF16(s->toStr(), &len));
329 return QString::fromUtf16(
reinterpret_cast<const char16_t*
>(utf16Data.get()), len / 2);
337 return gooStringToUnicode(d->m_popplerDoc->getDocInfoTitle());
342 return gooStringToUnicode(d->m_popplerDoc->getDocInfoProducer());
347 return gooStringToUnicode(d->m_popplerDoc->getDocInfoCreator());
352 return gooStringToUnicode(d->m_popplerDoc->getDocInfoAuthor());
355QVariantList PdfDocument::pagesVariant()
const
358 l.reserve(pageCount());
359 std::for_each(d->m_pages.begin(), d->m_pages.end(), [&l](
const PdfPage& p) { l.push_back(QVariant::fromValue(p)); });
365 PopplerGlobalParams gp;
368 doc->d->m_pdfData = data;
370 auto stream =
new MemStream(
const_cast<char*
>(doc->d->m_pdfData.constData()), 0, doc->d->m_pdfData.size(), Object());
371 std::unique_ptr<PDFDoc> popplerDoc(
new PDFDoc(stream));
372 if (!popplerDoc->isOk()) {
373 qCWarning(
Log) <<
"Got invalid PDF document!" << popplerDoc->getErrorCode();
377 doc->d->m_pages.reserve(popplerDoc->getNumPages());
378 for (
int i = 0; i < popplerDoc->getNumPages(); ++i) {
380 page.d->m_pageNum = i;
381 page.d->m_doc = doc->d.get();
382 doc->d->m_pages.push_back(
page);
385 doc->d->m_popplerDoc = std::move(popplerDoc);
386 return doc.release();
394#include "moc_pdfdocument.cpp"
PDF document for extraction.
PdfPage page(int index) const
The n-thj page in this document.
int fileSize() const
File size of the entire document in bytes.
static PdfDocument * fromData(const QByteArray &data, QObject *parent=nullptr)
Creates a PdfDocument from the given raw data.
static bool maybePdf(const QByteArray &data)
Fast check whether data might be a PDF document.
An image in a PDF document.
An external link in a PDF file.
A page in a PDF document.
Q_INVOKABLE QVariantList linksInRect(double left, double top, double right, double bottom) const
Returns all links in the specified sub-rect of this page.
PdfImage image(int index) const
The n-th image found in this document.
int linkCount() const
The number of links found in this document.
Q_INVOKABLE QString textInRect(double left, double top, double right, double bottom) const
Returns the text in the specified sub-rect of this page.
PdfLink link(int index) const
The n-th link found in this document.
int imageCount() const
The number of images found in this document.
Q_INVOKABLE QVariantList imagesInRect(double left, double top, double right, double bottom) const
Returns the images in the specified sub-rect of this page.
KIOCORE_EXPORT CopyJob * link(const QList< QUrl > &src, const QUrl &destDir, JobFlags flags=DefaultFlags)
Classes for reservation/travel data models, data extraction and data augmentation.
bool startsWith(QByteArrayView bv) const const
QObject * parent() const const
bool intersects(const QRectF &rectangle) const const
QString fromUtf16(const char16_t *unicode, qsizetype size)
QString fromUtf8(QByteArrayView str)
QTimeZone fromSecondsAheadOfUtc(int offset)
QVariant fromValue(T &&value)