Skip to content

Instantly share code, notes, and snippets.

@justincbagley
Last active November 27, 2024 11:16
Show Gist options
  • Save justincbagley/ec0a6334cc86e854715e459349ab1446 to your computer and use it in GitHub Desktop.
Save justincbagley/ec0a6334cc86e854715e459349ab1446 to your computer and use it in GitHub Desktop.
How To Convert Markdown to PDF

How to convert markdown to PDF:

This post reviews several methods for converting a Markdown (.md) formatted file to PDF, from UNIX or Linux machines.

Using Pandoc:

$ pandoc How_I_got_svg-resizer_working_on_Mac_OSX.md -s -o test1.pdf

Other methods:

GRIP

http://superuser.com/questions/689056/how-can-i-convert-github-flavored-markdown-to-a-pdf I've had success using grip to display markdown in Chrome and then use Chrome's "Save as PDF" option in the Print dialog.

pip install grip  
grip your_markdown.md

grip will render the markdown on localhost:5000 ... - just edit away and refresh the browser. Print when ready.

This gave a more reliable representation than pandoc and was lighter weight than installing LaTeX (required by pandoc for pdf generation).

The print is not command line in this answer, but still found this easier/more reliable (looked 100% like Github for a long document including relatively linked images and code highlighting).

Node.js

http://superuser.com/questions/689056/how-can-i-convert-github-flavored-markdown-to-a-pdf You can also use Node.js based markdown-pdf

npm install -g markdown-pdf
markdown-pdf /path/to/markdown

NOTES:

The GRIP results look just like GitHub README pages. The Pandoc result looks like (is) LaTex format. And the Node.js result is the most original looking, but slightly harder to read than GRIP output PDF. Overall, I prefer GRIP output.

@Hexdump74
Copy link

Hi @justincbagley.
Here is the output on my archlinux :

❯ grip my.md 10000 & PID=$! ; sleep 1 ; wkhtmltopdf http://localhost:10000 my.pdf ; kill $PID; evince my.pdf&
[1] 1057828
 * Serving Flask app 'grip.app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://localhost:10000/ (Press CTRL+C to quit)
Loading page (1/2)
 * Error: could not retrieve styles: [Errno 20] Not a directory: '/home/manu/.grip/cache-4.5.2'
127.0.0.1 - - [23/Aug/2021 12:13:26] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2021 12:13:26] "GET /__/grip/static/octicons/octicons.css HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2021 12:13:26] "GET /img.png HTTP/1.1" 200 -
127.0.0.1 - - [23/Aug/2021 12:13:26] "GET /__/grip/static/octicons/octicons.woff2?ef21c39f0ca9b1b5116e5eb7ac5eabe6 HTTP/1.1" 200 -
Printing pages (2/2)
Done
[2] 1057867
[1]  - 1057828 terminated  grip my.md 10000

It works, but as you can see from my logs, the md is very simple. Could you try with this simple and small md (img.png is my github avatar) :

# Notes

* first point
* second point

![](./img.png)

@galxy25
Copy link

galxy25 commented Jan 25, 2022

if you get pandoc: pdflatex: createProcess: posix_spawnp: illegal operation (Inappropriate ioctl for device)

make sure you have basictex installed and reload your shell
jgm/pandoc#7570

@hgftitanium
Copy link

Check out:

pandoc README.md -o README.pdf "-fmarkdown-implicit_figures -o" --from=markdown -V geometry:margin=.4in --toc --highlight-style=espresso

More dependencies, but it makes a great ToC!

Thanks for a great hint, @mzpqnxow !

@YakirOren
Copy link

cool too for converting many file types to pdf
https://gotenberg.dev/

@yogithesymbian
Copy link

yogithesymbian commented Jun 25, 2023

there is no 100% looks like github ... and still bad view on pdf . for example

  • marks like this one
    • oe

@ajaykumar2017
Copy link

Please try this one using VS Code, it worked for me.

  • Install VS Code Extension Markdown Pdf
  • Now Open Command Pallete Ctrl+Shift+P
  • Then choose Markdown PDF: Export(pdf) and then pdf will be generated in the corresponding directory.

@tektutor
Copy link

tektutor commented Sep 1, 2023

When the README.md file is larger, it seems to convert upto 35 pages max, do you know any tricks to convert the entire README.md to PDF without any page limits?

@sskras
Copy link

sskras commented Sep 1, 2023

@tektutor: By "it" do you mean pandoc, grip or markdown-pdf (Node.js) tool?

@elvinagam
Copy link

Works perfectly. Thanks.

Install VS Code Extension Markdown Pdf

Now Open Command Pallete Ctrl+Shift+P

Then choose Markdown PDF: Export(pdf) and then pdf will be generated in the corresponding directory.

@niepiekm
Copy link

Please try this one using VS Code, it worked for me.

* Install VS Code Extension `Markdown Pdf`

* Now Open Command Pallete `Ctrl+Shift+P`

* Then choose `Markdown PDF: Export(pdf)` and then pdf will be generated in the corresponding directory.

Great tip! Thanks.

@PitGartmann
Copy link

PitGartmann commented Mar 20, 2024

Did you have any issues with the VsCode Extension Markdown PDF when using a GitHub remote Repository? I was using GitLab before that which worked perfectly, but now that I changed to GitHub it doesnt seem to work anymore. Im not getting specific errors, but only the message that the file destination cannot be found.

Managed to fix the Problem myself. It was due to one of my files not having the .md ending.

@hpssjellis
Copy link

Please try this one using VS Code, it worked for me.

  • Install VS Code Extension Markdown Pdf
  • Now Open Command Pallete Ctrl+Shift+P
  • Then choose Markdown PDF: Export(pdf) and then pdf will be generated in the corresponding directory.

Thank you @ajaykumar2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment