desktop/qa/data/3page.odg                   |binary
 desktop/qa/desktop_lib/test_desktop_lib.cxx |   29 ++++++++++++++++++++++++++++
 desktop/source/lib/init.cxx                 |    7 +++++-
 3 files changed, 35 insertions(+), 1 deletion(-)

New commits:
commit d0451dcf96508bf4d75c8147168f974ad3ebf03f
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Wed Jan 26 08:38:59 2022 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Jan 26 09:31:21 2022 +0100

    desktop lok, export options: allow passing through a JSON string as-is
    
    setFormatSpecificFilterData() sets useful defaults, but the PDF export
    code has the (sane) behavior of preferring FilterData over
    FilterOptions, so in case we explicitly got a JSON string in
    FilterOptions to in fact set FilterData, then leave FilterData empty.
    
    Change-Id: I20e8094bf431782fe0f5d68e3ec630e1274e30c7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128970
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins

diff --git a/desktop/qa/data/3page.odg b/desktop/qa/data/3page.odg
new file mode 100644
index 000000000000..1fad913e0493
Binary files /dev/null and b/desktop/qa/data/3page.odg differ
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx 
b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index 689a406e4a07..76eccc8f4a6d 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -62,6 +62,7 @@
 #include <cppunit/TestAssert.h>
 #include <vcl/BitmapTools.hxx>
 #include <vcl/pngwrite.hxx>
+#include <vcl/filter/PDFiumLibrary.hxx>
 
 #if USE_TLS_NSS
 #include <nss.h>
@@ -159,6 +160,7 @@ public:
     void testSearchAllNotificationsCalc();
     void testPaintTile();
     void testSaveAs();
+    void testSaveAsJsonOptions();
     void testSaveAsCalc();
     void testPasteWriter();
     void testPasteWriterJPEG();
@@ -225,6 +227,7 @@ public:
     CPPUNIT_TEST(testSearchAllNotificationsCalc);
     CPPUNIT_TEST(testPaintTile);
     CPPUNIT_TEST(testSaveAs);
+    CPPUNIT_TEST(testSaveAsJsonOptions);
     CPPUNIT_TEST(testSaveAsCalc);
     CPPUNIT_TEST(testPasteWriter);
     CPPUNIT_TEST(testPasteWriterJPEG);
@@ -675,6 +678,32 @@ void DesktopLOKTest::testSaveAs()
     CPPUNIT_ASSERT(pDocument->pClass->saveAs(pDocument, 
aTempFile.GetURL().toUtf8().getStr(), "png", nullptr));
 }
 
+void DesktopLOKTest::testSaveAsJsonOptions()
+{
+    // Given a document with 3 pages:
+    LibLODocument_Impl* pDocument = loadDoc("3page.odg");
+
+    // When exporting that document to PDF, skipping the first page:
+    utl::TempFile aTempFile;
+    aTempFile.EnableKillingFile();
+    OString aOptions("{\"PageRange\":{\"type\":\"string\",\"value\":\"2-\"}}");
+    CPPUNIT_ASSERT(pDocument->pClass->saveAs(pDocument, 
aTempFile.GetURL().toUtf8().getStr(), "pdf", aOptions.getStr()));
+
+    // Then make sure the resulting PDF has 2 pages:
+    SvFileStream aFile(aTempFile.GetURL(), StreamMode::READ);
+    SvMemoryStream aMemory;
+    aMemory.WriteStream(aFile);
+    std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
+    std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument
+        = pPDFium->openDocument(aMemory.GetData(), aMemory.GetSize());
+    CPPUNIT_ASSERT(pPdfDocument);
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: 2
+    // - Actual  : 3
+    // i.e. FilterOptions was ignored.
+    CPPUNIT_ASSERT_EQUAL(2, pPdfDocument->getPageCount());
+}
+
 void DesktopLOKTest::testSaveAsCalc()
 {
     LibLODocument_Impl* pDocument = loadDoc("search.ods");
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index dfbf503b269f..f9b1d3e0ea96 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -2897,7 +2897,12 @@ static int doc_saveAs(LibreOfficeKitDocument* pThis, 
const char* sUrl, const cha
 
         comphelper::SequenceAsHashMap aFilterDataMap;
 
-        setFormatSpecificFilterData(sFormat, aFilterDataMap);
+        // If filter options is JSON string, then make sure aFilterDataMap 
stays empty, otherwise we
+        // would ignore the filter options.
+        if (!aFilterOptions.startsWith("{"))
+        {
+            setFormatSpecificFilterData(sFormat, aFilterDataMap);
+        }
 
         if (!watermarkText.isEmpty())
             aFilterDataMap["TiledWatermark"] <<= watermarkText;

Reply via email to