6

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/

Al2110
  • 185

5 Answers5

6

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.

AndyAnd
  • 76
4

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.

phseiff
  • 41
  • 3
0

I went for the Markdown PDF plugin in Code OSS (VS Code), where I write in Markdown anyway.

progonkpa
  • 271
0

I tried @AndyAnd answer (with grid) but image were not working (not displayed in PDF). I had to export it first to HTML (with grid) and then to PDF (with Pandoc & latex) to have a PDF with pictures. And still, the layout was not respected.

So I found a solution, but it's only for people that have the same use case that mine : As a developer, I write my tool's documentation in Markdown, but sometimes I need to export it, so users can read it. In IntelliJ (and all its derivatives IDE), according to its documentation, you need to click on "Tools | Markdown Converter | Export Markdown File To", and then you can choose MS Word, HTML and PDF format.

The PDF format works like a charm, with pictures and layouts !

Hope it can help :D

0

Since you're using a Windows OS I can suggest you to use the "Markdown PDF" extension in Visual Studio Code.

It works pretty good and you can output files in .pdf, .html, .png and .jpeg.

Images from external sources are also working well.

cheers

Livio
  • 1