diff options
author | Miklos Vajna <vmiklos@collabora.co.uk> | 2017-02-09 13:47:33 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.co.uk> | 2017-02-10 07:06:51 +0000 |
commit | ea92b7cc2e4a948e101f4915d50c2469968e95b1 (patch) | |
tree | a9a2d05dca1552d289680412e8c2e91507389139 /xmlsecurity | |
parent | 3b536279ed8131373a2c123873e07f45c36a8684 (diff) |
xmlsecurity: use pdfium to generate a preview in pdfverify
This is just test code to check if the bundled pdfium is usable.
Change-Id: I1f1d808796fe77924518cd004d99affe70279e88
Reviewed-on: https://gerrit.libreoffice.org/34081
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
Diffstat (limited to 'xmlsecurity')
-rw-r--r-- | xmlsecurity/Executable_pdfverify.mk | 5 | ||||
-rw-r--r-- | xmlsecurity/workben/pdfverify.cxx | 89 |
2 files changed, 94 insertions, 0 deletions
diff --git a/xmlsecurity/Executable_pdfverify.mk b/xmlsecurity/Executable_pdfverify.mk index 446c68f4969e..9a67a785983a 100644 --- a/xmlsecurity/Executable_pdfverify.mk +++ b/xmlsecurity/Executable_pdfverify.mk @@ -11,6 +11,10 @@ $(eval $(call gb_Executable_Executable,pdfverify)) $(eval $(call gb_Executable_use_sdk_api,pdfverify)) +$(eval $(call gb_Executable_use_externals,pdfverify,\ + pdfium \ +)) + $(eval $(call gb_Executable_set_include,pdfverify,\ $$(INCLUDE) \ -I$(SRCDIR)/xmlsecurity/inc \ @@ -22,6 +26,7 @@ $(eval $(call gb_Executable_use_libraries,pdfverify,\ cppuhelper \ sal \ tl \ + vcl \ xmlsecurity \ )) diff --git a/xmlsecurity/workben/pdfverify.cxx b/xmlsecurity/workben/pdfverify.cxx index 92504fcba42d..dceb863ca77b 100644 --- a/xmlsecurity/workben/pdfverify.cxx +++ b/xmlsecurity/workben/pdfverify.cxx @@ -9,6 +9,15 @@ #include <iostream> +#ifdef WNT +#include <prewin.h> +#endif +#include <fpdfview.h> +#include <fpdf_edit.h> +#ifdef WNT +#include <postwin.h> +#endif + #include <com/sun/star/lang/XMultiServiceFactory.hpp> #include <com/sun/star/uno/XComponentContext.hpp> #include <com/sun/star/xml/crypto/SEInitializer.hpp> @@ -17,6 +26,10 @@ #include <cppuhelper/bootstrap.hxx> #include <osl/file.hxx> #include <sal/main.h> +#include <vcl/bitmap.hxx> +#include <vcl/bitmapaccess.hxx> +#include <vcl/pngwrite.hxx> +#include <vcl/svapp.hxx> #include <pdfio/pdfdocument.hxx> @@ -24,6 +37,73 @@ using namespace com::sun::star; namespace { +/// Convert to inch, then assume 96 DPI. +double pointToPixel(double fPoint) +{ + return fPoint / 72 * 96; +} + +/// Does PDF to PNG conversion using pdfium. +void generatePreview(const OString& rPdfPath, const OString& rPngPath) +{ + FPDF_LIBRARY_CONFIG aConfig; + aConfig.version = 2; + aConfig.m_pUserFontPaths = nullptr; + aConfig.m_pIsolate = nullptr; + aConfig.m_v8EmbedderSlot = 0; + FPDF_InitLibraryWithConfig(&aConfig); + + // Read input into a buffer. + OUString aInURL; + osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(rPdfPath), aInURL); + SvFileStream aInStream(aInURL, StreamMode::READ); + SvMemoryStream aInBuffer; + aInBuffer.WriteStream(aInStream); + + // Load the buffer using pdfium. + FPDF_DOCUMENT pPdfDocument = FPDF_LoadMemDocument(aInBuffer.GetData(), aInBuffer.GetSize(), /*password=*/nullptr); + if (!pPdfDocument) + return; + + // Render the first page. + FPDF_PAGE pPdfPage = FPDF_LoadPage(pPdfDocument, /*page_index=*/0); + if (!pPdfPage) + return; + + // Returned unit is points, convert that to pixel. + int nPageWidth = pointToPixel(FPDF_GetPageWidth(pPdfPage)); + int nPageHeight = pointToPixel(FPDF_GetPageHeight(pPdfPage)); + FPDF_BITMAP pPdfBitmap = FPDFBitmap_Create(nPageWidth, nPageHeight, /*alpha=*/1); + if (!pPdfBitmap) + return; + + FPDF_DWORD nColor = FPDFPage_HasTransparency(pPdfPage) ? 0x00000000 : 0xFFFFFFFF; + FPDFBitmap_FillRect(pPdfBitmap, 0, 0, nPageWidth, nPageHeight, nColor); + FPDF_RenderPageBitmap(pPdfBitmap, pPdfPage, /*start_x=*/0, /*start_y=*/0, nPageWidth, nPageHeight, /*rotate=*/0, /*flags=*/0); + + // Save the buffer as a PNG file. + Bitmap aBitmap(Size(nPageWidth, nPageHeight), 32); + { + Bitmap::ScopedWriteAccess pWriteAccess(aBitmap); + const void* pPdfBuffer = FPDFBitmap_GetBuffer(pPdfBitmap); + std::memcpy(pWriteAccess->GetBuffer(), pPdfBuffer, nPageWidth * nPageHeight * 4); + } + BitmapEx aBitmapEx(aBitmap); +#ifdef WNT + aBitmapEx.Mirror(BmpMirrorFlags::Vertical); +#endif + vcl::PNGWriter aWriter(aBitmapEx); + OUString aOutURL; + osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(rPngPath), aOutURL); + SvFileStream aOutStream(aOutURL, StreamMode::WRITE); + aWriter.Write(aOutStream); + + FPDFBitmap_Destroy(pPdfBitmap); + FPDF_ClosePage(pPdfPage); + FPDF_CloseDocument(pPdfDocument); + FPDF_DestroyLibrary(); +} + int pdfVerify(int nArgc, char** pArgv) { if (nArgc < 2) @@ -46,6 +126,15 @@ int pdfVerify(int nArgc, char** pArgv) uno::Reference<lang::XMultiComponentFactory> xMultiComponentFactory = xComponentContext->getServiceManager(); uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(xMultiComponentFactory, uno::UNO_QUERY); comphelper::setProcessServiceFactory(xMultiServiceFactory); + + if (nArgc > 3 && OString(pArgv[3]) == "-p") + { + InitVCL(); + generatePreview(pArgv[1], pArgv[2]); + DeInitVCL(); + return 0; + } + uno::Reference<xml::crypto::XSEInitializer> xSEInitializer; try { |