A three-page PDF made of selectable text and a PDF made from scanned pages can react very differently to the same compression setting. If you are trying to meet an attachment limit, keep the original and compare the output size and what you can actually read or select.
Results with two generated PDFs
I tested a 3,248-byte text PDF and a 3,782,988-byte PDF made from two noisy grayscale JPEGs, using synthetic data. The table shows output sizes in bytes.
| Tool | Searchable text PDF | JPEG-based PDF |
|---|---|---|
| PDF Fan | 2,040 | 830,008 |
| iLovePDF | 2,549 | 415,758 |
| Ghostscript 10.05.0 | 4,055 | 3,027,988 |
| qpdf 12.0.0 | 2,035 | 3,782,237 |
I used the recommended setting for both browser tools, the /ebook preset for Ghostscript, and Flate recompression with object streams for qpdf. I work on the PDF Fan toolkit, one of the tools in the table.
In this test, qpdf cut about 37% from the text PDF and about 0.02% from the JPEG-based file. The qpdf command focused on PDF objects and Flate streams and left the JPEG streams as they were. Ghostscript’s ebook preset shrank the JPEG-based file by around 20% and made the small text PDF larger. The two browser presets also produced smaller JPEG-based files. The byte counts describe these inputs and presets. Your scan’s resolution, compression and small-print legibility can change the result.
Repeat the test on your machine
Save the Python below as make_compression_samples.py. Run it with Pillow 13.0.0 and ReportLab 4.4.9. It creates both inputs without personal documents. JPEG encoder versions can affect the starting size.
Show the Python script
"""Generate two synthetic PDFs for a reproducible compression comparison."""
import argparse
import io
import random
from pathlib import Path
from PIL import Image, ImageDraw
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
def text_sample(path: Path) -> None:
pdf = canvas.Canvas(str(path), pagesize=(612, 792), invariant=True)
for page in range(1, 4):
pdf.setFont("Helvetica-Bold", 22)
pdf.drawString(54, 735, f"Document archive sample - page {page}")
pdf.setFont("Helvetica", 12)
for row in range(1, 31):
pdf.drawString(54, 700 - row * 18, f"Entry {row:02}. Print and archive this example record.")
pdf.showPage()
pdf.save()
def imaging_sample(path: Path) -> None:
pdf = canvas.Canvas(str(path), pagesize=(612, 792), invariant=True)
for page in range(1, 3):
random_source = random.Random(9000 + page)
noise = bytes(random_source.randrange(218, 253) for _ in range(1600 * 2000))
image = Image.frombytes("L", (1600, 2000), noise)
draw = ImageDraw.Draw(image)
draw.text((110, 90), f"Synthetic image page {page}", fill=10, font_size=72)
for row in range(10):
top = 260 + row * 150
draw.text((110, top), f"Item {row + 1:02} - synthetic example", fill=20, font_size=36)
draw.line((110, top + 80, 1480, top + 80), fill=70, width=3)
buffer = io.BytesIO()
image.save(buffer, format="JPEG", quality=94)
buffer.seek(0)
pdf.drawImage(ImageReader(buffer), 18, 36, width=576, height=720)
pdf.showPage()
pdf.save()
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("output", type=Path)
output = parser.parse_args().output
output.mkdir(parents=True, exist_ok=True)
text_sample(output / "text-only.pdf")
imaging_sample(output / "image-scan.pdf")
if __name__ == "__main__":
main()
python make_compression_samples.py ./samples
for input in samples/text-only.pdf samples/image-scan.pdf; do
name=${input##*/}
qpdf --recompress-flate --compression-level=9 --object-streams=generate \
"$input" "qpdf-$name"
gs -q -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/ebook \
-dNOPAUSE -dBATCH "-sOutputFile=gs-$name" "$input"
wc -c "$input" "qpdf-$name" "gs-$name"
done
Run qpdf --check on each result and pdfinfo -box to compare page counts and bounds. For the text sample, run pdftotext on each copy and compare the outputs. For the JPEG sample, zoom in on the smallest text and faintest lines in both files. In my test all eight outputs passed the structure check and kept the page count and bounds. Text extraction matched on all four outputs from the searchable sample.
See the qpdf file size options and Ghostscript preset documentation for the command settings.
发表评论