Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

@georgegu1997

This comment has been minimized.

Copy link

@georgegu1997 georgegu1997 commented Jan 25, 2018

If you have Python 2 and 3 at the same time as I do, you may need to use "python3 -m grip .\filename.md" to solve the problem mentioned in pallets/flask#2543

@mzpqnxow

This comment has been minimized.

Copy link

@mzpqnxow mzpqnxow commented Feb 25, 2019

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!

@wisnubaldas

This comment has been minimized.

Copy link

@wisnubaldas wisnubaldas commented Dec 2, 2019

can i convert pdf to markdown ?

@justincbagley

This comment has been minimized.

Copy link
Owner Author

@justincbagley justincbagley commented Dec 9, 2019

Hi @wisnubaldas, you've probably figured this out by now, but you can use Pandoc. There are also some free online converters that I've played with and worked quite well, including https://pdf2md.morethan.io. And there is also a Python program, PDF to Markdown, which you can install with pip.

@justincbagley

This comment has been minimized.

Copy link
Owner Author

@justincbagley justincbagley commented Dec 9, 2019

Thanks @georgegu1997 and @mzpqnxow for your input here!

@brilliant-ember

This comment has been minimized.

Copy link

@brilliant-ember brilliant-ember commented Oct 31, 2020

I get this error when I try to use it, I tried all sorts of combinations,with quotes and without, with the ./ and without, with .md at the end and without

$ markdown-pdf ./a1.md 


internal/validators.js:125
    throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "file" argument must be of type string. Received type object
    at validateString (internal/validators.js:125:11)
    at normalizeSpawnArguments (child_process.js:411:3)
    at spawn (child_process.js:545:16)
    at Object.execFile (child_process.js:221:15)
    at WriteStream.<anonymous> (/usr/local/lib/node_modules/markdown-pdf/index.js:117:22)
    at WriteStream.emit (events.js:203:15)
    at finishMaybe (_stream_writable.js:646:14)
    at stream._final (_stream_writable.js:624:5)
    at WriteStream._final (internal/fs/streams.js:268:3)
    at callFinal (_stream_writable.js:617:10)


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.