search
HomeTechnology peripheralsIt IndustryCreating PDFs from Markdown with Pandoc and LaTeX

Creating PDFs from Markdown with Pandoc and LaTeX

Core points

This article author Chris Ward explains how to convert Markdown files to PDFs using Pandoc and LaTeX for their open source board game Chip Shop. Game components are written using Markdown, and the game website is also generated by these files.

Pandoc (an open source markup conversion tool) and LaTeX (an document declaration and layout system) are used to generate PDFs from Markdown files. Despite their powerful capabilities, they cannot combine multiple PDFs onto a single page, so Ward uses the command line tool PDFJam to meet this requirement.

The author provides a detailed guide on how to install necessary dependencies (Markdown, Jekyll, Pandoc, LaTeX, PDFJam), and gradually introduces the build process, including generating PDFs from Markdown, creating LaTeX files, and using PDFJam to transfer cards Combine on one page.

The author's ideal workflow is to generate a PDF file while generating a website, rather than when the visitor requests it. This approach also allows the PDF card version to look different from the HTML page without using complex CSS rules.

If you have read some of my posts on SitePoint or elsewhere, you probably know I'm working on a board game. This game, called Chip Shop, allows you to run a computer company in the 1980s America.

Creating PDFs from Markdown with Pandoc and LaTeX

As part of the project, I tried to open source the entire game as much as possible. After a few attempts, I decided to use Markdown as the basic framework for most game components (especially cards and instructions).

Since the game website uses Jekyll, the game website is generated from the Markdown file. I'm going to make a premium pre-boxed and self-printed version of the game, for which I need to generate a PDF from a Markdown file.

Target

My ideal workflow is to generate PDF files while generating the website, not when the visitor requests it. This excludes the option wkhtmltopdf that I usually use for PDF generation, as it is generating PDF from generated HTML. Another reason is that I want the PDF card version to look different from the HTML page, and Jekyll lacks any kind of "view mode" functionality to achieve this without using complex CSS rules.

Chip Shop game's card Markdown template file contains many Markdown pre-information fields for game mechanics, not all fields are used on every card. For easy printing, I need to put the cards on the A4 page as many times as possible—in this case, a 3×3 grid. Ultimately, the page needs to be printed on both sides, but I haven't implemented that yet.

Pandoc and LaTeX

Any web search that generates PDF solutions from Markdown will lead you on the path of Pandoc. Pandoc is an open source Swiss Army knife mark conversion tool that supports a wide variety of input and output mark formats.

To generate PDFs using Pandoc, LaTeX is required. LaTeX originated from the scientific research community and is a document declaration and layout system. Combined with Pandoc and LaTeX, we can use variables to generate PDFs from a series of Markdown files and support Markdown pre-information.

Despite the powerful Pandoc and LaTeX, I can't find any way to combine multiple PDFs (cards) onto a single page, especially when using variables in a Markdown file. After a lot of research, I chose PDFJam, a simple command line tool for this need.

Installing dependencies

Markdown

You don't need extra Markdown software besides possibly needing an editor, there are a lot of editors, and I suggest you read some SitePoint articles to make your choice.

Jekyll

I will continue to use Jekyll to illustrate the build process in the examples taken from my game, but if you don't need a website, it's not a necessary part of PDF generation.

Pandoc

On my Mac, I installed Pandoc using Homebrew, but all operating systems have corresponding options.

LaTeX

There are many words about the best way to install LaTeX, depending on your needs or how you intend to use it. The full installation of its common tools and libraries may be close to 2GB, but for most purposes a minimum installation is sufficient. Read the project's download page to find the option that works best for you.

In this tutorial, we will use the xelatex engine because I use custom fonts. However, you can choose any engine that provides the specific features you need.

PDFJam

Depending on how you install LaTeX, you may have installed PDFJam. (Type which pdfjam in the terminal to check.) If you don't have it installed, look for installation details here.

Building process

After some consideration, I run a bash script running locally seems to be the best choice at the moment. There is a better way, but it works, and I can improve the process later on, transfer it to a continuous integration system or Git Hooks.

View bash scripts on GitHub.

Let's introduce this script step by step now.

Settings

bundle install
bundle update

rm -dfr _site
rm -dfr pod

These commands ensure that the Ruby dependencies required by Jekyll to build a website are up to date, and we deleted any existing website and print folders.

Build a website

jekyll build
mkdir -p pod/pdf/cards

Next, we build the website and create a folder for the printed version of the card.

Generate PDF from Markdown

