Markdown to pdf linux

Готовое решение markdown2pdf с исходным кодом для Linux

Предисловие

Markdown это прекрасный способ написать небольшую статью, а иногда и достаточно объемный текст, с несложным форматированием в виде курсива и толстого шрифта. Также Markdown неплох для написания статей с включением исходного кода. Но иногда хочется без потерь, танцев с бубном перегнать его в обычный, хорошо оформленный файл PDF, и чтобы не было проблем при конвертации, какие, например были у меня — нельзя писать по русски в комментариях исходного кода, слишком длинные строки не переносятся, а обрезаются и прочие мелкие проблемы. Инструкция позволит быстро настроить конвертер md2pdf не особенно вникая как это работает. Скрипт для более менее автоматической установки ниже в соотвествующем разделе.

Установка TexLive

Разумеется, можно установить только нужные части данного пакета. Но лично мне было откровенно лень искать минимально необходимую рабочую инсталляцию. Чтобы все точно работало, устанавливаем весь пакет TexLive. Он называется texlive-full и весит чуть больше 2х гигабайт, имейте данный факт в виду. Выполняем команду:

После достаточно долгой установки можно переходить к следующему пункту.

Установка конвертера Pandoc

Pandoc — пакет Linux, позволяющий преобразовывать некоторые текстовые форматы в другие. В нем много интересных возможностей, с которыми вы можете ознакомится самостоятельно в интернете. Нас же интересует только возможность преобразование markdown файла в PDF. Проверим установлен ли Pandoc и если нет, то установим его. Например так:

Если в выводе написано что не установлен — устанавливаем:

Установка MD2PDF

Можно проследовать на страницу скрипта на GitHub, и дальше действовать по инструкции.

Или скачать архив, распаковать в любую папку, открыть ее в терминале и опять таки следовать инструкциям.

Откройте терминал и выполните:

Затем выполните с правами суперпользователя, например:

Имейте в виду скрипт использует утилиту для построения консольных диалогов whiptail. Если она у вас не установлена, или ставить ее вы не желаете, или хотите все сделать сами, то установите texlive-full и pandoc вручную и действуйте по инструкции дальше.

Установка md2pdf для всех пользователей:

Установка md2pdf для текущего пользователя:

Использование md2pdf

Просто откройте папку с Markdown файлом (some_file.md) в Терминале, и выполните команду:

В результате в папке появится файл some_file.md.pdf.

Заключение

На базе описанного метода можно построить какой угодно стиль PDF файлов, также можно конвертировать вместо md другие форматы, любые поддерживаемые Pandoc. Смею надеятся что однажды это инструкция пригодится 3 с половиной людям.

Источник

Converting Markdown to Beautiful PDF with Pandoc

Contents

Over the past few years, I have been using some dedicated note-taking software to manage my notes. But all these tools I have tried are unsatisfactory: they are either slow or cumbersome when I want to search my notes. Finally, I decided to take my notes in Markdown and convert them to PDF using Pandoc for reading. In this post, I will summarize how I do it.

Taking our notes in Markdown has several advantages:

We can edit the Markdown files with our favorite editor, for example, Sublime Text, which means more efficient editing and pleasant writing experience.

Since a Markdown file is a textual file, we can search it using powerful

search tool such as grep or ripgrep .

We can covert the Markdown files to various formats such as PDF, HTML, epub, mobi etc., for better reading experience, with the help of Pandoc.

The notes are all text files and are small in size, which means easier and faster syncing or backup between your native PC and the cloud service you use.

Читайте также:  Как восстановление системы windows виста

In this post, I would like to share how to generate beautiful PDF files from Markdown and give solutions to the issues I have encountered during the process.

Prerequisite

Before we begin, you need to make sure that you have installed the following tools:

First, Pandoc. After installation, you should add the path of the Pandoc executable to the system PATH variable.

TeX distribution. Please make sure that TeX has been installed on your system. You can use either TeX Live or MiKTeX or MacTeX base on your platform. You may need to set up the PATH variable 1 .

A powerful text editor. One of my favorite is Sublime Text. You can also choose to use VS Code or even Neovim.

Generating PDF from Markdown with Pandoc

Background

For those who are not familiar with Pandoc, Pandoc is a powerful tool for converting document between different formats. It is called the swiss knife of document converter. There are actually two steps involved in converting Markdown files to PDF:

  1. Markdown files are converted to LaTeX source files.
  2. Pandoc invokes the pdflatex , xelatex or other TeX command and converts .tex source file to the final PDF file.

