From 68e5a1faed2fc49b5a2ab433ea8cca754fff957d Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Sun, 12 Mar 2017 15:51:47 -0700 Subject: 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. --- Main.hs | 22 ++++++++++++++++------ 1 file 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 . {-# 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" -- cgit v1.2.3