Let's create a folder containing each Markdown file PDF version:

for filename in _cards/*.md; do
  echo $filename
  pandoc --from=markdown+yaml_metadata_block --template _layouts/cards.latex -o pod/pdf/cards/"$(basename "$filename" .md)".pdf --latex-engine=xelatex $filename
done

This script handles each Markdown file in the _cards directory, making sure to observe the Markdown pre-information field. Using the cards.latex template (which we will cover later), the correct LaTeX engine outputs a PDF with the appropriate name.

LaTeX File

Most of the magic of generating card files from Pandoc happens in LaTeX templates.

View LaTeX templates on GitHub.

LaTeX is new to me, but it is not too complicated. I'll explain what I changed from the default LaTeX file (located in Pandoc_install_dir/data/templates/default.latex) to get the card to work properly. I recommend sharelatex.com for previewing them when editing LaTeX files.

bundle install
bundle update

rm -dfr _site
rm -dfr pod

We need a specific page size and we will use the columns to show the cost and score of the card later. We are using graphics and custom fonts, so we need these packages.

We are trying to create a clear and concise simple layout. Here is how we implement it:

jekyll build
mkdir -p pod/pdf/cards

I think a lot of the above is quite easy to understand for anyone who is used to code or tagging. We are creating elements of the card, aligning them, setting the font size and checking if there are values, and then outputting them so that the card does not end up with empty fields.

We resize the image to a specific size and center it. The cost and score values ​​are arranged in two columns, set using the begin{tabular} command, and the number of columns is set using the number of l.

Creating PDFs from Markdown with Pandoc and LaTeX

Combining cards on one page

We use PDFJam to create a large PDF file with each individual PDF card:

for filename in _cards/*.md; do
  echo $filename
  pandoc --from=markdown+yaml_metadata_block --template _layouts/cards.latex -o pod/pdf/cards/"$(basename "$filename" .md)".pdf --latex-engine=xelatex $filename
done

Use this command, we specify the following:

  • The page orientation should always be vertical
  • Each individual PDF should be framed
  • Grid size
  • File name suffix
  • File name

PDFJam may give an error if you are not outputting to its working directory, so I move the file to where I actually want it (hopefully it will be solved in the future). Here we can also delete a single PDF file if we don't need it.

That's it - we have a printable PDF of websites and game cards.

Creating PDFs from Markdown with Pandoc and LaTeX

Run script

I use ./build.sh to run the build script. Since there are a lot of images and PDF processing, it takes about five to ten minutes. Then I have a separate script to deploy these folders to the web server.

Next steps

This process took me a while to get it right, but it's good enough now to continue to improve the process and layout after the game test.

I hope you find my research and experiments useful to your project. If you have any comments or suggestions, please let me know.

FAQs (FAQs) about creating PDFs from Markdown using Pandoc and LaTeX

How to install Pandoc and LaTeX on my system?

To install Pandoc, you can use it from the official website (https://www.php.cn/link/8f1dd6e7a88b9cf615c146330c591ba9.

Can I customize the appearance of PDFs created using Pandoc and LaTeX?

Yes, you can use LaTeX templates to customize the appearance of the PDF. Pandoc uses the default template to generate PDFs, but you can specify your own templates using the --template option. You can create your own templates or use one of the many templates available online, such as those found in the Wandmalfarbe Pandoc LaTeX template GitHub repository.

How to convert Markdown files to PDF using Pandoc and LaTeX?

To convert a Markdown file to a PDF, you can use the following command in a terminal or command prompt: pandoc yourfile.md -o yourfile.pdf. Replace yourfile.md with the name of your Markdown file and yourfile.pdf with the desired name of your PDF file. This command tells Pandoc to convert Markdown files to PDF using the default LaTeX template.

(The rest of the FAQ content is the same as the original text, omitted here to avoid duplication)

The above is the detailed content of Creating PDFs from Markdown with Pandoc and LaTeX. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Behind the first Android access to DeepSeek: Seeing the power of womenBehind the first Android access to DeepSeek: Seeing the power of womenMar 12, 2025 pm 12:27 PM

The rise of Chinese women's tech power in the field of AI: The story behind Honor's collaboration with DeepSeek women's contribution to the field of technology is becoming increasingly significant. Data from the Ministry of Science and Technology of China shows that the number of female science and technology workers is huge and shows unique social value sensitivity in the development of AI algorithms. This article will focus on Honor mobile phones and explore the strength of the female team behind it being the first to connect to the DeepSeek big model, showing how they can promote technological progress and reshape the value coordinate system of technological development. On February 8, 2024, Honor officially launched the DeepSeek-R1 full-blood version big model, becoming the first manufacturer in the Android camp to connect to DeepSeek, arousing enthusiastic response from users. Behind this success, female team members are making product decisions, technical breakthroughs and users

DeepSeek's 'amazing' profit: the theoretical profit margin is as high as 545%!DeepSeek's 'amazing' profit: the theoretical profit margin is as high as 545%!Mar 12, 2025 pm 12:21 PM

DeepSeek released a technical article on Zhihu, introducing its DeepSeek-V3/R1 inference system in detail, and disclosed key financial data for the first time, which attracted industry attention. The article shows that the system's daily cost profit margin is as high as 545%, setting a new high in global AI big model profit. DeepSeek's low-cost strategy gives it an advantage in market competition. The cost of its model training is only 1%-5% of similar products, and the cost of V3 model training is only US$5.576 million, far lower than that of its competitors. Meanwhile, R1's API pricing is only 1/7 to 1/2 of OpenAIo3-mini. These data prove the commercial feasibility of the DeepSeek technology route and also establish the efficient profitability of AI models.

Midea launches its first DeepSeek air conditioner: AI voice interaction can achieve 400,000 commands!Midea launches its first DeepSeek air conditioner: AI voice interaction can achieve 400,000 commands!Mar 12, 2025 pm 12:18 PM

Midea will soon release its first air conditioner equipped with a DeepSeek big model - Midea fresh and clean air machine T6. The press conference is scheduled to be held at 1:30 pm on March 1. This air conditioner is equipped with an advanced air intelligent driving system, which can intelligently adjust parameters such as temperature, humidity and wind speed according to the environment. More importantly, it integrates the DeepSeek big model and supports more than 400,000 AI voice commands. Midea's move has caused heated discussions in the industry, and is particularly concerned about the significance of combining white goods and large models. Unlike the simple temperature settings of traditional air conditioners, Midea fresh and clean air machine T6 can understand more complex and vague instructions and intelligently adjust humidity according to the home environment, significantly improving the user experience.

Top 10 Best Free Backlink Checker Tools in 2025Top 10 Best Free Backlink Checker Tools in 2025Mar 21, 2025 am 08:28 AM

Website construction is just the first step: the importance of SEO and backlinks Building a website is just the first step to converting it into a valuable marketing asset. You need to do SEO optimization to improve the visibility of your website in search engines and attract potential customers. Backlinks are the key to improving your website rankings, and it shows Google and other search engines the authority and credibility of your website. Not all backlinks are beneficial: Identify and avoid harmful links Not all backlinks are beneficial. Harmful links can harm your ranking. Excellent free backlink checking tool monitors the source of links to your website and reminds you of harmful links. In addition, you can also analyze your competitors’ link strategies and learn from them. Free backlink checking tool: Your SEO intelligence officer

Another national product from Baidu is connected to DeepSeek. Is it open or follow the trend?Another national product from Baidu is connected to DeepSeek. Is it open or follow the trend?Mar 12, 2025 pm 01:48 PM

DeepSeek-R1 empowers Baidu Library and Netdisk: The perfect integration of deep thinking and action has quickly integrated into many platforms in just one month. With its bold strategic layout, Baidu integrates DeepSeek as a third-party model partner and integrates it into its ecosystem, which marks a major progress in its "big model search" ecological strategy. Baidu Search and Wenxin Intelligent Intelligent Platform are the first to connect to the deep search functions of DeepSeek and Wenxin big models, providing users with a free AI search experience. At the same time, the classic slogan of "You will know when you go to Baidu", and the new version of Baidu APP also integrates the capabilities of Wenxin's big model and DeepSeek, launching "AI search" and "wide network information refinement"

Building a Network Vulnerability Scanner with GoBuilding a Network Vulnerability Scanner with GoApr 01, 2025 am 08:27 AM

This Go-based network vulnerability scanner efficiently identifies potential security weaknesses. It leverages Go's concurrency features for speed and includes service detection and vulnerability matching. Let's explore its capabilities and ethical

Prompt Engineering for Web DevelopmentPrompt Engineering for Web DevelopmentMar 09, 2025 am 08:27 AM

AI Prompt Engineering for Code Generation: A Developer's Guide The landscape of code development is poised for a significant shift. Mastering Large Language Models (LLMs) and prompt engineering will be crucial for developers in the coming years. Th

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),