Created
August 10, 2018 04:40
-
-
Save kingishb/04fadfa0ff268e26b27a266745ef8a92 to your computer and use it in GitHub Desktop.
generate nicely formatted pdfs from markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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