This Go project converts PDF files to Markdown format. The program extracts text and images from the PDF, saves the images in a local assets folder, and generates a Markdown file referencing the images. The CLI is built using the Cobra package, providing a structured and user-friendly command-line interface.
- Text Extraction: Extracts text from a PDF and saves it to a Markdown file.
- Image Extraction: Extracts images from a PDF and stores them in an
assetsfolder. - Image Referencing: References the extracted images in the generated Markdown file.
- Modular CLI: Utilizes Cobra for a scalable and maintainable command-line interface.
- Fully Free and Open-Source: Built using Go libraries and CLI tools.
-
Install Go:
-
Install the
pdfcpuCLI tool:go install github.com/pdfcpu/pdfcpu/cmd/pdfcpu@latest
Ensure the
pdfcpuexecutable is in your system'sPATH. -
Install Project Dependencies:
go get github.com/ledongthuc/pdf go get github.com/spf13/cobra@latest
-
Clone this Repository:
git clone https://github.com/sukeesh/markitdown-go.git cd markitdown-go -
Initialize the Go Module:
go mod tidy
-
Build the Program:
go build -o pdf2md main.go
The CLI tool pdf2md provides a convert subcommand to handle PDF to Markdown conversion. Below are detailed instructions on how to use the tool.
Convert a PDF file to Markdown with default settings:
./pdf2md convert <path/to/input.pdf>Example:
./pdf2md convert sample.pdfThis command will:
- Generate an
output.mdfile containing the extracted text and image references. - Save extracted images in the default
assets/directory.
Specify custom output Markdown file and assets directory using flags:
./pdf2md convert <path/to/input.pdf> -o <path/to/output.md> -a <path/to/assets/>Example:
./pdf2md convert sample.pdf -o output/sample.md -a output/assets/-
-o, --output: Specifies the output Markdown file.- Default:
output.md - Usage:
-o path/to/output.md
- Default:
-
-a, --assets: Specifies the directory to save extracted images.- Default:
assets - Usage:
-a path/to/assets/
- Default:
Get help on the CLI usage:
./pdf2md --helpGet help on the convert subcommand:
./pdf2md convert --help# Sample PDF Title
This is some sample text extracted from the PDF.

More text on the next line.
image_1.pngimage_2.jpg
markitdown-go/
├── cmd/
│ ├── convert.go # Defines the 'convert' subcommand
│ └── root.go # Defines the root command
├── pkg/
│ └── pdfconverter/
│ └── pdfconverter.go # Core logic for PDF to Markdown conversion
├── go.mod # Go module file
├── go.sum # Go checksum file
├── main.go # Entry point of the application
├── README.md # Project documentation
└── LICENSE # License information
-
cmd/: Contains Cobra command definitions.root.go: Defines the base command and global flags.convert.go: Implements theconvertsubcommand.
-
pkg/pdfconverter/: Encapsulates the core functionality for converting PDFs to Markdown.pdfconverter.go: Handles text and image extraction, and Markdown generation.
-
main.go: Executes the root command.
extractText: Extracts text from the PDF using thegithub.com/ledongthuc/pdflibrary.extractImages: Uses thepdfcpuCLI tool to extract images and saves them in the specifiedassetsdirectory.generateMarkdown: Combines the extracted text and image references into a Markdown file.- CLI Commands: Utilizes Cobra to handle command-line arguments and flags, providing a structured interface for users.
- ledongthuc/pdf: For text extraction.
- pdfcpu: For image extraction.
- Cobra: For building the CLI interface.
Contributions are welcome! Feel free to fork this repository, make changes, and submit a pull request. Whether it's improving documentation, adding new features, or fixing bugs, your contributions help make this project better.
-
Fork the Repository: Click the "Fork" button at the top-right of this repository's page.
-
Clone Your Fork:
git clone https://github.com/yourusername/markitdown-go.git cd markitdown-go -
Create a New Branch:
git checkout -b feature/your-feature-name
-
Make Your Changes: Implement your feature or fix.
-
Commit Your Changes:
git commit -m "Add feature: your feature description" -
Push to Your Fork:
git push origin feature/your-feature-name
-
Submit a Pull Request: Go to the original repository and submit a pull request from your fork.
This project is licensed under the MIT License. See the LICENSE file for details.