Markdown2pdf source code for Linux
- Tutorial
Foreword
Markdown is a great way to write a short article, and sometimes quite voluminous text, with simple formatting in italics and thick print. Markdown is also good for writing articles with source code included. But sometimes I want to transfer it without loss, dancing with a tambourine to a regular, well-designed PDF file, and so that there are no problems during conversion, which, for example, were mine - you can’t write in Russian in the source code comments, lines that are too long are not transferred, but other minor issues are cut off. The instruction will allow you to quickly configure the md2pdf converter without particularly understanding how it works. The script for a less automatic installation is below in the appropriate section.
Install TexLive
Of course, you can install only the necessary parts of this package. But personally, I was frankly too lazy to look for the minimum necessary working installation. For everything to work, install the entire TexLive package. It is called texlive-full and weighs a little more than 2 gigabytes, keep this fact in mind. We execute the command:
user@hostname:~$ sudo apt install texlive-full -y
After a sufficiently long installation, you can proceed to the next step.
Install Pandoc Converter
Pandoc is a Linux package that allows you to convert some text formats to others. It has many interesting features that you can familiarize yourself with on the Internet. We are only interested in the ability to convert markdown file to PDF. Check if Pandoc is installed and if not, install it. For example, like this:
user@hostname:~$ dpkg -s pandoc
If the output says that it is not installed - install:
user@hostname:~$ sudo apt install pandoc -y
Install MD2PDF
You can proceed to the script page on GitHub, and then proceed according to the instructions.
Or download the archive , unzip it to any folder, open it in the terminal and again follow the instructions.
Open a terminal and do:
git clone https://github.com/melichron/md2pdf.git && cd md2pdf
Then execute as root, for example:
sudo ./install.sh
Keep in mind the script uses the utility for building console whiptail dialogs . If you don’t have it installed, or you don’t want to install it, or want to do everything yourself, then install texlive-full and pandoc manually and follow the instructions further.
Install md2pdf for all users:
root@host:~/md2pdf# cp ./src/template.tex $(kpsewhich -expand-var='$TEXMFLOCAL')
root@host:~/md2pdf# echo "#!/bin/bash" | tee usr/local/bin/md2pdf
root@host:~/md2pdf# echo "pandoc --output=\$1.pdf --from=markdown_github --latex-engine=pdflatex --listings --template=$(kpsewhich -expand-var='$TEXMFLOCAL')/template.tex \$1" | tee -a usr/local/bin/md2pdf
root@host:~/md2pdf# chmod 755 /usr/local/bin/md2pdf
Install md2pdf for the current user:
user@host:~/md2pdf$ cp ./src/template.tex $(kpsewhich -expand-var='$TEXMFHOME')
user@host:~/md2pdf$ echo "#!/bin/bash" | tee ~/.local/bin/md2pdf
user@host:~/md2pdf$ echo "pandoc --output=\$1.pdf --from=markdown_github --latex-engine=pdflatex --listings --template=$(kpsewhich -expand-var='$TEXMFHOME')/template.tex \$1" | tee -a ~/.local/bin/md2pdf
user@host:~/md2pdf$ chmod 755 ~/.local/bin/md2pdf
Using md2pdf
Just open the folder with the Markdown file ( some_file.md ) in the Terminal, and run the command:
user@hostname:~$ md2pdf some_file.md
As a result, the file some_file.md.pdf appears in the folder .
Conclusion
Based on the described method, you can build any style of PDF files, you can also convert other formats instead of md, any supported by Pandoc. I dare to hope that one day this instruction will be useful to 3 and a half people.