A Go-based command-line tool to convert PDF files to images (PNG/JPEG) with customizable translucent watermarks.
- PDF to Image Conversion: Convert PDF pages to high-quality PNG or JPEG images (300 DPI)
- Customizable Watermarks: Add translucent text watermarks with full customization
- System Font Support: Automatically detects and uses system fonts (Microsoft YaHei preferred on Windows)
- Cross-Platform: Works on Windows, macOS, and Linux
- Windows/MinGW Compatible: Uses WebAssembly-based PDF rendering (no CGO required)
- Tiled Watermarks: Diagonal watermark pattern covering entire page
git clone https://github.com/KawaiiSh1zuku/pdf2img.git
cd pdf2imgTurn to go-pdfium for further information.
- Go 1.21 or later
- pdfium library
pdf2img -i <input.pdf> [options]| Flag | Description | Default |
|---|---|---|
-i |
Input PDF file path (required) | - |
-o |
Output directory path | output |
-f |
Output format: png or jpg |
png |
-w |
Watermark text | CONFIDENTIAL |
-color |
Watermark RGB color (format: R,G,B) |
255,0,0 (red) |
-opacity |
Watermark opacity (0-100) | 30 |
-size |
Watermark font size | 48 |
-sx |
Horizontal spacing between watermarks | 200 |
-sy |
Vertical spacing between watermarks | 150 |
-r |
Watermark rotation angle (degrees) | 45 |
-font |
Path to custom TTF/TTC font file | auto-detect |
-dpi |
Rendering DPI | 300 |
# Convert PDF to PNG with default settings
pdf2img -i document.pdf
# Convert to JPEG
pdf2img -i document.pdf -f jpg
# Specify output directory
pdf2img -i document.pdf -o ./my_images# Custom watermark text
pdf2img -i document.pdf -w "DRAFT COPY"
# Blue watermarks with 50% opacity
pdf2img -i document.pdf -color "0,100,200" -opacity 50
# Dense horizontal watermarks
pdf2img -i document.pdf -r 0 -sx 150 -sy 100
# Large red watermarks
pdf2img -i document.pdf -size 72 -color "255,0,0" -opacity 40# Use specific font file
pdf2img -i document.pdf -font "/path/to/font.ttf" -w "Custom Font"
# Windows with Microsoft YaHei
pdf2img -i document.pdf -font "C:\Windows\Fonts\msyh.ttc"The program automatically searches for fonts in priority order:
C:\Windows\Fonts\msyh.ttc(Microsoft YaHei)C:\Windows\Fonts\msyh.ttf- Other CJK fonts (SimHei, SimSun, etc.)
- Western fonts (Arial, Segoe UI)
- User font directory
/Library/Fonts/System/Library/Fonts~/Library/Fonts
/usr/share/fonts/usr/local/share/fonts~/.fonts~/.local/share/fonts
Converted images are saved with the naming pattern:
<input_name>_page_1.png
<input_name>_page_2.png
<input_name>_page_3.png
...
- Uses go-pdfium with WebAssembly backend
- Uses CGO to lower memory usage
- Standard Go
imagepackage - Bilinear interpolation for rotated watermarks
- Supports PNG and JPEG output
If you see "Warning: no suitable system font found":
- Install Microsoft YaHei or another TrueType font
- Use
-fontflag to specify font path manually
- Ensure Microsoft YaHei is installed (Windows)
- Use
-fontto specify a CJK-compatible font - Verify the font file exists and is readable
# Ensure Go modules are enabled
go mod tidy
# Build for Windows (cross-compile from any OS)
GOOS=windows GOARCH=amd64 go build -o pdf2img.exe .Apache License 2.0
- PDF rendering: go-pdfium by Klippa
- Font parsing: golang.org/x/image/font