pdftoppm仅转换PDF首页求助:指定页码仍输出首页
Let's break down why your pdftoppm command keeps outputting the first page instead of the specified pages, and fix it step by step:
1. Fix your output redirection syntax
The most common mistake here is how you're handling output. By default, pdftoppm generates separate numbered files (like input-5.pgm for page 5) instead of sending image data directly to stdout. When you use > output.pgm, you're actually capturing the command's empty standard output (not the image) instead of the file you want.
To correctly redirect a single page to your target file, add - as the output filename (this tells pdftoppm to send the image to stdout):
pdftoppm -f 5 -l 5 -gray input.pdf - > output.pgm
For a range of pages, skip the redirection if you want individual files (PGM doesn't support multi-page natively anyway):
pdftoppm -f 5 -l 10 -gray input.pdf output_prefix # This creates output_prefix-5.pgm, output_prefix-6.pgm, ..., output_prefix-10.pgm
2. Check if displayed page numbers match internal indices
Sometimes a PDF’s visible page numbers (like i, ii, 1, 2) don’t align with its internal page indices (which start at 1 for the first physical page). For example, a 4-page Roman-numeral preface would make the displayed page 5 correspond to internal page 9.
Use pdftk to check the mapping between displayed and internal pages:
pdftk input.pdf dump_data | grep -A2 PageLabel
Adjust your -f and -l values to match the internal page numbers if needed.
3. Test with a clean PDF and update poppler
- Try your command on a known-good multi-page PDF (like a standard sample document). If it works there, your original PDF might be corrupted or have unusual page structure.
- Outdated
pdftoppm(part of the poppler-utils package) can have bugs. Update it:- Debian/Ubuntu:
sudo apt update && sudo apt install --only-upgrade poppler-utils - macOS (Homebrew):
brew upgrade poppler
- Debian/Ubuntu:
4. Clear old cached files
If you’re still seeing the first page in output.pgm, it might be a leftover from a previous run. Delete the file first, then re-run the corrected command to ensure you get a fresh output.
内容的提问来源于stack exchange,提问作者Kalpit

