How to convert a Markdown file to PDF
Asked Answered
B

13

212

I have a Markdown file that I wish to convert to PDF so that I can upload it on Speakerdeck. I am using Pandoc to convert from markdown to PDF.

My problem is I can't specify what content should go on what page of the PDF, because Markdown doesn't provide any feature like that.

E.g., Markdown:

###Hello
* abc
* def

###Bye
* ghi
* jkl

Now I want Hello to be one slide and Bye to be on another slide on Speakerdeck. So, I will need them to be on different pages in the PDF that I generate using Pandoc.

But both Hello and Bye gets on the same page in the PDF.

How can I accomplish this?

Balthazar answered 13/7, 2013 at 13:7 Comment(2)
try using gimli ruby gem. its quite easy, for reference see blog.kushdilip.com/2014/02/…Egghead
For future readers, they may want to check out github.com/alanshaw/markdown-pdfShip
F
237

Via the terminal (tested in 2020)

Download dependencies

sudo apt-get install pandoc texlive-latex-base texlive-fonts-recommended texlive-extra-utils texlive-latex-extra

Try to use

pandoc MANUAL.txt -o example13.pdf
pandoc MANUAL.md -o example13.pdf

Via a Visual Studio Code extension (tested in 2020)

  • Download the Yzane Markdown PDF extension
  • Right click inside a Markdown file (md)
  • The content below will appear
  • Select the Markdown PDF: Export (pdf) option

Visual Studio Code Markdown PDF

Fivepenny answered 2/4, 2019 at 22:0 Comment(6)
This answer also fixed an issue I was having on my side: ! LaTeX Error: File `booktabs.sty' not found.Innkeeper
In a debian:10 docker image, this tries to install 1GB of dependencies, all that just to convert a few kilobytes of Markdown into PDF. I wonder if there is a solution that involves a bit less overhead.Orient
Note: For me the second solution only worked after restarting VSCodeCrellen
The Linux OS doesn't support emojis well (Windows is better) -- I doubt it's the duty of operating systems to support emojis. I guess it's about applications.Extradition
The answer rocks! it works well with images and code snippets unlike the other alternatives.Dun
Installation on Windows can be done by executing the choco install rsvg-convert python miktex and the choco install pandoc commands. Reference: pandoc.org/installing.html#windowsIntendancy
S
77

2016 update:

NPM module: https://github.com/alanshaw/markdown-pdf

Has a command line interface: https://github.com/alanshaw/markdown-pdf#usage

npm install -g markdown-pdf
markdown-pdf <markdown-file-path>

Or, an online service: http://markdown2pdf.com

Southwards answered 22/6, 2016 at 20:7 Comment(8)
using the online service, I keep getting Error generating PDF.Leahy
The online service seems broken now.Redhanded
@ÁrpádMagosányi well, I use it on Ubuntu. be specific what doesn't work.Southwards
Works on Ubuntu 19.04 command line. A few event related warnings, but produces a correct pdf.Hauptmann
I tested it, tables don't seem to have separating lines in the output PDFSubfloor
If you don't want to install the tool, you can do it with npx: npx markdown-pdf <mardkown-file-path>Tyre
unfortunately markdown-pdf seems to be dead and doesn't work either on current Ubuntu nor on current Debian systems as of 2021-09-01, see hereUnkind
Now the website is back online, but the output is not perfect: in my test the list markers were not in the same line as the items and "italic" between quotes wasn't recognized.Emarginate
C
45

As SpeakerDeck only accepts PDF files, the easiest option is to use the Latex Beamer backend for pandoc:

pandoc -t beamer -o output.pdf yourInput.mkd

Note that you should have LaTeX Beamer installed for that.

In Ubuntu, you can do sudo apt-get install texlive-latex-recommended to install it. If you use Windows, you may try this answer.

You may also want to try the HTML/CSS output from Slidy:

pandoc --self-contained -t slidy -o output-slidy.html yourInput.mkd

It has a decent printing output, as you can check out trying to print the original.

Read more about slideshows with pandoc here.

Candlestand answered 13/7, 2013 at 14:31 Comment(5)
pandoc -t beamer -o output.pdf yourInput.mkd produces only one pdf page. Extra contents are cut.Narcisanarcissism
@Narcisanarcissism Same issue here. Any hints?Gujarati
@rkioji Try the tip from the other answer, add *** where you want to break a page.Candlestand
To install the dependencies on Fedora: sudo dnf install -y pandoc texlive-beamer texlive-ec Suk
Last link johnmacfarlane.net is dead. The domain probably does not exist anymore.Pterosaur
F
21

Easy online solution: dillinger.io.

Just paste your Markdown content into the editor on the left and see the (html) preview on the right. Then click Export as on the top and chose pdf.

It's based on the open source dillinger editor.

Flutist answered 17/9, 2020 at 10:35 Comment(2)
pandoc errored out on me, because of "too nested" lists (was trying to print an outliner export), dillinger worked, thanks!Pandiculation
dillinger.io renders HTML tags as text in tables, eg. b<sub>7</sub> | b<sub>6</sub> | etc... does not display subscript in the HTML preview, but funnily enough the PDF output is correct, so thanks from me also!Recuperator
A
7

Adding to elias' answer, if you want to separate text in slides, just put *** between the text you want to separate. For your example to be in several pages, write it like this:

### Hello
- abc
- def

***

### Bye
- ghi
- jkl

And then use elias' answer, pandoc -t beamer -o output.pdf yourInput.md.

I have Ubuntu 18.10 (Cosmic Cuttlefish) and installed the full package from texlive. It works for me.

Aperiodic answered 12/1, 2019 at 12:50 Comment(0)
F
7

I found that many markdown-to-pdf converters produce files that I don't find exactly neat-looking. However there is a solution to this.

If you're using IntelliJ, you can use a plugin called "Markdown". The export function uses pandoc as an engine so you probably will need to install that along with pdf-latex. https://pandoc.org/installing.html

In IntelliJ, under Tools > Markdown Converter > Export Markdown File To...

And there you go, a clean looking document. Additional styling can be added via a .css stylesheet.

Feriga answered 22/12, 2022 at 7:33 Comment(0)
G
6

Previously I had used the npm markdown-pdf answer. However, on a fresh install of Ubuntu 19.04 (Disco Dingo) I had issues getting it to install correctly.

Instead I started using the Visual Studio Code package: "Markdown PDF"

Details:
Name: Markdown PDF
Id: yzane.markdown-pdf
Description: Convert Markdown to PDF
Version: 1.2.0
Publisher: yzane
Visual Studio Marketplace link: https://marketplace.visualstudio.com/items?itemName=yzane.markdown-pdf

It has worked consistently well. If you've had issues getting other answers to work, I would recommend trying this.

Gongorism answered 2/6, 2019 at 6:52 Comment(1)
I've just done this on both Windows and Linux. Installation was a breeze in both cases, and worked well without any configuration or setup.Brunswick
D
4

I've managed to get a stable Markdown -> HTML > PDF pipeline working with the MarkReport project.

It is a bit more than what Pandoc will do though, since it is based on WeasyPrint and is therefore aimed for clean report publishing, with cover, headers, sections, ... It also enriches the HTML with syntax highlighting and LaTeX equations.

Delgado answered 2/3, 2019 at 22:55 Comment(1)
Minor note: pandoc can export via WeasyPrint, too. (Useful, e.g., when targeting HTML from a document containing citations).Involve
Q
3

I suggest using mdtopdf. It supports syntax highlighting, paging and dark and light themes.

Install md2pdf by obtaining the release for your arch and OS or, if you have go installed, invoke:

$ go install github.com/mandolyte/mdtopdf/cmd/md2pdf@latest

If you require syntax highlighting, download the gohighlight lexers

md2pdf supports all major markdown features and accepts local files, remote HTTP(s) URL and STDIN inputs. The below command will convert a markdown file to a PDF with a dark theme, syntax highlighting (you'll need to provide the language hint, of course), page/slide separation and a footer:

md2pdf -i https://github.com/jessp01/crash-course-in/raw/main/courses/apt_dpkg_deb/apt_dpkg_deb.md \
    -o apt_dpkg_deb.pdf \
    -s ~/.config/zaje/syntax_files \
    --theme dark \
    --new-page-on-hr \
    --with-footer \
    --author "Jesse Portnoy <[email protected]>" \
    --title "A crash course on handling deb packages"
Questor answered 8/9, 2023 at 16:14 Comment(0)
P
2

With python script:

pip install markdown-pdf

from markdown_pdf import Section, MarkdownPdf

pdf = MarkdownPdf()
# each section starts from new page in pdf file
pdf.add_section(Section("###Hello\n* abc\n* def\n"))  # page 1
pdf.add_section(Section("###Bye\n* ghi\n* jkl\n"))  # page 2
pdf.save("out.pdf")
Phago answered 2/10, 2023 at 16:43 Comment(0)
C
1

Simple way with iOS:

Use Shortcuts app (by Apple)

Make Rich Text From Markdown: Clipboard

^

Make PDF from Rich Text From Markdown

^

Show [PDF] in Quick Look

Just copy text and run the shortcut. Press share in Quick Look (bottom left) to store or send it. I use this to quickly convert Joplin notes to pdf.

Conundrum answered 5/2, 2022 at 3:10 Comment(0)
A
1

Using Okular

  1. Make sure your file has the ".md" extension.
  2. Open in Okular
  3. File -> Export As -> PDF -> Select save location and save.

Using discount

To go the Markdown -> HTML -> PDF path, you can use the discount package directly (Okular uses it)

  1. Say your file is named "filename.md". Use markdown filename.md > outfile.html to get the html into an html file "outfile.html".
  2. Use your preferred method of converting HTML to PDF.
  • discount also has other binaries: theme and makepage - which you can check out for their specific features.
Alby answered 17/1, 2024 at 21:5 Comment(0)
S
0

Use the python md2pdf library with weasyprint.

pip install md2pdf weasyprint

Then convert your file

md2pdf inputfile.md outputfile.pdf

Want more control over formatting? Grab some markdown css from here: https://github.com/sindresorhus/github-markdown-css

Then use:

md2pdf --css markdown.css inputfile.md outputfile.pdf
Supernatant answered 13/3, 2024 at 1:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.