summaryrefslogtreecommitdiff
path: root/vcl
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2018-06-09 13:09:35 -0400
committerAshod Nakashian <ashnakash@gmail.com>2019-08-25 13:31:53 +0200
commit9194f019afb0599d5e72476786fabfa996e07f20 (patch)
treee6c1e044e5f2eca5b52cb938d4fd9a13734096e8 /vcl
parente4cec56a699b75102c39f4f80879a8080fc5ecc1 (diff)
pdfium: Import PDF with unloaded images.
Reviewed-on: https://gerrit.libreoffice.org/56268 Reviewed-by: Ashod Nakashian <ashnakash@gmail.com> Tested-by: Ashod Nakashian <ashnakash@gmail.com> (cherry picked from commit e07561d2ae743b208a0807ef32d7f011614b73e5) Change-Id: I5e4a16ff38b9643127ce16879b35f456c13bcff8 Reviewed-on: https://gerrit.libreoffice.org/77688 Tested-by: Jenkins Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Diffstat (limited to 'vcl')
-rw-r--r--vcl/source/filter/ipdf/pdfread.cxx70
1 files changed, 70 insertions, 0 deletions
diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx
index 63162c6e2ad2..d9c197bffd77 100644
--- a/vcl/source/filter/ipdf/pdfread.cxx
+++ b/vcl/source/filter/ipdf/pdfread.cxx
@@ -17,6 +17,7 @@
#include <fpdf_save.h>
#endif
+#include <impgraph.hxx>
#include <vcl/graph.hxx>
#include <bitmapwriteaccess.hxx>
#include <unotools/ucbstreamhelper.hxx>
@@ -269,6 +270,75 @@ size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps,
return rBitmaps.size();
}
+
+size_t ImportPDFUnloaded(const OUString& rURL, std::vector<std::pair<Graphic, Size>>& rGraphics,
+ const double fResolutionDPI)
+{
+ std::unique_ptr<SvStream> xStream(
+ ::utl::UcbStreamHelper::CreateStream(rURL, StreamMode::READ | StreamMode::SHARE_DENYNONE));
+
+ // Save the original PDF stream for later use.
+ SvMemoryStream aMemoryStream;
+ if (!getCompatibleStream(*xStream, aMemoryStream, STREAM_SEEK_TO_BEGIN, STREAM_SEEK_TO_END))
+ return 0;
+
+ // Copy into PdfData
+ uno::Sequence<sal_Int8> aPdfData;
+ aMemoryStream.Seek(STREAM_SEEK_TO_END);
+ aPdfData = css::uno::Sequence<sal_Int8>(aMemoryStream.Tell());
+ aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+ aMemoryStream.ReadBytes(aPdfData.getArray(), aPdfData.getLength());
+
+ // Prepare the link with the PDF stream.
+ const size_t nGraphicContentSize = aPdfData.getLength();
+ std::unique_ptr<sal_uInt8[]> pGraphicContent(new sal_uInt8[nGraphicContentSize]);
+ memcpy(pGraphicContent.get(), aPdfData.get(), nGraphicContentSize);
+ std::shared_ptr<GfxLink> pGfxLink(std::make_shared<GfxLink>(
+ std::move(pGraphicContent), nGraphicContentSize, GfxLinkType::NativePdf));
+ auto pPdfData = std::make_shared<uno::Sequence<sal_Int8>>(aPdfData);
+
+ FPDF_LIBRARY_CONFIG aConfig;
+ aConfig.version = 2;
+ aConfig.m_pUserFontPaths = nullptr;
+ aConfig.m_pIsolate = nullptr;
+ aConfig.m_v8EmbedderSlot = 0;
+ FPDF_InitLibraryWithConfig(&aConfig);
+
+ // Load the buffer using pdfium.
+ FPDF_DOCUMENT pPdfDocument
+ = FPDF_LoadMemDocument(aPdfData.getArray(), aPdfData.getLength(), /*password=*/nullptr);
+ if (!pPdfDocument)
+ return 0;
+
+ const int nPageCount = FPDF_GetPageCount(pPdfDocument);
+ if (nPageCount <= 0)
+ return 0;
+
+ for (size_t nPageIndex = 0; nPageIndex < static_cast<size_t>(nPageCount); ++nPageIndex)
+ {
+ double fPageWidth = 0;
+ double fPageHeight = 0;
+ if (FPDF_GetPageSizeByIndex(pPdfDocument, nPageIndex, &fPageWidth, &fPageHeight) == 0)
+ continue;
+
+ // Returned unit is points, convert that to pixel.
+ const size_t nPageWidth = pointToPixel(fPageWidth, fResolutionDPI);
+ const size_t nPageHeight = pointToPixel(fPageHeight, fResolutionDPI);
+
+ // Create the Graphic and link the original PDF stream.
+ Graphic aGraphic;
+ aGraphic.setPdfData(pPdfData); // TODO: Skip if unchanged.
+ aGraphic.setPageNumber(nPageIndex);
+ aGraphic.SetGfxLink(pGfxLink);
+
+ rGraphics.emplace_back(std::move(aGraphic), Size(nPageWidth, nPageHeight));
+ }
+
+ FPDF_CloseDocument(pPdfDocument);
+ FPDF_DestroyLibrary();
+
+ return rGraphics.size();
+}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */