Skip to content

KawaiiSh1zuku/pdf2img

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PDF2IMG

A Go-based command-line tool to convert PDF files to images (PNG/JPEG) with customizable translucent watermarks.

Features

  • 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

Installation

From Source

git clone https://github.com/KawaiiSh1zuku/pdf2img.git
cd pdf2img

Turn to go-pdfium for further information.

Requirements

Usage

pdf2img -i <input.pdf> [options]

Flags

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

Examples

Basic Usage

# 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

Watermark Customization

# 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

Custom Font

# 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"

Font Detection

The program automatically searches for fonts in priority order:

Windows

  1. C:\Windows\Fonts\msyh.ttc (Microsoft YaHei)
  2. C:\Windows\Fonts\msyh.ttf
  3. Other CJK fonts (SimHei, SimSun, etc.)
  4. Western fonts (Arial, Segoe UI)
  5. User font directory

macOS

  • /Library/Fonts
  • /System/Library/Fonts
  • ~/Library/Fonts

Linux

  • /usr/share/fonts
  • /usr/local/share/fonts
  • ~/.fonts
  • ~/.local/share/fonts

Output

Converted images are saved with the naming pattern:

<input_name>_page_1.png
<input_name>_page_2.png
<input_name>_page_3.png
...

Technical Details

PDF Rendering

  • Uses go-pdfium with WebAssembly backend
  • Uses CGO to lower memory usage

Image Processing

  • Standard Go image package
  • Bilinear interpolation for rotated watermarks
  • Supports PNG and JPEG output

Troubleshooting

Font Not Found

If you see "Warning: no suitable system font found":

  1. Install Microsoft YaHei or another TrueType font
  2. Use -font flag to specify font path manually

Chinese Characters Not Displaying

  1. Ensure Microsoft YaHei is installed (Windows)
  2. Use -font to specify a CJK-compatible font
  3. Verify the font file exists and is readable

Build Issues

# 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 .

License

Apache License 2.0

Credits

About

A Go-based command-line tool to convert PDF files to images (PNG/JPEG) with customizable translucent watermarks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors