When you are on a Linux based operating system, you are already enjoying the spirit of openness. In this article, we will discuss a famous free and open-source document converter “Pandoc.” Pandoc supports more than 40 file formats and is used as a writing tool. Pandoc can manage section references, tables, bibliographies, and figures in Markdown. Markdown is a standard syntax followed by Researchers while publishing research papers. Pandoc comes with a very reliable “pandoc-crossref” filter. It performs automatic numbering, cross-referencing of a document containing complex figures, tables, and many more.
Many Software Professionals even use Pandoc to document the whole software development life cycle as docs-as-code system. There is a concept of version control, used by Developers to keep track of the changes to their projects. Developers can access the previous version of files using this feature available via Git. The same applies to the people working on research papers or writing documentation. You can write the whole document in a lightweight, text-based markup language and integrate it with version control software(VCS) for versioning. You can later publish the document using “Pandoc” in below formats,

  • Documentation in HTML format ~ Web-based
  • Word-processed or typeset ~ (LibreOffice, Microsoft Word, Tex)
  • Portable document format (PDF)
  • e-Book (ePub) and many more

All of the above benefits can be achieved simply by combining the version controlling with lightweight text files.

Configuring Pandoc

  • Let us install Pandoc in your Linux based operating system. In our case, we will be using Ubuntu as the operating system.
  • Command to install Pandoc in various Linux based Operating systems,
  • Ubuntu: sudo apt-get install pandoc
  • Fedora: sudo dnf install pandoc
  • Manjaro: sudo pacman -Syu pandoc
How To Convert Files On Linux Command Line Using Pandoc
  • After successful installation, you can check the version of Pandoc by using below command,
  • Command: pandoc –version
How To Convert Files On Linux Command Line Using Pandoc

Use Pandoc without any files

  • You can use Pandoc to write text-based documents.
  • You can tell Pandoc that you’ve finished writing by pressing Ctrl+D.
  • The only format that Pandoc understands is Markdown. It would be best if you learned the Markdown format before start using Pandoc. In this tutorial, we will walk you through the basics of the Markdown language.
  • The output is usually in HTML format for Markdown format.

Steps:

  • Open the terminal and type “pandoc” to open the pandoc command-line writer.
How To Convert Files On Linux Command Line Using Pandoc
  • Type few inputs in Markdown format as mentioned below,
How To Convert Files On Linux Command Line Using Pandoc
  • Press CTRL+D to generate the output in HTML format as mentioned below,
How To Convert Files On Linux Command Line Using Pandoc

Markdown Basics


Note: Extension of Markdown files : .md

  • H3 header -> ### Blockquoted header -> <h3>Blockquoted header</h3>
  • Under paragraph tag -> This is blockquote text. -> <p>This is blockquote text.</p>
  • Emphasis format: Italic -> *italic* or _italic_ -> <em>italic</em>
  • Strong format: **bold** or __bold__ -> <strong>bold</strong>
  • Horizontal rule: 3 or more hyphens, asterisks, or underscores on a single line. Example: * * * or *** or ***** -> <hr />
  • Line return: Forcing a line-break\s\s -> Forcing a line-break <br>
  • Links: A link [here] (http://example.com/) -> A link <a href=”http://example.com/”>here</a>
  • Lists: Unordered List -> plus, hyphens or asterisks as list markers

Example :

+ One
– Two
* Three
Output :

<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>

Create a Markdown file

Before you start, create and fill a Markdown file first using below steps in an Ubuntu enviornment,

  • Create a markdown file with .md extension using below command in a terminal using vi editor,
  • Command: vi sample.md
How To Convert Files On Linux Command Line Using Pandoc
  • The vi editor will open the file in a terminal-based text editor and fill it with some markdowns as mentioned below,
How To Convert Files On Linux Command Line Using Pandoc
  • Save the file using the command “:wq” followed by Enter.

Converting files

Let us convert the Markdown-formatted file into HTML format by using below command,
Command: pandoc -o sample.html sample.md

How To Convert Files On Linux Command Line Using Pandoc
  • Upon completion of the conversion, you will be able to see a new file in the folder named “sample.html.”
  • You can double click that file to verify the content of the Markdown in a browser.
  • If you don’t have a browser you can use Lynx by using this command,
  • sudo apt install lynx -> lynx sample.html
How To Convert Files On Linux Command Line Using Pandoc

Specifying File Formats

Pandoc allows you to specify the file formats required during the conversion. Even though it is not mandatory, you sometimes need to specify the format when both the file formats use the same extension, such as “TeX” and “LaTeX” both use the “.tex” extension.
Command: pandoc -f markdown -t latex -s -o sample.tex sample.md
Decode the syntax:

  • -f: from extension
  • -t: to extension
  • -s: standalone option to generate all the Latex preamble required to make the document complete.

Generating PDFs

  • Pandoc makes use of the LatTeX pdf engine to generate pdfs.
  • It is recommended to install a LatTeX editor to satisfy all the dependencies correctly. The easy to use editor is “Texmaker.”
  • Command to install Pandoc in various Linux based Operating systems,
  • Ubuntu: sudo apt-get install texmaker
  • Fedora: sudo dnf install texmaker
  • Manjaro: sudo pacman -Syu texmaker

Easy steps

  • Generate the Office document file(in odt) from the Markdown file by using below command,

Command: pandoc -o sample.odt –reference-doc=odt-template.odt sample.md

  • Open the document in LibreOffice writer and save it as pdf.

Conclusion

Pandoc is a famous open-source conversion tool used by professionals due to its flexibility in conversion to and from more than forty formats. In this article, we have covered the installation and the basic commands of Pandoc. You can play around it by going through the official documentation here.

About 

Harsit Mohapatra is a full-time Software Engineer passionate and hungry for tech updates and knowledge. Besides being a crazy software developer, he is an avid traveller and loves capturing the things that give him a perception through his lenses

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.