@@ -79,13 +79,52 @@ changePathSeparators = intercalate "/" . splitDirectories
7979#endif
8080
8181makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex,
82− -- wkhtmltopdf, weasyprint, prince, context, pdfroff)
82+ -- wkhtmltopdf, weasyprint, prince, context, pdfroff,
83+ -- or path to executable)
8384 -> [String] -- ^ arguments to pass to pdf creator
8485 -> (WriterOptions -> Pandoc -> PandocIO Text) -- ^ writer
8586 -> WriterOptions -- ^ options
8687 -> Pandoc -- ^ document
8788 -> PandocIO (Either ByteString ByteString)
88−makePDF "wkhtmltopdf" pdfargs writer opts doc@(Pandoc meta _) = do
89+makePDF program pdfargs writer opts doc = do
90+ case takeBaseName program of
91+ "wkhtmltopdf" -> makeWithWkhtmltopdf program pdfargs writer opts doc
92+ prog | prog `elem` ["weasyprint", "prince"] -> do
93+ source <- writer opts doc
94+ verbosity <- getVerbosity
95+ liftIO $ html2pdf verbosity program pdfargs source
96+ "pdfroff" -> do
97+ source <- writer opts doc
98+ let args = ["-ms", "-mpdfmark", "-e", "-t", "-k", "-KUTF-8", "-i",
99+ "--no-toc-relocation"] ++ pdfargs
100+ verbosity <- getVerbosity
101+ liftIO $ ms2pdf verbosity program args source
102+ baseProg -> do
103+ -- With context and latex, we create a temp directory within
104+ -- the working directory, since pdflatex sometimes tries to
105+ -- use tools like epstopdf.pl, which are restricted if run
106+ -- on files outside the working directory.
107+ let withTemp = withTempDirectory "."
108+ commonState <- getCommonState
109+ verbosity <- getVerbosity
110+ liftIO $ withTemp "tex2pdf." $ \tmpdir -> do
111+ source <- runIOorExplode $ do
112+ putCommonState commonState
113+ doc' <- handleImages tmpdir doc
114+ writer opts doc'
115+ case baseProg of
116+ "context" -> context2pdf verbosity program tmpdir source
117+ prog | prog `elem` ["pdflatex", "lualatex", "xelatex"]
118+ -> tex2pdf verbosity program pdfargs tmpdir source
119+ _ -> return $ Left $ UTF8.fromStringLazy $ "Unknown program " ++ program
120+
121+makeWithWkhtmltopdf :: String -- ^ wkhtmltopdf or path
122+ -> [String] -- ^ arguments
123+ -> (WriterOptions -> Pandoc -> PandocIO Text) -- ^ writer
124+ -> WriterOptions -- ^ options
125+ -> Pandoc -- ^ document
126+ -> PandocIO (Either ByteString ByteString)
127+makeWithWkhtmltopdf program pdfargs writer opts doc@(Pandoc meta _) = do
89128 let mathArgs = case writerHTMLMathMethod opts of
90129 -- with MathJax, wait til all math is rendered:
91130 MathJax _ -> ["--run-script", "MathJax.Hub.Register.StartupHook('End Typeset', function() { window.status = 'mathjax_loaded' });",
@@ -111,39 +150,7 @@ makePDF "wkhtmltopdf" pdfargs writer opts doc@(Pandoc meta _) = do
111150 ]
112151 source <- writer opts doc
113152 verbosity <- getVerbosity
114− liftIO $ html2pdf verbosity "wkhtmltopdf" args source
115−makePDF "weasyprint" pdfargs writer opts doc = do
116− source <- writer opts doc
117− verbosity <- getVerbosity
118− liftIO $ html2pdf verbosity "weasyprint" pdfargs source
119−makePDF "prince" pdfargs writer opts doc = do
120− source <- writer opts doc
121− verbosity <- getVerbosity
122− liftIO $ html2pdf verbosity "prince" pdfargs source
123−makePDF "pdfroff" pdfargs writer opts doc = do
124− source <- writer opts doc
125− let args = ["-ms", "-mpdfmark", "-e", "-t", "-k", "-KUTF-8", "-i",
126− "--no-toc-relocation"] ++ pdfargs
127− verbosity <- getVerbosity
128− liftIO $ ms2pdf verbosity args source
129−makePDF program pdfargs writer opts doc = do
130− -- With context and latex, we create a temp directory within
131− -- the working directory, since pdflatex sometimes tries to
132− -- use tools like epstopdf.pl, which are restricted if run
133− -- on files outside the working directory.
134− let withTemp = withTempDirectory "."
135− commonState <- getCommonState
136− verbosity <- getVerbosity
137− liftIO $ withTemp "tex2pdf." $ \tmpdir -> do
138− source <- runIOorExplode $ do
139− putCommonState commonState
140− doc' <- handleImages tmpdir doc
141− writer opts doc'
142− case takeBaseName program of
143− "context" -> context2pdf verbosity tmpdir source
144− prog | prog `elem` ["pdflatex", "lualatex", "xelatex"]
145− -> tex2pdf' verbosity pdfargs tmpdir program source
146− _ -> return $ Left $ UTF8.fromStringLazy $ "Unknown program " ++ program
153+ liftIO $ html2pdf verbosity program args source
147154
148155handleImages :: FilePath -- ^ temp dir to store images
149156 -> Pandoc -- ^ document
@@ -195,13 +202,13 @@ convertImage tmpdir fname =
195202 mime = getMimeType fname
196203 doNothing = return (Right fname)
197204
198−tex2pdf' :: Verbosity -- ^ Verbosity level
199− -> [String] -- ^ Arguments to the latex-engine
200− -> FilePath -- ^ temp directory for output
201− -> String -- ^ tex program
202− -> Text -- ^ tex source
203− -> IO (Either ByteString ByteString)
204−tex2pdf' verbosity args tmpDir program source = do
205+tex2pdf :: Verbosity -- ^ Verbosity level
206+ -> String -- ^ tex program
207+ -> [String] -- ^ Arguments to the latex-engine
208+ -> FilePath -- ^ temp directory for output
209+ -> Text -- ^ tex source
210+ -> IO (Either ByteString ByteString)
211+tex2pdf verbosity program args tmpDir source = do
205212 let numruns = if "\\tableofcontents" `T.isInfixOf` source
206213 then 3 -- to get page numbers
207214 else 2 -- 1 run won't give you PDF bookmarks
@@ -328,14 +335,15 @@ runTeXProgram verbosity program args runNumber numRuns tmpDir source = do
328335 return (exit, log', pdf)
329336
330337ms2pdf :: Verbosity
338+ -> String
331339 -> [String]
332340 -> Text
333341 -> IO (Either ByteString ByteString)
334−ms2pdf verbosity args source = do
342+ms2pdf verbosity program args source = do
335343 env' <- getEnvironment
336344 when (verbosity >= INFO) $ do
337345 putStrLn "[makePDF] Command line:"
338− putStrLn $ "pdfroff " ++ " " ++ unwords (map show args)
346+ putStrLn $ program ++ " " ++ unwords (map show args)
339347 putStr "\n"
340348 putStrLn "[makePDF] Environment:"
341349 mapM_ print env'
@@ -344,11 +352,11 @@ ms2pdf verbosity args source = do
344352 putStr $ T.unpack source
345353 putStr "\n"
346354 (exit, out) <- E.catch
347− (pipeProcess (Just env') "pdfroff" args
355+ (pipeProcess (Just env') program args
348356 (BL.fromStrict $ UTF8.fromText source))
349357 (\(e :: IOError) -> if isDoesNotExistError e
350358 then E.throwIO $
351− PandocPDFProgramNotFoundError "pdfroff"
359+ PandocPDFProgramNotFoundError program
352360 else E.throwIO e)
353361 when (verbosity >= INFO) $ do
354362 BL.hPutStr stdout out
@@ -358,7 +366,7 @@ ms2pdf verbosity args source = do
358366 ExitSuccess -> Right out
359367
360368html2pdf :: Verbosity -- ^ Verbosity level
361− -> String -- ^ Program (wkhtmltopdf, weasyprint or prince)
369+ -> String -- ^ Program (wkhtmltopdf, weasyprint, prince, or path)
362370 -> [String] -- ^ Args to program
363371 -> Text -- ^ HTML5 source
364372 -> IO (Either ByteString ByteString)
@@ -369,7 +377,7 @@ html2pdf verbosity program args source = do
369377 file <- withTempFile "." "html2pdf.html" $ \fp _ -> return fp
370378 pdfFile <- withTempFile "." "html2pdf.pdf" $ \fp _ -> return fp
371379 BS.writeFile file $ UTF8.fromText source
372− let pdfFileArgName = ["-o" | program == "prince"]
380+ let pdfFileArgName = ["-o" | takeBaseName program == "prince"]
373381 let programArgs = args ++ [file] ++ pdfFileArgName ++ [pdfFile]
374382 env' <- getEnvironment
375383 when (verbosity >= INFO) $ do
@@ -408,10 +416,11 @@ html2pdf verbosity program args source = do
408416 (ExitSuccess, Just pdf) -> Right pdf
409417
410418context2pdf :: Verbosity -- ^ Verbosity level
419+ -> String -- ^ "context" or path to it
411420 -> FilePath -- ^ temp directory for output
412421 -> Text -- ^ ConTeXt source
413422 -> IO (Either ByteString ByteString)
414−context2pdf verbosity tmpDir source = inDirectory tmpDir $ do
423+context2pdf verbosity program tmpDir source = inDirectory tmpDir $ do
415424 let file = "input.tex"
416425 BS.writeFile file $ UTF8.fromText source
417426#ifdef _WINDOWS
@@ -426,7 +435,7 @@ context2pdf verbosity tmpDir source = inDirectory tmpDir $ do
426435 putStrLn "[makePDF] temp dir:"
427436 putStrLn tmpDir'
428437 putStrLn "[makePDF] Command line:"
429− putStrLn $ "context" ++ " " ++ unwords (map show programArgs)
438+ putStrLn $ program ++ " " ++ unwords (map show programArgs)
430439 putStr "\n"
431440 putStrLn "[makePDF] Environment:"
432441 mapM_ print env'
@@ -435,7 +444,7 @@ context2pdf verbosity tmpDir source = inDirectory tmpDir $ do
435444 BL.readFile file >>= BL.putStr
436445 putStr "\n"
437446 (exit, out) <- E.catch
438− (pipeProcess (Just env') "context" programArgs BL.empty)
447+ (pipeProcess (Just env') program programArgs BL.empty)
439448 (\(e :: IOError) -> if isDoesNotExistError e
440449 then E.throwIO $
441450 PandocPDFProgramNotFoundError "context"