Test cases / pdf

deft pdf compress

Make smaller: --level, --target-size 2MB

6 runs on real files, 8 tests on generated files, not shown. 0 failed.

Fixed cases

Generated files, compared with ground truth by tools other than deft.

real print PDF with CMYK photos shrinks and keeps its coloursPassed

deft pdf compress photos.pdf --level strong -o out.pdf

Input

photos.pdf 2.5 MB · 2 pages
Page 1Page 2

Output

out.pdf 530.5 KB · 2 pages
Page 1Page 2
out.png 126.5 KB · 440x340
out.png
photos.png 130.0 KB · 440x340
photos.png
What it printed
ok pdf.compress photos.pdf -> out.pdf  2.5MB -> 530KB (-79%)  0.09s
Automatic checks (2)
  • max_bytes: bytes = 1300000, file = "out.pdf"
  • python: code = "import subprocess\nfrom PIL import Image, ImageChops, ImageStat\nfor f in ('photos', 'out'):\n subprocess.check_call(['gs', '-q', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-sDEVICE=png16m', '-r40', '-dFirstPage=1', '-dLastPage=1', '-o', f + '.png', f + '.pdf'])\ndiff = ImageStat.Stat(ImageChops.difference(Image.open('photos.png').convert('RGB'), Image.open('out.png').convert('RGB'))).mean\nassert max(diff) < 15, diff\n"

Real-file tasks

Real files and a plain-language job, from the agent benchmark. These are the reference answers, not an agent run.

Assemble an application from mixed filesPassed

Put together one application.pdf for a portal: letter.docx first, then the photo DSCN0010.jpg, then fw9.pdf. Number the pages at the bottom. The portal rejects anything over 1MB.

deft doc to-pdf letter.docx -o letter.pdf
deft image to-pdf DSCN0010.jpg -o photo.pdf
deft pdf merge letter.pdf photo.pdf fw9.pdf -o joined.pdf
deft pdf number joined.pdf -o numbered.pdf
deft pdf compress numbered.pdf --target-size 1MB -o application.pdf

Input

DSCN0010.jpg 157.9 KB · 640x480
DSCN0010.jpg
fw9.pdf 137.5 KB · 6 pages
Page 1Page 2Page 3
letter.docx 11.3 KB
Rendered by macOS Quick Look, first page

Rendered by macOS Quick Look, first page

Text inside
I am a test document
This is page 1
I am Calibri (Body) in font size 11

This is page two
It’s Arial Black in 16 point
It’s also in blue

Output

application.pdf 222.6 KB · 9 pages
Page 1Page 2Page 3
joined.pdf 220.9 KB · 9 pages
Page 1Page 2Page 3
letter.pdf 9.6 KB · 2 pages
Page 1Page 2
numbered.pdf 224.1 KB · 9 pages
Page 1Page 2Page 3
photo.pdf 158.6 KB · 1 page
Page 1
Automatic checks (2)
  • max_bytes: file = "application.pdf", bytes = 1048576
  • python: code = "import subprocess\ndef text(first, last):\n return subprocess.run(['gs', '-q', '-dNOPAUSE', '-dBATCH', '-sDEVICE=txtwrite', f'-dFirstPage={first}', f'-dLastPage={last}', '-sOutputFile=-', 'application.pdf'], capture_output=True, text=True, errors='replace').stdout\nn = int(subprocess.check_output(['gs', '-q', '-dNODISPLAY', '--permit-file-read=application.pdf', '-c', '(application.pdf) (r) file runpdfbegin pdfpagecount = quit'], text=True).split()[-1])\nw9 = subprocess.check_output(['gs', '-q', '-dNODISPLAY', '--permit-file-read=fw9.pdf', '-c', '(fw9.pdf) (r) file runpdfbegin pdfpagecount = quit'], text=True).split()[-1]\nassert n >= int(w9) + 2, n\nassert 'W-9' in text(n - int(w9) + 1, n - int(w9) + 1), 'the form is not last'\nassert 'W-9' not in text(1, 1), 'the letter is not first'\nassert str(n) in text(n, n).split(), 'last page has no number'\n"

Compress a folder of PDFsPassed

Compress every PDF in the reports folder and put the results in a folder called compressed, keeping the file names.

deft pdf compress reports/*.pdf -o compressed/

Input

reports/f1040.pdf 215.1 KB · 2 pages
Page 1Page 2
reports/fw9.pdf 137.5 KB · 6 pages
Page 1Page 2Page 3
reports/scan.pdf 1.8 MB · 4 pages
Page 1Page 2Page 3

Output

compressed/f1040.pdf 213.7 KB · 2 pages
Page 1Page 2
compressed/fw9.pdf 130.2 KB · 6 pages
Page 1Page 2Page 3
compressed/scan.pdf 431.6 KB · 4 pages
Page 1Page 2Page 3
Automatic checks (4)
  • count_files: glob = "compressed/*.pdf", count = 3
  • exists: file = "compressed/scan.pdf"
  • each: glob = "compressed/*.pdf", check = {"type": "format", "format": "pdf"}
  • max_bytes: file = "compressed/scan.pdf", bytes = 1500000

Compress a photo-heavy PDFPassed

Reduce photos.pdf to less than half its size and save it as light.pdf.

deft pdf compress photos.pdf --level strong -o light.pdf

Input

photos.pdf 2.5 MB · 2 pages
Page 1Page 2

Output

light.pdf 530.5 KB · 2 pages
Page 1Page 2
Automatic checks (3)
  • pdf_pages: file = "light.pdf", pages = 2
  • max_bytes: file = "light.pdf", bytes = 1322761
  • pdf_page: file = "light.pdf", page = 1, ink_min = 0.2

Shrink a scan for emailPassed

scan.pdf is too big to email. Make a copy called small.pdf under 150KB that is still readable.

deft pdf compress scan.pdf --target-size 150KB -o small.pdf

Input

scan.pdf 1.8 MB · 4 pages
Page 1Page 2Page 3

Output

small.pdf 112.7 KB · 4 pages
Page 1Page 2Page 3
Automatic checks (3)
  • pdf_pages: file = "small.pdf", pages = 4
  • max_bytes: file = "small.pdf", bytes = 153600
  • pdf_page: file = "small.pdf", page = 1, ink_min = 0.02

Send a scan to an accountantPassed

scan.pdf is going to our accountant. Make the text searchable, keep it under 1MB, and protect it with the password hunter2. Save it as for-accountant.pdf.

deft pdf ocr scan.pdf -o searchable.pdf
deft pdf compress searchable.pdf --target-size 1MB -o small.pdf
deft pdf encrypt small.pdf --new-password hunter2 -o for-accountant.pdf

Input

scan.pdf 1.8 MB · 4 pages
Page 1Page 2Page 3

Output

This PDF has no preview: it needs a password or cannot be read.

searchable.pdf 1.9 MB · 4 pages
Page 1Page 2Page 3
small.pdf 453.0 KB · 4 pages
Page 1Page 2Page 3
Automatic checks (3)
  • max_bytes: file = "for-accountant.pdf", bytes = 1048576
  • pdf_encrypted: file = "for-accountant.pdf", encrypted = true
  • pdf_text: file = "for-accountant.pdf", password = "hunter2", contains = ["CORONA"]