我正在研究的项目的一部分涉及使用Pandoc创建PDF.我有程序的一部分制作PDF.为了弄清楚如何做到这一点,我试图fuel.hs从JGM BayHack 2014进行修改.
但是,我遇到了困难.我有以下功能:
export :: (MonadIO m) => Pandoc -> m (Either BL.ByteString BL.ByteString)
export = liftIO . makePDF "xelatex" writeLaTeX def { writerStandalone = True }
Run Code Online (Sandbox Code Playgroud)
在我修改的fuel.hs的正文中,
pdfbytes <- export letter
print pdfbytes
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
$ stack runghc fuel.hs
Run from outside a project, using implicit global project config
Using resolver: lts-3.7 from implicit global project's config file: /home/stevejb/.stack/global/stack.yaml
Left "! Emergency stop.\n<*> /tmp/tex2pdf.8283/input.tex\n \nNo pages of output.\nTranscript written on /tmp/tex2pdf.8283/input.log.\n"
"Fail"
Run Code Online (Sandbox Code Playgroud)
但是,正在引用的日志文件不存在.我不知道如何调试这个.我安装了xelatex.
在#haskell IRC的帮助下,我能够让它运转起来.关键是添加我自己的LaTeX模板.因此,可以使用以下内容:
export :: (MonadIO m) => String -> Pandoc -> m (Either BL.ByteString BL.ByteString)
export tmpl pdoc = liftIO $ makePDF "xelatex" writeLaTeX (def { writerStandalone = True, writerTemplate = tmpl}) pdoc
getLetter = do
json <- BL.readFile "cng_fuel_chicago.json"
let letter = case decode json of
Just stations -> createLetter [s | s <- stations,
"Voyager" `elem` cardsAccepted s]
Nothing -> error "Could not decode JSON"
return $ letter
main :: IO ()
main = do
letter <- getLetter
temp <- readFile "template.tex"
let str_should_have_something = writeLaTeX (def {writerStandalone = True, writerTemplate = temp}) letter
print str_should_have_something
mybytes <- export temp letter
case mybytes of Right b -> BL.writeFile "mypdf.pdf" b
Left _ -> putStrLn "Export error"
Run Code Online (Sandbox Code Playgroud)
要获取模板,您可以在shell中以独立模式使用Pandoc:
pandoc -D latex > template.tex
此外,在查找默认模板方面,可能存在使用堆栈,使用cabal和使用系统包管理器安装Pandoc的问题.我不确定所有这些是如何相互作用的.
完全包含在这里的要点.