↩ 🔵 🔵 🔵


Encounters

၊၊||၊|။

pymupdf ⚬ᵖʸ|_encounter|ENC|20261017135122-00-•

Splitting on pages is a much more robust boundary. It also fixes the missing-spaces problem from the cropping attempt: a page split copies each page’s content stream unchanged, so text extracted from the split file should match the original exactly.

The file-size problem has the same cause as before. A single-page file still carries the fonts embedded for the whole document, even though one page uses only a fraction of their glyphs. With several fonts in a textbook, that can make a one-page file 100–300 KB when it really needs about 20 KB. Getting files small means re-subsetting the fonts per page, not just splitting.

My recommendation: PyMuPDF, because it can split and re-subset in one pass:

import pymupdf💜  # pip install pymupdf
 
src = pymupdf.open("book.pdf")💜
for i in range(len(src)):          # or a list of just the pages you need
   out = pymupdf.open()
   out.insert_pdf(src, from_page=i, to_page=i)💜
   out.subset_fonts()💜             # keep only the glyphs this page uses
   out.save(f"book_p{i+1:03d}.pdf", garbage=4, deflate=True, use_objstms=True)💜

garbage=4 removes duplicate and unreferenced objects, deflate compresses the streams, and use_objstms packs small objects together. None of these change how the page looks or extracts. Font subsetting can occasionally fail on unusual font types (Type 3 or some older CID fonts). When that happens, the page still saves, just with the larger font.

Alternatives:

  • qpdf: `qpdf --split-pages book.pdf out-%d.pdf`1 is very fast and completely lossless. It drops resources a page doesn’t use, but it doesn’t re-subset fonts, so files stay larger. It’s a good choice if you’d rather have speed and zero risk than the smallest size.
  • Ghostscript (pdfwrite): this re-subsets fonts well, but it rebuilds the entire PDF. That’s the most likely of the three to change the text layer or recompress images. Given your earlier spacing trouble, I’d avoid it.

Two practical notes:

  • Scanned books: if a PDF is page images with an OCR layer, the image is most of the file size. Font subsetting won’t help much, and shrinking further means recompressing the image. Born-digital textbooks are where subsetting pays off.
  • Split only what you need: since you’ll do this at scale, pass just the highlighted page numbers rather than splitting entire books. That fits your pdf-highlight-extraction workflow naturally, because the highlight annotations already tell you which pages matter.

If you upload a sample PDF, I can run the script and compare the qpdf and PyMuPDF output sizes on your actual material.

Source: Claude ○˒

(echo:: @ ᯤ ᯤ ᯤ ᯤ ᯤ ᯤ)

Footnotes

  1. ??? ↩

Link to original