aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2017-03-12 15:51:47 -0700
committerSean Whitton <spwhitton@spwhitton.name>2017-03-12 15:51:47 -0700
commit68e5a1faed2fc49b5a2ab433ea8cca754fff957d (patch)
treea3f4379edd1197965728db6f6883472cf46853ca
parenta4fbdb7b1d60b6536822f8a1895c917a1ccf4d5c (diff)
downloadsscan-68e5a1faed2fc49b5a2ab433ea8cca754fff957d.tar.gz
rework calling OCRmyPDF
1. Catch stderr and don't log it unless OCRmyPDF exits non-zero. 2. Work around OCRmyPDF dying when stdout is not connected.
-rw-r--r--Main.hs22
1 files changed, 16 insertions, 6 deletions
diff --git a/Main.hs b/Main.hs
index d708a10..67367e2 100644
--- a/Main.hs
+++ b/Main.hs
@@ -24,7 +24,7 @@ along with sscan. If not, see <http://www.gnu.org/licenses/>.
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent (forkIO)
-import Control.Monad (void, when)
+import Control.Monad (unless, void, when)
import Data.Time.Clock.POSIX (getPOSIXTime, posixSecondsToUTCTime)
import Data.Time.Format (defaultTimeLocale, formatTime,
iso8601DateFormat)
@@ -33,7 +33,8 @@ import System.Directory (getHomeDirectory, removeFile,
renamePath, withCurrentDirectory)
import System.Exit (ExitCode (..))
import System.FilePath ((<.>), (</>))
-import System.IO (IOMode (WriteMode), hClose, openFile,
+import System.IO (IOMode (WriteMode), hClose,
+ hGetContents, hPutStr, openFile,
withFile)
import System.IO.Temp (withSystemTempDirectory)
import System.Process
@@ -65,12 +66,21 @@ processScanSessDir st dir = withCurrentDirectory dir $ do
-- 3. maybe ocrmypdf
when (st^.stOCR) $ do
renamePath (dir </> "temp.pdf") (dir </> "temp2.pdf")
- void $ createProcessWait_ "OCRmyPDF"
- (proc "ocrmypdf" ["-c", "-i", "-r", "temp2.pdf", "temp.pdf"])
+ -- OCRmyPDF dies if stdout is not connected, so tell it
+ -- to output to stdout
+ void $ withFile "temp.pdf" WriteMode $ \tempFile -> do
+ (_, _, Just herr, p) <- createProcess_ "OCRmyPDF"
+ (proc "ocrmypdf" ["-c", "-i", "-r", "temp2.pdf", "-"])
{ std_in = NoStream
- , std_out = NoStream
- , std_err = UseHandle logH
+ , std_out = UseHandle tempFile
+ , std_err = CreatePipe
}
+ -- OCRmyPDF does not yet have a --quiet option, so we
+ -- emulate the chronic(1) utility
+ exitCode <- waitForProcess p
+ unless (exitCode == ExitSuccess) $
+ hGetContents herr >>= hPutStr logH
+ hClose herr
-- 4. qpdf (ocrmypdf invokes qpdf but it doesn't use
-- --linearize, which shrinks the PDF, often substantially)
void $ createProcessWait_ "qpdf"