git.iko.soy/typeable/pandoc
on master help sign in
html2pdf: ensure temp file is deleted...

even if the pdf program is not found. Closes #5720.

commit716483e03aca2a9afb6ac777575d8a2f8da878b7 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 31 Aug 2019, 09:18 (7 years ago) parentfba1296
1 file changed+35−34
src/Text/Pandoc/PDF.hs+35−34
src/Text/Pandoc/PDF.hs+35−34
@@ -33,7 +33,7 @@ import System.Directory
3333import System.Environment
3434import System.Exit (ExitCode (..))
3535import System.FilePath
36−import System.IO (stdout)
36+import System.IO (stdout, hClose)
3737import System.IO.Temp (withSystemTempDirectory, withTempDirectory,
3838 withTempFile)
3939import System.IO.Error (IOError, isDoesNotExistError)
@@ -418,39 +418,40 @@ html2pdf verbosity program args source = do
418418 -- write HTML to temp file so we don't have to rewrite
419419 -- all links in `a`, `img`, `style`, `script`, etc. tags,
420420 -- and piping to weasyprint didn't work on Windows either.
421− file <- withTempFile "." "html2pdf.html" $ \fp _ -> return fp
422− pdfFile <- withTempFile "." "html2pdf.pdf" $ \fp _ -> return fp
423− BS.writeFile file $ UTF8.fromText source
424− let pdfFileArgName = ["-o" | takeBaseName program == "prince"]
425− let programArgs = args ++ [file] ++ pdfFileArgName ++ [pdfFile]
426− env' <- getEnvironment
427− when (verbosity >= INFO) $
428− UTF8.readFile file >>=
429− showVerboseInfo Nothing program programArgs env'
430− (exit, out) <- E.catch
431− (pipeProcess (Just env') program programArgs BL.empty)
432− (\(e :: IOError) -> if isDoesNotExistError e
433− then E.throwIO $
434− PandocPDFProgramNotFoundError program
435− else E.throwIO e)
436− removeFile file
437− when (verbosity >= INFO) $ do
438− BL.hPutStr stdout out
439− putStr "\n"
440− pdfExists <- doesFileExist pdfFile
441− mbPdf <- if pdfExists
442− -- We read PDF as a strict bytestring to make sure that the
443− -- temp directory is removed on Windows.
444− -- See https://github.com/jgm/pandoc/issues/1192.
445− then do
446− res <- (Just . BL.fromChunks . (:[])) `fmap` BS.readFile pdfFile
447− removeFile pdfFile
448− return res
449− else return Nothing
450− return $ case (exit, mbPdf) of
451− (ExitFailure _, _) -> Left out
452− (ExitSuccess, Nothing) -> Left ""
453− (ExitSuccess, Just pdf) -> Right pdf
421+ withTempFile "." "html2pdf.html" $ \file h1 ->
422+ withTempFile "." "html2pdf.pdf" $ \pdfFile h2 -> do
423+ hClose h1
424+ hClose h2
425+ BS.writeFile file $ UTF8.fromText source
426+ let pdfFileArgName = ["-o" | takeBaseName program == "prince"]
427+ let programArgs = args ++ [file] ++ pdfFileArgName ++ [pdfFile]
428+ env' <- getEnvironment
429+ when (verbosity >= INFO) $
430+ UTF8.readFile file >>=
431+ showVerboseInfo Nothing program programArgs env'
432+ (exit, out) <- E.catch
433+ (pipeProcess (Just env') program programArgs BL.empty)
434+ (\(e :: IOError) -> if isDoesNotExistError e
435+ then E.throwIO $
436+ PandocPDFProgramNotFoundError program
437+ else E.throwIO e)
438+ when (verbosity >= INFO) $ do
439+ BL.hPutStr stdout out
440+ putStr "\n"
441+ pdfExists <- doesFileExist pdfFile
442+ mbPdf <- if pdfExists
443+ -- We read PDF as a strict bytestring to make sure that the
444+ -- temp directory is removed on Windows.
445+ -- See https://github.com/jgm/pandoc/issues/1192.
446+ then do
447+ res <- Just . BL.fromChunks . (:[]) <$>
448+ BS.readFile pdfFile
449+ return res
450+ else return Nothing
451+ return $ case (exit, mbPdf) of
452+ (ExitFailure _, _) -> Left out
453+ (ExitSuccess, Nothing) -> Left ""
454+ (ExitSuccess, Just pdf) -> Right pdf
454455
455456context2pdf :: Verbosity -- ^ Verbosity level
456457 -> String -- ^ "context" or path to it