feat(engine): add Chromedp and Wkhtmltopdf engines for PDF conversion

* implement gopdfEngine for direct PDF rendering
* create HTML rendering function for markdown
* add tests for engine functionality
* remove main.go as entry point
This commit is contained in:
Hein committed 2026-10-08 16:42:47 +02:00
1 parent 73f258aa95
commit 50b7246c21
23 files changed
+1948 -393

No files matched your search

+41 -15
View File
@@ -1,25 +1,38 @@
# go-mdtopdf-helper
A command-line tool that automatically converts Markdown files to PDF using wkhtmltopdf. Perfect for maintaining PDF documentation alongside your Markdown files in Git repositories.
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 high-quality PDFs
- 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)
- Automatic detection of wkhtmltopdf installation
## Prerequisites
## Output Formats
This tool requires [wkhtmltopdf](https://wkhtmltopdf.org/) to be installed on your system. The application will check for its presence in standard installation locations:
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`).
- Windows: `C:\Program Files\wkhtmltopdf\bin`
- Linux: `/usr/local/bin/wkhtmltopdf` or `/usr/bin/wkhtmltopdf`
- macOS: `/usr/local/bin/wkhtmltopdf` or via Homebrew
- **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.
If wkhtmltopdf is not found, you will be prompted to install it.
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](https://wkhtmltopdf.org/) | Legacy engine. Looked up in the standard install locations (Windows: `C:\Program Files\wkhtmltopdf\bin`, Linux/macOS: `/usr/local/bin`, `/usr/bin`, Homebrew). |
## Installation
@@ -51,6 +64,10 @@ Options:
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
@@ -79,11 +96,16 @@ When enabled as a pre-commit hook, the tool will:
## PDF Output Configuration
The generated PDFs are configured with:
- 300 DPI resolution
- 15mm margins on all sides
- Local file access enabled for images
- Support for common Markdown extensions
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
@@ -105,11 +127,15 @@ Contributions are welcome! Please feel free to submit a Pull Request. Here's how
3. Make your changes
4. Run tests:
```bash
go test ./...
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](https://github.com/signintech/gopdf) - Pure Go PDF generation
- [chromedp](https://github.com/chromedp/chromedp) - Headless Chrome driver
- [go-wkhtmltopdf](https://github.com/SebastiaanKlippert/go-wkhtmltopdf) - Go wrapper for wkhtmltopdf
- [gomarkdown](https://github.com/gomarkdown/markdown) - Markdown parser and HTML renderer