git.iko.soy/typeable/pandoc
on master help sign in
Revert piping html to pdf-engine (#4628)

* Revert "PDF: Use withTempDir in html2pdf." We're going back to using tmpFile instead of piping * Revert "html2pdf: inject base tag wih current working directory (#4443)" Fixes #4413

commit7c0ef683116d0308da91a61b15b5c640b0e81eda authorMauro Bieg <mb21@users.noreply.github.com> dateSat 5 May 2018, 18:31 (8 years ago) parent59f0c1d
1 file changed+46−50
src/Text/Pandoc/PDF.hs+46−50
src/Text/Pandoc/PDF.hs+46−50
@@ -45,26 +45,23 @@ import qualified Data.ByteString.Lazy.Char8 as BC
4545import Data.Maybe (fromMaybe)
4646import Data.Text (Text)
4747import qualified Data.Text as T
48−import qualified Data.Text.IO as TextIO
4948import System.Directory
5049import System.Environment
5150import System.Exit (ExitCode (..))
5251import System.FilePath
5352import System.IO (stdout)
54−import System.IO.Temp (withTempDirectory)
53+import System.IO.Temp (withTempDirectory, withTempFile)
5554#if MIN_VERSION_base(4,8,3)
5655import System.IO.Error (IOError, isDoesNotExistError)
5756#else
5857import System.IO.Error (isDoesNotExistError)
5958#endif
60−import Text.HTML.TagSoup
61−import Text.HTML.TagSoup.Match
6259import Text.Pandoc.Definition
6360import Text.Pandoc.Error (PandocError (PandocPDFProgramNotFoundError))
6461import Text.Pandoc.MIME (getMimeType)
6562import Text.Pandoc.Options (HTMLMathMethod (..), WriterOptions (..))
6663import Text.Pandoc.Process (pipeProcess)
67−import Text.Pandoc.Shared (inDirectory, stringify, withTempDir)
64+import Text.Pandoc.Shared (inDirectory, stringify)
6865import qualified Text.Pandoc.UTF8 as UTF8
6966import Text.Pandoc.Walk (walkM)
7067import Text.Pandoc.Writers.Shared (getField, metaToJSON)
@@ -365,51 +362,50 @@ html2pdf :: Verbosity -- ^ Verbosity level
365362 -> [String] -- ^ Args to program
366363 -> Text -- ^ HTML5 source
367364 -> IO (Either ByteString ByteString)
368−html2pdf verbosity program args htmlSource = do
369− cwd <- getCurrentDirectory
370− let tags = parseTags htmlSource
371− (hd, tl) = break (tagClose (== "head")) tags
372− baseTag = TagOpen "base"
373− [("href", T.pack cwd <> T.singleton pathSeparator)] : [TagText "\n"]
374− source = renderTags $ hd ++ baseTag ++ tl
375− withTempDir "html2pdf.pdf" $ \tmpdir -> do
376− let pdfFile = tmpdir </> "out.pdf"
377− let pdfFileArgName = ["-o" | program == "prince"]
378− let programArgs = args ++ ["-"] ++ pdfFileArgName ++ [pdfFile]
379− env' <- getEnvironment
380− when (verbosity >= INFO) $ do
381− putStrLn "[makePDF] Command line:"
382− putStrLn $ program ++ " " ++ unwords (map show programArgs)
383− putStr "\n"
384− putStrLn "[makePDF] Environment:"
385− mapM_ print env'
386− putStr "\n"
387− putStrLn "[makePDF] Contents of intermediate HTML:"
388− TextIO.putStr source
389− putStr "\n"
390− (exit, out) <- E.catch
391− (pipeProcess (Just env') program programArgs $ BL.fromStrict $ UTF8.fromText source)
392− (\(e :: IOError) -> if isDoesNotExistError e
393− then E.throwIO $
394− PandocPDFProgramNotFoundError program
395− else E.throwIO e)
396− when (verbosity >= INFO) $ do
397− BL.hPutStr stdout out
398− putStr "\n"
399− pdfExists <- doesFileExist pdfFile
400− mbPdf <- if pdfExists
401− -- We read PDF as a strict bytestring to make sure that the
402− -- temp directory is removed on Windows.
403− -- See https://github.com/jgm/pandoc/issues/1192.
404− then do
405− res <- (Just . BL.fromChunks . (:[])) `fmap` BS.readFile pdfFile
406− removeFile pdfFile
407− return res
408− else return Nothing
409− return $ case (exit, mbPdf) of
410− (ExitFailure _, _) -> Left out
411− (ExitSuccess, Nothing) -> Left ""
412− (ExitSuccess, Just pdf) -> Right pdf
365+html2pdf verbosity program args source = do
366+ -- write HTML to temp file so we don't have to rewrite
367+ -- all links in `a`, `img`, `style`, `script`, etc. tags,
368+ -- and piping to weasyprint didn't work on Windows either.
369+ file <- withTempFile "." "html2pdf.html" $ \fp _ -> return fp
370+ pdfFile <- withTempFile "." "html2pdf.pdf" $ \fp _ -> return fp
371+ BS.writeFile file $ UTF8.fromText source
372+ let pdfFileArgName = ["-o" | program == "prince"]
373+ let programArgs = args ++ [file] ++ pdfFileArgName ++ [pdfFile]
374+ env' <- getEnvironment
375+ when (verbosity >= INFO) $ do
376+ putStrLn "[makePDF] Command line:"
377+ putStrLn $ program ++ " " ++ unwords (map show programArgs)
378+ putStr "\n"
379+ putStrLn "[makePDF] Environment:"
380+ mapM_ print env'
381+ putStr "\n"
382+ putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
383+ BL.readFile file >>= BL.putStr
384+ putStr "\n"
385+ (exit, out) <- E.catch
386+ (pipeProcess (Just env') program programArgs BL.empty)
387+ (\(e :: IOError) -> if isDoesNotExistError e
388+ then E.throwIO $
389+ PandocPDFProgramNotFoundError program
390+ else E.throwIO e)
391+ removeFile file
392+ when (verbosity >= INFO) $ do
393+ BL.hPutStr stdout out
394+ putStr "\n"
395+ pdfExists <- doesFileExist pdfFile
396+ mbPdf <- if pdfExists
397+ -- We read PDF as a strict bytestring to make sure that the
398+ -- temp directory is removed on Windows.
399+ -- See https://github.com/jgm/pandoc/issues/1192.
400+ then do
401+ res <- (Just . BL.fromChunks . (:[])) `fmap` BS.readFile pdfFile
402+ removeFile pdfFile
403+ return res
404+ else return Nothing
405+ return $ case (exit, mbPdf) of
406+ (ExitFailure _, _) -> Left out
407+ (ExitSuccess, Nothing) -> Left ""
408+ (ExitSuccess, Just pdf) -> Right pdf
413409
414410context2pdf :: Verbosity -- ^ Verbosity level
415411 -> FilePath -- ^ temp directory for output