@@ -38,6 +38,7 @@ import Text.Pandoc.Compat.Monoid ((<>))
3838import System.Exit (ExitCode (..))
3939import System.FilePath
4040import System.IO (stderr, stdout)
41+import System.IO.Temp (withTempFile)
4142import System.Directory
4243import Data.Digest.Pure.SHA (showDigest, sha1)
4344import System.Environment
@@ -49,7 +50,7 @@ import qualified Text.Pandoc.UTF8 as UTF8
4950import Text.Pandoc.Definition
5051import Text.Pandoc.Walk (walkM)
5152import Text.Pandoc.Shared (fetchItem', warn, withTempDir, inDirectory)
52−import Text.Pandoc.Options (WriterOptions(..))
53+import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..))
5354import Text.Pandoc.MIME (extensionFromMimeType, getMimeType)
5455import Text.Pandoc.Process (pipeProcess)
5556import qualified Data.ByteString.Lazy as BL
@@ -63,18 +64,29 @@ changePathSeparators :: FilePath -> FilePath
6364changePathSeparators = intercalate "/" . splitDirectories
6465#endif
6566
66−makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex)
67+makePDF :: String -- ^ pdf creator (pdflatex, lualatex,
68+ -- xelatex, context, wkhtmltopdf)
6769 -> (WriterOptions -> Pandoc -> String) -- ^ writer
6870 -> WriterOptions -- ^ options
6971 -> Pandoc -- ^ document
7072 -> IO (Either ByteString ByteString)
73+makePDF "wkhtmltopdf" writer opts doc =
74+ wkhtml2pdf (writerVerbose opts) args source
75+ where args = case writerHTMLMathMethod opts of
76+ -- with MathJax, wait til all math is rendered:
77+ MathJax _ -> ["--run-script", "MathJax.Hub.Register.StartupHook('End Typeset', function() { window.status = 'mathjax_loaded' });",
78+ "--window-status", "mathjax_loaded"]
79+ _ -> []
80+ source = writer opts doc
7181makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
7282 doc' <- handleImages opts tmpdir doc
7383 let source = writer opts doc'
7484 args = writerLaTeXArgs opts
7585 case program of
7686 "context" -> context2pdf (writerVerbose opts) tmpdir source
77− _ -> tex2pdf' (writerVerbose opts) args tmpdir program source
87+ _ | program `elem` ["pdflatex", "lualatex", "xelatex"]
88+ -> tex2pdf' (writerVerbose opts) args tmpdir program source
89+ _ -> return $ Left $ UTF8.fromStringLazy $ "Unknown program " ++ program
7890
7991handleImages :: WriterOptions
8092 -> FilePath -- ^ temp dir to store images
@@ -235,6 +247,49 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do
235247 else return Nothing
236248 return (exit, out <> err, pdf)
237249
250+wkhtml2pdf :: Bool -- ^ Verbose output
251+ -> [String] -- ^ Args to wkhtmltopdf
252+ -> String -- ^ HTML5 source
253+ -> IO (Either ByteString ByteString)
254+wkhtml2pdf verbose args source = do
255+ file <- withTempFile "." "wkhtml2pdf.html" $ \fp _ -> return fp
256+ pdfFile <- withTempFile "." "wkhtml2pdf.pdf" $ \fp _ -> return fp
257+ UTF8.writeFile file source
258+ let programArgs = args ++ [file, pdfFile]
259+ env' <- getEnvironment
260+ when verbose $ do
261+ putStrLn "[makePDF] Command line:"
262+ putStrLn $ "wkhtmltopdf" ++ " " ++ unwords (map show programArgs)
263+ putStr "\n"
264+ putStrLn "[makePDF] Environment:"
265+ mapM_ print env'
266+ putStr "\n"
267+ putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
268+ B.readFile file >>= B.putStr
269+ putStr "\n"
270+ (exit, out, err) <- pipeProcess (Just env') "wkhtmltopdf"
271+ programArgs BL.empty
272+ removeFile file
273+ when verbose $ do
274+ B.hPutStr stdout out
275+ B.hPutStr stderr err
276+ putStr "\n"
277+ pdfExists <- doesFileExist pdfFile
278+ mbPdf <- if pdfExists
279+ -- We read PDF as a strict bytestring to make sure that the
280+ -- temp directory is removed on Windows.
281+ -- See https://github.com/jgm/pandoc/issues/1192.
282+ then do
283+ res <- (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
284+ removeFile pdfFile
285+ return res
286+ else return Nothing
287+ let log' = out <> err
288+ return $ case (exit, mbPdf) of
289+ (ExitFailure _, _) -> Left log'
290+ (ExitSuccess, Nothing) -> Left ""
291+ (ExitSuccess, Just pdf) -> Right pdf
292+
238293context2pdf :: Bool -- ^ Verbose output
239294 -> FilePath -- ^ temp directory for output
240295 -> String -- ^ ConTeXt source