Because I often use non-ASCII characters in my files and my Markdown files use quotation, table and other complex format, I have met a few problems during the conversion process. In the following text, I will introduce how to solve these issues.

How to Handle Languages other than English

By default, Pandoc uses pdflatex command to generate PDF files, which can not handle Unicode characters well. You will encounter errors when you try to convert Markdown files containing Unicode characters to PDF files.

In order to handle Unicode characters, we need to use xelatex command instead. For the CJK languages, you need to use CJKmainfont option to give the proper font which supports the language you are using 2 . In this post, I will use the Chinese language as an example.

On Windows systems, for Pandoc version above 2.0, you can use the following command to generate the PDF file:

In the above command, KaiTi is the name of a font which supports the Chinese characters. How do we find a font supporting a particular language? First, you need to know the language code for the language you are using. For example, the language code for Chinese is zh . Then, use the fc-list command to look up the fonts which support this language 3 :

The output of command is like the following:

The font name is the string after the font location. Since the font names may contain spaces, you need to quote the font name when you want to use a particular font, e.g., -V CJKmainfont=»Source Han Serif CN» .

In Pandoc version 2.0, —pdf-engine option replaces the old —latex-engine option. On Linux systems where the Pandoc version may be old, the above command will not work. You need to use the following command instead 4 :

On Linux systems, the way to find the font supporting your language is the same as Windows system.

Issues and techniques

Add title, author and date info

Pandoc supports adding these info via its YAML header extension. We can easily add the document title, author and date info like this:

Block quote, table and list are not correctly rendered

The reason is that Pandoc requires that you leave an empty line before block quote, list and table environment. If the lines in the block quote are not correctly broken, i.e., all the lines are merged as one paragraph, you can add a space after each line to solve this issue.

Add highlight to block code

Pandoc supports block code syntax highlighting for many languages and offers several highlight themes. To list the highlight themes that Pandoc provides, use the following command:

To list all the languages that Pandoc supports, use the following command:

To use syntax highlighting for different languages, you need to specify the language in the block quote and use —highlight-style , e.g.

In the above command, we use the zenburn theme, I also recommend using the tango or breezedark theme.

Читайте также:  Hdaudbus sys для windows

Use numbered section and add TOC

By default, there is no table of contents (TOC) in the generated PDF and no numbers in the headers 5 . To add TOC, use the —toc option; to add section numbers, use the -N option. A complete example is as follows:

According to the Pandoc user guide, we can add colors to different links via the colorlinks option to separate the links from the normal texts:

colorlinks add color to link text; automatically enabled if any of linkcolor, filecolor, citecolor, urlcolor, or toccolor are set

To customize the color of different types of links, Pandoc offers different options:

linkcolor, filecolor, citecolor, urlcolor, toccolor color for internal links, external links, citation links, linked URLs, and links in table of contents, respectively: uses options allowed by xcolor, including the dvipsnames, svgnames, and x11names lists

For example, to set the URL color to NavyBlue and set the TOC color to Red , we can use the following command:

Note that the urlcolor option will not color the raw URL links in the text. To color those raw links, you can enclose those links with <> , e.g., .

Change the PDF margin

The default margin for the generated PDF is too large. According to the Pandoc FAQ, you can use the following option to change the margin:

The complete command is:

Error when using backslash inside Markdown

In ordinary Markdown format, it is fine to use backslash characters inside the files. But Pandoc interpret the backslash and string after it as LaTeX command by default. As a result, you may encounter weired errors when trying to compile Markdown files containing backslash characters. Based on discussions here and here, the solution is to make Pandoc treat the Markdown file as normal Markdown files and not interpret the LaTeX command. You need to use the following flag:

Or you can use two backslash to represent a literal backslash, e.g., \\sometxt . If you want to express a LaTeX command, enclose the command with inline code block, like this: \textt<> .

Add background color to inline code

In translating Markdown source file to TeX files, Pandoc use the \texttt command to represent the inline code. So inline code has no background color in the generated PDF files. To increase the readability of inline code, we can modify the \texttt command to add background color to text.

First, we need to create a file named head.tex and add the following settings to it:

When converting Markdown files, use the -H option to refer the head.tex file, e.g.

In the generated PDF, the inline code will have grey background color. You can change the background color as you wish.

Change the default style of block quote

By default, when converting Markdown to PDF, Pandoc use the quote environment for Markdown block quotes. The texts inside quotation are only indented, making it hard to recognize the environment.

