Skip to content

Instantly share code, notes, and snippets.

@kingishb
Created August 10, 2018 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingishb/04fadfa0ff268e26b27a266745ef8a92 to your computer and use it in GitHub Desktop.
Save kingishb/04fadfa0ff268e26b27a266745ef8a92 to your computer and use it in GitHub Desktop.
generate nicely formatted pdfs from markdown
// Setup:
// Install pandoc, wkhtmltopdf, and clone https://github.com/otsaloma/markdown-css
// into ~/.local. Build this and put it somewhere in your path.
// Usage:
// $ mpdf README.md
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)
func main() {
if len(os.Args) < 2 {
log.Fatal("Please provide markdown file.")
}
infile := os.Args[1]
n := strings.Split(infile, ".")[0]
html := fmt.Sprintf("/tmp/%s.html", n)
pdf := fmt.Sprintf("%s.pdf", n)
pandoc := fmt.Sprintf("pandoc %s --output %s --css ~/.local/markdown-css/github.css --highlight-style=haddock --self-contained",
infile, html)
wkpdf := fmt.Sprintf("wkhtmltopdf %s %s", html, pdf)
_, err := exec.Command("sh", "-c", pandoc).Output()
if err != nil {
log.Fatal(err)
}
_, err = exec.Command("sh", "-c", wkpdf).Output()
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment