Converting a Markdown File to PDF Using Pandoc

September 9, 2012
By

(This article was first published on Data, Evidence, and Policy - Jared Knowles, and kindly contributed to R-bloggers)

Working with knitr and markdown is a great way to share quick reports with colleagues, but in cases where IE8 is still the dominant browser, shipping an HTML file with embedded graphics is a non-starter. IE8 does not support the Data URI format used to embed images directly in the HTML file if those files are greater than 32kb (http://en.wikipedia.org/wiki/Data_URI_scheme). This means that you can't easily share a graphic heavy report as an HTML file with colleagues (and really, what statistical report isn't figure heavy?).

So the next best thing is to ship a PDF. One way to do this would be to print the HTML file from a browser that can display it as a PDF. In this case, the resulting file is generally quite ugly, the images are distorted often, and the header and footer are problematic. Another way is to rewrite your report with Markdown more friendly for conversion into LaTeX and then to PDF. Neither of these is fun, neither is efficient, and neither looks ideal.

Luckily, I found a great way to use pandoc to convert the HTML report into a good looking PDF without resorting to rewriting the report in LaTeX and reknitting. This means you can get the power of Markdown with the portability of PDF for long form documents and one-off data reports. All you need is a handy little script to do the translating from format to format.

You can read the StackOverflow discussion here: http://stackoverflow.com/questions/11025123/how-to-convert-r-markdown-to-pdf

My interpretation/use of this is below:

# Define your report
system("RMDFILE=myreport")
# Knit the Rmd to an Md file
# Convert the MD file to Html
system("Rscript -e 'require(knitr);require(markdown);knit('$RMDFILE.rmd', '$RMDFILE.md');
markdownToHTML('$RMDFILE.md', '$RMDFILE.html', options=c(\"use_xhml\"))'")
require(knitr)
require(markdown)
knit("myreport.Rmd")
markdownToHTML('myreport.md','myreport.html', options=c("use_xhml"))
# convert the system("pandoc -s myreport.html -o myreport.pdf")

To leave a comment for the author, please follow the link and comment on his blog: Data, Evidence, and Policy - Jared Knowles.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.