diff options
author | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2018-06-09 13:09:35 -0400 |
---|---|---|
committer | Ashod Nakashian <ashnakash@gmail.com> | 2019-08-25 13:31:53 +0200 |
commit | 9194f019afb0599d5e72476786fabfa996e07f20 (patch) | |
tree | e6c1e044e5f2eca5b52cb938d4fd9a13734096e8 | |
parent | e4cec56a699b75102c39f4f80879a8080fc5ecc1 (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>
-rw-r--r-- | include/vcl/pdfread.hxx | 12 | ||||
-rw-r--r-- | sd/source/filter/pdf/sdpdffilter.cxx | 36 | ||||
-rw-r--r-- | vcl/source/filter/ipdf/pdfread.cxx | 70 |
3 files changed, 95 insertions, 23 deletions
diff --git a/include/vcl/pdfread.hxx b/include/vcl/pdfread.hxx index 1de9cfc9b259..1e555d9841c7 100644 --- a/include/vcl/pdfread.hxx +++ b/include/vcl/pdfread.hxx @@ -11,6 +11,7 @@ #define INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX #include <vector> +#include <tools/gen.hxx> #include <tools/stream.hxx> namespace com @@ -45,7 +46,16 @@ VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Bitmap& rBitmap, size_t nPageInd VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic, double fResolutionDPI = 96.); VCL_DLLPUBLIC size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps, - css::uno::Sequence<sal_Int8>& rPdfData, double fResolutionDPI = 96.); + css::uno::Sequence<sal_Int8>& rPdfData, + const double fResolutionDPI = 96.); + +/// Import PDF as Graphic images (1 per page), all unloaded. +/// Since Graphic is unloaded, we need to return the page size (in pixels) separately. +/// Does not set rPdfData if no conversion is done. +/// Returns the number of pages read. +VCL_DLLPUBLIC size_t ImportPDFUnloaded(const OUString& rURL, + std::vector<std::pair<Graphic, Size>>& rGraphics, + const double fResolutionDPI = 96.); } #endif // INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX diff --git a/sd/source/filter/pdf/sdpdffilter.cxx b/sd/source/filter/pdf/sdpdffilter.cxx index 3fe38252b626..0cf7baa9dd6c 100644 --- a/sd/source/filter/pdf/sdpdffilter.cxx +++ b/sd/source/filter/pdf/sdpdffilter.cxx @@ -54,37 +54,29 @@ bool SdPdfFilter::Import() // Rendering resolution. const double dResolutionDPI = 96.; - uno::Sequence<sal_Int8> aPdfData; - std::vector<Bitmap> aBitmaps; - if (vcl::ImportPDF(aFileName, aBitmaps, aPdfData, dResolutionDPI) == 0) + std::vector<std::pair<Graphic, Size>> aGraphics; + if (vcl::ImportPDFUnloaded(aFileName, aGraphics, dResolutionDPI) == 0) return false; - // 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); - + // Add as many pages as we need up-front. mrDocument.CreateFirstPages(); - for (size_t i = 0; i < aBitmaps.size() - 1; ++i) + for (size_t i = 0; i < aGraphics.size() - 1; ++i) { mrDocument.DuplicatePage(0); } - size_t nPageNumber = 0; - for (const Bitmap& aBitmap : aBitmaps) + for (const std::pair<Graphic, Size>& aPair : aGraphics) { - // Create the Graphic and link the original PDF stream. - Graphic aGraphic(aBitmap); - aGraphic.setPdfData(pPdfData); - aGraphic.setPageNumber(nPageNumber); - aGraphic.SetGfxLink(pGfxLink); + const Graphic& rGraphic = aPair.first; + const Size& aSize = aPair.second; + + const sal_Int32 nPageNumber = rGraphic.getPageNumber(); + if (nPageNumber < 0 || static_cast<size_t>(nPageNumber) >= aGraphics.size()) + continue; // Page is out of range // Create the page and insert the Graphic. - SdPage* pPage = mrDocument.GetSdPage(nPageNumber++, PageKind::Standard); - Size aGrfSize(OutputDevice::LogicToLogic(aGraphic.GetPrefSize(), aGraphic.GetPrefMapMode(), + SdPage* pPage = mrDocument.GetSdPage(nPageNumber, PageKind::Standard); + Size aGrfSize(OutputDevice::LogicToLogic(aSize, rGraphic.GetPrefMapMode(), MapMode(MapUnit::Map100thMM))); // Resize to original size based on 72 dpi to preserve page size. @@ -95,7 +87,7 @@ bool SdPdfFilter::Import() pPage->SetSize(aGrfSize); Point aPos(0, 0); - pPage->InsertObject(new SdrGrafObj(pPage->getSdrModelFromSdrPage(), aGraphic, + pPage->InsertObject(new SdrGrafObj(pPage->getSdrModelFromSdrPage(), rGraphic, tools::Rectangle(aPos, aGrfSize))); } 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: */ |