* implement gopdfEngine for direct PDF rendering * create HTML rendering function for markdown * add tests for engine functionality * remove main.go as entry point
4.8 KiB
go-mdtopdf-helper
A command-line tool that automatically converts Markdown files to PDF. Perfect for maintaining PDF documentation alongside your Markdown files in Git repositories.
Features
- Converts Markdown files to PDF, HTML or DOCX (one or several at once)
- Choice of PDF rendering engines
- Works out of the box: the default engine is pure Go with no external dependencies
- Can run as a Git pre-commit hook
- Recursive directory scanning
- Parallel file processing
- Cross-platform support (Windows, Linux, macOS)
Output Formats
Select formats with -format, for example -format pdf,html,docx. Each file is written next to its Markdown source with the matching extension (README.md → README.pdf, README.html, README.docx).
- pdf: rendered by the selected engine (see below).
- html: a standalone page with embedded CSS.
- docx: a Word document generated in pure Go (no Word or other tools needed). Supports headings, paragraphs, bold/italic/code, links, bulleted and numbered lists, code blocks, quotes, rules and tables. Images are written as
[image: alt]text.
In pre-commit hook mode every generated file is staged.
Engines
The engine only applies to PDF output.
Select an engine with -engine:
| Engine | Needs | Notes |
|---|---|---|
gopdf (default) |
nothing | Pure Go. Supports headings, paragraphs, bold/italic/code, links, lists, code blocks, quotes, rules and tables. Images are shown as [image: alt]. |
chromedp |
Chrome or Chromium | Best rendering (full HTML/CSS). Runs headless Chrome; the sandbox is disabled automatically when running as root, as in most CI containers. |
wkhtmltopdf |
wkhtmltopdf | Legacy engine. Looked up in the standard install locations (Windows: C:\Program Files\wkhtmltopdf\bin, Linux/macOS: /usr/local/bin, /usr/bin, Homebrew). |
Installation
go get github.com/Warky-Devs/go-mdtopdf-helper
Usage
Basic Usage
Convert Markdown files in the current directory:
go-mdtopdf-helper
Command Line Options
go-mdtopdf-helper [options]
Options:
-dir string
Directory to scan for markdown files (default ".")
-recursive
Scan directories recursively (default true)
-parallel
Convert files in parallel (default true)
-hook
Run as git pre-commit hook
-engine string
PDF engine: gopdf, wkhtmltopdf, chromedp (default "gopdf")
-format string
Output formats, comma separated: pdf, html, docx (default "pdf")
Git Pre-commit Hook
To use as a Git pre-commit hook:
- Create a file named
pre-commitin your repository's.git/hooks/directory - Add the following content:
#!/bin/sh
go-mdtopdf-helper -hook
- Make the hook executable:
chmod +x .git/hooks/pre-commit
When enabled as a pre-commit hook, the tool will:
- Detect staged Markdown files
- Ask for confirmation before conversion
- Convert files to PDF
- Automatically stage the generated PDFs
PDF Output Configuration
Generated PDFs are A4 with 15mm margins on all sides. The chromedp and wkhtmltopdf engines render the full Markdown-to-HTML output, so they support everything the Markdown parser's common extensions produce.
Project Layout
cmd/go-mdtopdf-helper/ CLI entry point (flags, hook mode)
internal/converter/ File discovery, parallel conversion, git hook
internal/document/ HTML and DOCX writers
internal/engine/ Engine interface and the gopdf, wkhtmltopdf and chromedp backends
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. Here's how you can contribute:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Development Setup
- Clone the repository
- Install dependencies:
go mod download - Make your changes
- Run tests:
make test
make also provides build, lint (golangci-lint), format (gofmt) and all. Tests for the chromedp and wkhtmltopdf engines are skipped when the tool is not installed.
Dependencies
- gopdf - Pure Go PDF generation
- chromedp - Headless Chrome driver
- go-wkhtmltopdf - Go wrapper for wkhtmltopdf
- gomarkdown - Markdown parser and HTML renderer
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Thanks to SebastiaanKlippert for the go-wkhtmltopdf library
- Thanks to the gomarkdown team for their Markdown parser