sdext/source/pdfimport/wrapper/wrapper.cxx         |   87 ++++++++++++++-------
 sdext/source/pdfimport/xpdfwrapper/wrapper_gpl.cxx |   16 +++
 2 files changed, 75 insertions(+), 28 deletions(-)

New commits:
commit a77caf540cf4a0b97974da02362153021996d9e4
Author:     Dr. David Alan Gilbert <d...@treblig.org>
AuthorDate: Thu Feb 13 17:01:47 2025 +0000
Commit:     David Gilbert <d...@treblig.org>
CommitDate: Tue Mar 4 00:55:42 2025 +0100

    tdf#55425 sdext,pdfimport: Add a status string for opening the PDF
    
    Make the poppler process start with an explicit status line saying
    if it managed to open the PDF or if it hit an encryption problem.
    
    The current lines are:
      #OPEN
         It opened OK
      #ERROR:1:
         Or other number, meaning an error (see poppler for error codes)
      #ERROR:2:ENCRYTPTED
         We failed to open the document because it was encrypted.
         Don't rely on the number, look for the 'ENCRYPTED' string.
    
    Change-Id: I57ca014233ad0ca4b478108033055bc991465011
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/181740
    Tested-by: Jenkins
    Reviewed-by: David Gilbert <freedesk...@treblig.org>

diff --git a/sdext/source/pdfimport/wrapper/wrapper.cxx 
b/sdext/source/pdfimport/wrapper/wrapper.cxx
index 0c8175df235a..e32af020c729 100644
--- a/sdext/source/pdfimport/wrapper/wrapper.cxx
+++ b/sdext/source/pdfimport/wrapper/wrapper.cxx
@@ -1146,6 +1146,8 @@ bool xpdf_ImportFromFile(const OUString& rURL,
     bool bRet=true;
     try
     {
+        std::unique_ptr<Buffering> pBuffering;
+
         if( eErr!=osl_Process_E_None )
         {
             SAL_WARN(
@@ -1166,17 +1168,38 @@ bool xpdf_ImportFromFile(const OUString& rURL,
             osl_writeFile( pIn, aBuf.getStr(), sal_uInt64(aBuf.getLength()), 
&nWritten );
         }
 
-        if( pOut && pErr )
+        if (pOut)
+        {
+            // Check for a header saying if the child managed to open the 
document
+            OStringBuffer aHeaderLine;
+            pBuffering = std::unique_ptr<Buffering>(new Buffering(pOut));
+            oslFileError eFileErr = pBuffering->readLine(aHeaderLine);
+            if (osl_File_E_None == eFileErr)
+            {
+                SAL_INFO("sdext.pdfimport", "Header line:" << 
aHeaderLine.toString());
+            }
+            else
+            {
+                SAL_WARN("sdext.pdfimport", "Unable to read header line; " << 
eFileErr);
+                bRet = false;
+            }
+        }
+        else
+        {
+            SAL_WARN("sdext.pdfimport", "No output file");
+            bRet = false;
+        }
+
+        if (bRet && pOut && pErr)
         {
             // read results of PDF parser. One line - one call to
             // OutputDev. stderr is used for alternate streams, like
             // embedded fonts and bitmaps
             Parser aParser(rSink,pErr,xContext);
-            Buffering aBuffering(pOut);
             OStringBuffer line;
             for( ;; )
             {
-                oslFileError nRes = aBuffering.readLine(line);
+                oslFileError nRes = pBuffering->readLine(line);
 
                 if ( osl_File_E_None != nRes )
                     break;
diff --git a/sdext/source/pdfimport/xpdfwrapper/wrapper_gpl.cxx 
b/sdext/source/pdfimport/xpdfwrapper/wrapper_gpl.cxx
index 1cb85e244fbe..35cf370e07fa 100644
--- a/sdext/source/pdfimport/xpdfwrapper/wrapper_gpl.cxx
+++ b/sdext/source/pdfimport/xpdfwrapper/wrapper_gpl.cxx
@@ -191,8 +191,20 @@ int main(int argc, char **argv)
                  pUserPasswordStr );
 #endif
 
-    if (!aDoc.isOk())
-        return aDoc.getErrorCode();
+    if (aDoc.isOk())
+    {
+        printf("#OPEN
");
+        fflush(stdout);
+    }
+    else
+    {
+        int err = aDoc.getErrorCode();
+        // e.g. #ERROR:1:   (code for OpenFile)
+        //      #ERROR:2:ENCRYPTED   For ones we need to detect, use text
+        printf( "#ERROR:%d:%s
", err, err==errEncrypted ? "ENCRYPTED" : "");
+        fflush(stdout);
+        return err;
+    }
 
     pdfi::PDFOutDev aOutDev(&aDoc);
     if (options == TO_STRING_VIEW("SkipImages")) {
commit 99d7822b3932a9c74b8fc33bbbce504c33f3ee7e
Author:     Dr. David Alan Gilbert <d...@treblig.org>
AuthorDate: Thu Feb 13 16:23:18 2025 +0000
Commit:     David Gilbert <d...@treblig.org>
CommitDate: Tue Mar 4 00:55:28 2025 +0100

    tdf#55425 sdext,pdfimport: wrapper: Split out line read
    
    Split out the line read into a seperate method since we'll
    need it in a later patch,
    Note, it's reading on a pipe.
    
    Change-Id: Ie65246b01b4a898f8b9fe3ee6077e5ac05b52742
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/181739
    Reviewed-by: David Gilbert <freedesk...@treblig.org>
    Tested-by: Jenkins

diff --git a/sdext/source/pdfimport/wrapper/wrapper.cxx 
b/sdext/source/pdfimport/wrapper/wrapper.cxx
index 547ab148f954..0c8175df235a 100644
--- a/sdext/source/pdfimport/wrapper/wrapper.cxx
+++ b/sdext/source/pdfimport/wrapper/wrapper.cxx
@@ -1045,6 +1045,41 @@ public:
         *pBytesRead = nBytesRead;
         return osl_File_E_None;
     }
+
+    // Read a line and return any error
+    // Note: It skips leading 
 and 
+    //       It clears the line buffer at the start
+    oslFileError readLine(OStringBuffer& line)
+    {
+        char aChar('
');
+        sal_uInt64 nBytesRead;
+        oslFileError nRes;
+
+        line.setLength(0);
+
+        // skip garbage   
 at start of line
+        for (;;)
+        {
+            nRes = read(&aChar, 1, &nBytesRead);
+            if (osl_File_E_None != nRes || nBytesRead != 1 || (aChar != '
' && aChar != ' '))
+                break;
+        }
+        if (osl_File_E_None != nRes)
+            return nRes;
+
+        if (aChar != '
' && aChar != ' ')
+            line.append(aChar);
+
+        for (;;)
+        {
+            nRes = read(&aChar, 1, &nBytesRead);
+            if (osl_File_E_None != nRes || nBytesRead != 1 || aChar == '
' || aChar == ' ')
+                break;
+            line.append(aChar);
+        }
+
+        return nRes;
+    }
 };
 
 }
@@ -1141,37 +1176,14 @@ bool xpdf_ImportFromFile(const OUString& rURL,
             OStringBuffer line;
             for( ;; )
             {
-                char aChar('
');
-                sal_uInt64 nBytesRead;
-                oslFileError nRes;
+                oslFileError nRes = aBuffering.readLine(line);
 
-                // skip garbage   
 at start of line
-                for (;;)
-                {
-                    nRes = aBuffering.read(&aChar, 1, &nBytesRead);
-                    if (osl_File_E_None != nRes || nBytesRead != 1 || (aChar 
!= '
' && aChar != ' ') )
-                        break;
-                }
-                if ( osl_File_E_None != nRes )
-                    break;
-
-                if( aChar != '
' && aChar != ' ' )
-                    line.append( aChar );
-
-                for (;;)
-                {
-                    nRes = aBuffering.read(&aChar, 1, &nBytesRead);
-                    if ( osl_File_E_None != nRes || nBytesRead != 1 || aChar 
== '
' || aChar == ' ' )
-                        break;
-                    line.append( aChar );
-                }
                 if ( osl_File_E_None != nRes )
                     break;
                 if ( line.isEmpty() )
                     break;
 
                 aParser.parseLine(line);
-                line.setLength(0);
             }
         }
     }

Reply via email to