Web - Proudly sponsored by SiteGround
Article

# Creating PDFs from Markdown with Pandoc and LaTeX

We teamed up with SiteGround
pandoc --from=markdown+yaml_metadata_block --template _layouts/cards.latex -o pod/pdf/cards/"$(basename "$filename" .md)".pdf --latex-engine=xelatex $filename done The script processes every Markdown file in the _cards directory, ensuring that the Markdown front matter fields are observed. Using the cards.latex template (we'll look at that next), the correct LaTeX engine outputs a PDF with an appropriate name. ### The LaTeX File A lot of the magic for generating the card files from Pandoc takes place in a LaTeX template. LaTeX is new to me, but it isn't too complex. I'll explain what I changed from the default LaTeX file (found in Pandoc_install_dir/data/templates/default.latex) to make the cards work. I recommend sharelatex.com for previewing LaTeX files as you edit them. \usepackage[paperheight=9.0cm,paperwidth=5.7cm,margin=0.5cm]{geometry} % Set page size \usepackage{multicol} % We need column layouts \usepackage{amsthm,amsmath,amssymb} \usepackage{graphicx} % We want images in our layout \graphicspath{{/Users/chrisward/Workspace/cs_jk/_site/assets/images/cards/}} % Where are images located \usepackage{float} \usepackage[utf8]{inputenc} \usepackage{fontspec} \setmainfont{VT323} % We want to use a custom font installed on our local system, so add that package and select the font We need a specific page size, and we'll use columns later for the costs and scores of the cards. We're using graphics and custom fonts, so we need those packages. We're attempting to create a simple layout that's clear and uncluttered. Here's how we accomplished it: \begin{document} \begin{flushright} {$title$} \end{flushright}$if(image)$\begin{figure}[H] \centering \includegraphics[height=2cm]{$image$} \end{figure}$endif$\begin{flushleft} \scriptsize{$body$} \end{flushleft} \scriptsize \begin{tabular}{ l l } Costs & Scores \\$if(staffcost)$Staff: {$staffcost$}$endif$&$if(loyaltyscore)$Loyalty: {$loyaltyscore$}$endif$\\$if(rdcost)$RandD: {$rdcost$}$endif$&$if(profitscore)$Profit: {$profitscore$} for {$profitlength$} turns$endif$\\$if(marketingcost)$Marketing: {$marketingcost$}$endif$&$if(longevityscore)$Longevity: {$longevityscore$}$endif$\\$if(longevitycost)$Longevity: {$longevitycost$}$endif$& \\$if(moneycost)$Money: {$moneycost$}$endif$& \end{tabular} \begin{flushleft}$if(specialscore)$Special: {$specialscore$}$endif$\end{flushleft} \begin{center}$if(legal)$\tiny{$legal$}$endif\end{center} \end{document} I feel a lot of the above is reasonably self explanatory for anyone used to code or markup. We're creating the elements of the card, aligning them, setting font sizes and checking if there are values before outputting them, so that the card doesn't end up with empty fields. We resize the image to a particular size and centre it. The costs and score values are in a two-column layout, set with the begin{tabular} command and the column quantity with the number of ls. ### Combining Cards Onto One Page We use PDFJam to create a large PDF file combining each of the individual PDF cards: pdfjam pod/pdf/cards/*.pdf --no-landscape --frame true --nup 3x3 --suffix complete --outfile ./cards.pdf mv cards pod/cards_complete.pdf With this command, we specificy the following: • that the page orientation should always be portrait • that each individual PDF should be framed • the grid size • a file name suffix • a file name. PDFJam can give an error if you don't output into its working directory, so I move the file to where I actually want it (hopefully that's solvable in the future). Here we could also delete the individual PDF files if we don't want them. And that's it—we have a website and printable PDF of the game cards. ### Running the Script I run the build script with ./build.sh. As there's a lot of image and PDF processing, it takes about five to ten minutes. I then have a separate script that deploys these folders to a web server. ## What's Next This process has taken me a while to get right, but it's now good enough to move forwards and finesse the process and layouts after play testing. I hope you find my research and experiments useful for your projects. Please let me know if you have any comments or suggestions. More: We teamed up with SiteGround To bring you up to 65% off web hosting, plus free access to the entire SitePoint Premium library (worth99). Get SiteGround + SitePoint Premium Now
Login or Create Account to Comment