We can create a custom environment to add background color and indentation to the quotation environment. Add the following setting to head.tex :

When you want to convert Markdown file to PDF, you can use the following command:

The produced PDF is like the following:

References

Put the settings to head.tex

You may have noticed the clumsiness if you try to customize a lot of settings. When converting Markdown to PDF, we often need to use several settings. If you specify all these options on the command line, it would be time consuming and cumbersome to edit. A good way is ease the issue is to put some command settings to head.tex file and refer to this file during Markdown file conversion.

For example, we can put the settings related to margin, inline code highlighting, and link color to head.tex :

Click to see the code.

Nested list level exceed the limit

One reader Karl Liu mentioned that if the nested list level exceeds 6, you will encounter the following error when trying to generate PDF file:

More detailed discussions can be found here. The solution proposed is to add the following settings in head.tex :

Читайте также:  Windows 10 пропал nod32

Click to see the code.

Add the -H head.tex option when compiling PDF files.

Add anchors in Markdown

I try to use anchors in Markdown following the discussion here. Unfortunately, in the generated PDF, the anchor does not work: when I click the linking text, there is no jump to the destination page.

Instead, we should use the attribute to give an id to the location we want to jump to and then refer to it in other places using the id. Here is an example:

How to resize image

We can also resize images using the attribute. You can specify width or height in absolute pixel values or as percentage relative to the page or column width. For example:

How to start a new page for each section

By default, when you generate PDF from Markdown files, each section started by the level 1 header do not start from the new page: it will continue from where the last section ends. If you want to start a new page when a new section starts, you need to add the following settings to head.tex according to this:

But when I tried to produce PDF with the updated head.tex files, I got an error:

According to discussions here, it is because Pandoc’s default LaTeX redefines the \pragraph command and we have to disable this behaviour. We need to use -V subparagraph when invoking the pandoc command:

Start a new page only after TOC

What if we only want to add a new page after the table of contents page? An easy way is to hack the \tableofcontents command. Add the following command to head.tex to redefine \tableofcontents command:

In the above command, we first save the old command and then redefine it to avoid recursive calls.

Line breaks

In Markdown, you can create a hard linebreak by appending two spaces after a line:

Using space at the line end for formating is annoying since it cause the trailing whitespace warning. The space characters are also not visible.

Pandoc also provides an escaped_line_breaks extension. You can use \ in the end of a line followed by newline character to represent a hard line break:

Images references

Pandoc supports LaTeX command inside Markdown, to refer to an image, you can use the LaTeX syntax:

Generate PDF using Sublime Text build system

It is cumbersome to switch to the terminal and use Pandoc to generate the PDF files and preview it after finishing writing the Markdown files. To simply the process, I use the Sublime Text build system for building PDF file and previewing. I use the light-weight Sumatra PDF reader for PDF previewing.

An example build system is shown below:

Click to see the code.

You can download the build system and head.tex file here.

Pandoc is not recognized on Windows systems

For some reasons unknown to me, when using the above build systems to compile Markdown files, I encountered the following errors:

‘pandoc’ is not recognized as an internal or external command, operable program or batch file.

After looking up the Sublime Text documentation, I find that we can add path in the build system. So I adjust the above build system:

Click to see the code.

After that, everything goes well.

Conclusion

In this post, I give a complete summary on how to generate beautiful PDF files from Markdown. I also share several solutions to the issues I have encountered. I hope that you can now generate beautiful PDF from Markdown files.

References

  • Dealing with Chinese in Pandoc
  • Pandoc’s handling of block quote
  • Pandoc syntax highlighting
  • colors provided by dvipsnames
  • Pandoc section number
  • Pandoc command not found
  • Anchors in Pandoc
    • https://github.com/jgm/pandoc/issues/1299
    • https://github.com/jgm/pandoc/issues/684
  • Resize image
  • start a new page after toc
  • Pandoc hard line break
    • https://stackoverflow.com/questions/48329455/pandoc-not-maintaining-newlines-from-txt-file-to-word-file
    • https://stackoverflow.com/questions/28283008/preserve-line-breaks-in-title-using-pandoc
  • Image reference in Pandoc
  1. Make sure that you can use latex command on the command line. ↩︎

    For other languages, you need to use —mainfont option. ↩︎

    For Windows system, you can use fc-list command after installing the TeX Live full edition. For Linux systems, this command is usually pre-installed. ↩︎

    Tested on Pandoc version 1.12.3.1. ↩︎

    Only the font size varies for different header levels. ↩︎

    Источник

Оцените статью