Converting .md to .pdf, with images

7,254

Solution 1

Take a look at How Can I Convert Github-Flavored Markdown To A PDF which includes creating "...a long document including relatively linked images and code highlighting" as a PDF.

Solution 2

You can use gh-md-to-html for this, which is a command line tool that can also convert markdown to pdf (Full disclosure: I'm the author).

You can install it by installing wkhtmltopdf and then installing gh-md-to-html with

pip3 install gh-md-to-html[pdf_export]

And then use

gh-md-to-html path_to_your_file.md -p <name>.pdf 

The -p option declares under which file name to save the resulting pdf file; the "<name>" is automatically replaced with the name of your input file.

Under to hood, gh-md-to-html converts the file to html (as the name suggests) and then from html to pdf using wkhtmltopdf.

The resulting pdf file is, in any case, styled similar to how GitHub styles their README files; if you want to disable that, you can supply the option -s false to the command, which disables the default styling.

The conversion process is done partially online (using GitHub's markdown REST API); in case you don't want that, you can use pip3 install gh-md-to-html[offline_conversion] and then run gh-md-to-html with the -o OFFLINE option.

gh-md-to-html can handle a variety of different input sources (repositories, weblinks, local files, strings) and resolves image addresses dependent on the input source, so there really shouldn't be any cases of generated pdf files missing images with it. There is a table in the project's README that dives further into this, though, if you need some additional documentation.

Share:
7,254
Al2110
Author by

Al2110

Updated on September 18, 2022

Comments

  • Al2110
    Al2110 over 1 year

    How to convert a markdown file to pdf, if it contains images? There are many tools available that perform .md to .pdf conversion, but if there are any images in the markdown, they are not in the final pdf, instead the alt text is displayed.

    For example, the following is how the .md previews in Visual Studio Code: enter image description here

    This is how the above looks in the pdf: enter image description here

    This was tried with the following: https://www.markdowntopdf.com/