diff options
author | John MacFarlane <jgm@berkeley.edu> | 2015-12-21 22:13:44 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2015-12-21 22:13:44 -0800 |
commit | 0596b65a74a543fd065a672da6445f295d05fc36 (patch) | |
tree | 326fd2cd6f346274acf2e66c9199fe4eeffdbe62 /src/Text/Pandoc | |
parent | 0a768f1cc568722973302554265efa414d097da3 (diff) | |
download | pandoc-0596b65a74a543fd065a672da6445f295d05fc36.tar.gz |
pdf via wkhtmltopdf: take `title` and `page-size` from metadata.
Adjusted default `page-size` to `letter`, to match current LaTeX
template.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r-- | src/Text/Pandoc/PDF.hs | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/src/Text/Pandoc/PDF.hs b/src/Text/Pandoc/PDF.hs index 72b563044..852e8c033 100644 --- a/src/Text/Pandoc/PDF.hs +++ b/src/Text/Pandoc/PDF.hs @@ -46,10 +46,13 @@ import Control.Monad (unless, when, (<=<)) import qualified Control.Exception as E import Data.List (isInfixOf) import Data.Maybe (fromMaybe) +import qualified Data.Map as M import qualified Text.Pandoc.UTF8 as UTF8 import Text.Pandoc.Definition import Text.Pandoc.Walk (walkM) -import Text.Pandoc.Shared (fetchItem', warn, withTempDir, inDirectory) +import Text.Pandoc.Shared (fetchItem', warn, withTempDir, inDirectory, + stringify) +import Text.Pandoc.Writers.Shared (getField, metaToJSON) import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..)) import Text.Pandoc.MIME (extensionFromMimeType, getMimeType) import Text.Pandoc.Process (pipeProcess) @@ -70,14 +73,20 @@ makePDF :: String -- ^ pdf creator (pdflatex, lualatex, -> WriterOptions -- ^ options -> Pandoc -- ^ document -> IO (Either ByteString ByteString) -makePDF "wkhtmltopdf" writer opts doc = - wkhtml2pdf (writerVerbose opts) args source - where args = case writerHTMLMathMethod opts of - -- with MathJax, wait til all math is rendered: +makePDF "wkhtmltopdf" writer opts doc@(Pandoc meta _) = do + let mathArgs = case writerHTMLMathMethod opts of + -- with MathJax, wait til all math is rendered: MathJax _ -> ["--run-script", "MathJax.Hub.Register.StartupHook('End Typeset', function() { window.status = 'mathjax_loaded' });", "--window-status", "mathjax_loaded"] _ -> [] - source = writer opts doc + meta' <- metaToJSON opts (return . stringify) (return . stringify) meta + let toArgs (f, d) = maybe (maybe [] (\x -> ['-':'-':f, x]) d) + (\x -> ['-':'-':f, x]) $ getField f meta' + let args = mathArgs ++ + concatMap toArgs [("page-size", Just "letter") + ,("title", Nothing)] + let source = writer opts doc + html2pdf (writerVerbose opts) args source makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do doc' <- handleImages opts tmpdir doc let source = writer opts doc' @@ -247,13 +256,13 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do else return Nothing return (exit, out <> err, pdf) -wkhtml2pdf :: Bool -- ^ Verbose output - -> [String] -- ^ Args to wkhtmltopdf - -> String -- ^ HTML5 source - -> IO (Either ByteString ByteString) -wkhtml2pdf verbose args source = do - file <- withTempFile "." "wkhtml2pdf.html" $ \fp _ -> return fp - pdfFile <- withTempFile "." "wkhtml2pdf.pdf" $ \fp _ -> return fp +html2pdf :: Bool -- ^ Verbose output + -> [String] -- ^ Args to wkhtmltopdf + -> String -- ^ HTML5 source + -> IO (Either ByteString ByteString) +html2pdf verbose args source = do + file <- withTempFile "." "html2pdf.html" $ \fp _ -> return fp + pdfFile <- withTempFile "." "html2pdf.pdf" $ \fp _ -> return fp UTF8.writeFile file source let programArgs = args ++ [file, pdfFile] env' <- getEnvironment |