使用pandoc作为库来制作PDF [英] Using pandoc as a library to make a PDF

查看:92
本文介绍了使用pandoc作为库来制作PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究的项目的一部分涉及使用Pandoc创建PDF.我有制作PDF的程序部分.为了弄清楚如何做到这一点,我正在尝试从 JGM BayHack 2014 修改fuel.hs.

但是,我遇到了困难.我具有以下功能:

export :: (MonadIO m) => Pandoc -> m (Either BL.ByteString BL.ByteString)
export = liftIO . makePDF "xelatex" writeLaTeX  def { writerStandalone = True } 

在我修改过的燃料的主体中.

  pdfbytes <- export letter
  print pdfbytes

我得到以下输出:

$ 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"

但是,被引用的日志文件不存在.我不确定如何调试它.我已经安装了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"

要获取模板,可以从外壳程序以独立模式使用Pandoc:

pandoc -D latex > template.tex

此外,在查找默认模板方面,使用堆栈,cabal和系统软件包管理器安装Pandoc可能存在问题.我不确定所有这些如何相互作用.


完全包含要点此处.

Part of a project I am working on involves creating a PDF, using Pandoc. I have the part of the program which makes a PDF. To figure out how to do this, I am trying to modify fuel.hs from JGM BayHack 2014.

However, I am having difficulty. I have the following function:

export :: (MonadIO m) => Pandoc -> m (Either BL.ByteString BL.ByteString)
export = liftIO . makePDF "xelatex" writeLaTeX  def { writerStandalone = True } 

In the body of my modified fuel.hs,

  pdfbytes <- export letter
  print pdfbytes

I get the following output:

$ 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"

However, the log file that is being referenced does not exist. I am not sure how to debug this. I have xelatex installed.


解决方案

With great help from #haskell IRC, I was able to get it working. The key was to add my own LaTeX template. Thus, one can use the following:

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"

To get a template, you can use Pandoc in standalone mode from the shell:

pandoc -D latex > template.tex

Also, there may be an issue with Pandoc installed using stack, using cabal, and using the system package manger, in terms of finding the default templates. I am not sure exactly how all of this interacts.


Fully contained gist here.

这篇关于使用pandoc作为库来制作PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