A pure Go CLI tool for converting between Markdown and various formats without external dependencies.
β¨ Pure Go - No CGO, no external system dependencies (wkhtmltopdf, Pandoc, etc.)
π Multiple Conversions:
- PDF β Markdown: Extract text and structure from PDF files
- HTML β Markdown: Convert HTML files or strings to clean Markdown
- Web β Markdown: Fetch URLs with readability mode and convert to Markdown
- Markdown β PDF: Generate PDF documents from Markdown
# Clone the repository
git clone https://github.com/green-creeper/mdtool.git
cd mdtool
# Download dependencies
go mod download
# Build the binary
go build -o mdtool main.go
# Optional: Install globally
go install# Convert a file
mdtool html2md input.html output.md
# From stdin to stdout
cat input.html | mdtool html2md > output.md
# From file to stdout
mdtool html2md input.html# Fetch and convert a web page
mdtool web2md https://example.com/article output.md
# Output to stdout
mdtool web2md https://example.com/articleThe web2md command uses readability to extract the main content, removing navigation, ads, and other boilerplate.
# Convert PDF to Markdown
mdtool pdf2md document.pdf output.md
# Output to stdout
mdtool pdf2md document.pdf# Convert Markdown to PDF
mdtool md2pdf input.md output.pdf
# Auto-generate output filename (input.md.pdf)
mdtool md2pdf input.mdmdtool/
βββ main.go # Entry point
βββ go.mod # Dependencies
βββ cmd/
β βββ mdtool/ # CLI commands
β βββ root.go # Root command
β βββ html2md.go # HTML β MD command
β βββ web2md.go # Web β MD command
β βββ pdf2md.go # PDF β MD command
β βββ md2pdf.go # MD β PDF command
βββ internal/
β βββ converter/ # Format converters
β β βββ converter.go # Converter interface
β β βββ html2md.go # HTML converter
β β βββ pdf2md.go # PDF extractor
β β βββ md2pdf.go # PDF generator
β βββ scraper/ # Web scraping
β βββ web2md.go # Web fetcher + converter
βββ pkg/
βββ models/ # Data models
βββ models.go # Request/Response types
All converters implement a common interface:
type Converter interface {
Convert(req *ConvertRequest) *ConvertResponse
Name() string
SupportedFormats() (source, target string)
}This allows easy extension for new formats.
Each conversion is treated as a provider with its own implementation:
- HTML2MDConverter: Uses
JohannesKaufmann/html-to-markdown - Web2MDConverter: Combines HTTP client +
go-readability+html-to-markdown - PDF2MDConverter: Uses
ledongthuc/pdffor text extraction - MD2PDFConverter: Uses
go-pdf/fpdfwith embedded DejaVu fonts for full Unicode support
All dependencies are pure Go libraries:
| Library | Purpose | License |
|---|---|---|
| JohannesKaufmann/html-to-markdown | HTML to MD conversion | MIT |
| go-shiori/go-readability | Readability extraction | MIT |
| ledongthuc/pdf | PDF text extraction | MIT |
| yuin/goldmark | Markdown parsing (AST) | MIT |
| go-pdf/fpdf | PDF generation | MIT |
| spf13/cobra | CLI framework | Apache 2.0 |
| PuerkitoBio/goquery | HTML parsing | BSD-3 |
| DejaVu Fonts | Embedded Unicode fonts | Bitstream Vera |
mdtool web2md https://blog.golang.org/go1.18 go1.18.md# Create a markdown report
cat > report.md << 'EOF'
# Monthly Report
## Summary
This month we achieved the following goals...
## Metrics
- 100% uptime
- 50% faster response times
---
*Generated on 2024-01-15*
EOF
# Convert to PDF
mdtool md2pdf report.md monthly-report.pdf# Fetch web page, convert to MD, then to PDF
mdtool web2md https://example.com/article article.md
mdtool md2pdf article.md article.pdfTo add a new converter:
- Create a new file in
internal/converter/ - Implement the
Converterinterface - Add a new command in
cmd/mdtool/ - Register the command in
root.go
Example stub:
type DocxToMDConverter struct{}
func (c *DocxToMDConverter) Convert(req *models.ConvertRequest) *models.ConvertResponse {
// Implementation here
}
func (c *DocxToMDConverter) Name() string {
return "DOCX to Markdown Converter"
}
func (c *DocxToMDConverter) SupportedFormats() (string, string) {
return "docx", "markdown"
}- Text-based PDFs only: Cannot extract text from scanned/image-based PDFs
- Basic formatting: Complex layouts may not be preserved
- No images: Text extraction only
- Tables: Renders GFM-style tables with borders
- Code blocks: Renders fenced code blocks with monospace font (great for file trees)
- Lists: Supports ordered and unordered lists with nesting
- Font limitations: Uses Arial for text, Courier for code
- No images: Image embedding is not yet supported
- JavaScript-rendered content: Cannot fetch content that requires JavaScript execution
- Dynamic pages: Works best with static content
Contributions are welcome! Areas for improvement:
- Add image support in MD β PDF
- Improve PDF text extraction (handle more complex layouts)
- Add DOCX/ODT support
- Add image extraction from PDFs
- Support for custom fonts and styling
This project uses google/go-licenses to ensure compliance with dependency licenses.
To generate a report of all licenses used:
go install github.com/google/go-licenses@latest
go-licenses report ./... > licenses.csvMIT License - feel free to use and modify as needed.
Built with β€οΈ using pure Go libraries.