A markdown is a markup language to write readable documents and later convert it into different formats. You can use markdown syntax to write formatted documents whose style is already predefined and get a clean readable document. Read my previous post on how to learn about markdown and how to use it. These markdown files are converted into HTML, PDF or email. In this post, I will describe the process to convert a markdown file to a PDF document using pandoc application.
Writing a markdown document
Writing a markdown document requires only a simple text editors like Notepad or Sublime Text. Once you have written you document in markdown, you need to save it with (.md) file. It means markdown. This markdown file must contain correct syntax and no omissions. It there are errors, then you will receive errors when you try to convert your file to PDF document.
Here is an example of markdown document, this file is saved as simple-doc.md.
This document can be converted into many other types of document format with the help of markdown applications.
Markdown Application – Pandoc
A markdown application can range from simple text editor to advanced markdown file processors. But I want to exclude the markdown editors from the list of applications. What I meant by markdown application is an application that may or may not have text editors but includes a markdown parser.
The parser is the code that understands the markdown syntax and convert it to other languages. Similarly, parser can read other types of documents and convert them into markdown. files.
Pandoc is an application that will convert from one markup language to another including markdown files. I will not talk much about Pandoc because it is not in the scope of this article, however, you must know that the Pandoc application has Haskell library with modules for different document formats and a command line tool that we are going to use to convert out documents.
Haskell is another programming language for writing software. To convert our markdown file into PDF, first go to the Pandoc – Installing pandoc page and download the software for your operating system. Since, I am using Microsoft Windows, I downloaded the pandoc=3.0.1-x86_64.msi file. This is the version, at the time of writing this article.
The (,msi) is a simple installer and you must double click and follow the instructions to install the software. Here are the steps.
Step1 – Accept the license agreement. If you want to install for all users, then select “Install for all users on this machine” , it will enable “Advanced” button, that will allow you to choose the installation directory.
Step2 – Click Install to continue. The setup will take few minutes to install the software, depending on the speed of your computer.
Step3 – Click Finish to complete the installation.
The next step is to add support to typeset your PDF document.
Think of markdown file as a content creator with some clarity on the document structure. But it is not possible to format in PDF file unless you have a TEX typesetter. The MiKtex is a windows Tex package that contains information on how to convert your markdown file into beautiful formatted PDF file.
Go to Getting MiKTeX page and download it from there for Windows. Instructions to install the package is also given in the same page.
Converting Markdown File to PDF
You can use pandoc in two different way to convert your file into PDF. Use Command Prompt or Windows Powershell, whichever is comfortable for you.
Open Command Prompt on your Windows computer, and it will display the current path of the system. It is usually, C:\Users\<username>. You must change the directory to the location where you have kept the markdown file.
To change directory, type cd . . , this will take you to one level up in the path. For example, if you are in C:\Drum\My files, then cd . . will take you to C:\Drum.
To enter a directory using command prompt, type cd <name of the directory>. For example, if I am at C:\Drum and want to enter a directory My files inside Drum, type cd My files.
Here is how, I changed the directory and set the path to a folder “BOLGS” that contains my markdown file.
The next step is to verify if Pandoc is working of not. Type pandoc –version at the command prompt and you must see the version information as shown below.
If pandoc is not working, then reinstall it again.
To convert the markdown file to PDF, run the following command.
pandoc -s -o sample-doc.pdf sample-doc.md
This command will take few seconds to run and will convert your markdown file to beautiful PDF file.
Check the Directory for PDF Output
I went to the directory C:\BLOGS for PDF output and here is the output PDF file. It is clean and perfectly formatted for me.
Markdown to PDF file can be performed using some markdown application with a markdown parser. One such application is Pandoc. To convert markdown to PDF, install Pandoc and Tex package called MiKteX for windows. Then use command prompt to change directory where markdown file is located and run command to convert the document to a PDF file. The PDF file will be created in the same directory as the markdown file.