Convert the files which are not pdf into pdf and size less than 400kb of all files and create a folder of nertpg and arrange individual files in that folder so i can choose file from folder later
~/
~/attachments/3ed117e3-b954-490e-8bff-12cc73fa3d75
python - <<'PY' from pathlib import Path import fitz base=Path('/tmp/workspace/attachments/3ed117e3-b954-490e-8bff-12cc73fa3d75') for p in sorted(base.iterdir()): size=p.stat().st_size if p.suffix.lower()=='.pdf': try: pages=len(fitz.open(p)) except: pages='?' else: pages='image' print(f'{p.name}\t{size}\t{pages}') PY
python - <<'PY' from pathlib import Path from pypdf import PdfReader base=Path('/tmp/workspace/attachments/3ed117e3-b954-490e-8bff-12cc73fa3d75') for p in sorted(base.iterdir()): n = len(PdfReader(str(p)).pages) if p.suffix.lower()=='.pdf' else 'image' print(f'{p.name}\t{p.stat().st_size/1024:.1f} KB\t{n}') PY
mkdir -p "/tmp/workspace/nertpg" && python - <<'PY' from pathlib import Path from PIL import Image from pypdf import PdfReader, PdfWriter import io, shutil src=Path('/tmp/workspace/attachments/3ed117e3-b954-490e-8bff-12cc73fa3d75') out=Path('/tmp/workspace/nertpg') limit=400*1024 def save_jpeg_pdf(img, target): img=img.convert('RGB') # Avoid upscaling. successive settings retain readable submissions while meeting cap for maxdim, q in [(2200,78),(1800,72),(1500,66),(1300,60),(1100,55),(900,50),(750,45)]: x=img.copy() x.thumbnail((maxdim,maxdim), Image.Resampling.LANCZOS) bio=io.BytesIO() x.save(bio,format='PDF',resolution=150.0,quality=q,optimize=True) data=bio.getvalue() if len(data)<limit: target.write_bytes(data) return len(data), (x.size,q) target.write_bytes(data) return len(data),(x.size,q) def pdf_to_images(pdf): r=PdfReader(str(pdf)) ims=[] for page in r.pages: entries=list(page.images) # scanned documents usually have one page-size image. Extract it losslessly to PIL. if not entries: raise RuntimeError('no extractable page image') biggest=max(entries,key=lambda im: im.image.width*im.image.height) ims.append(biggest.image.convert('RGB')) return ims def save_multi_pdf(images,target): # Choose a uniform progressive compression setting for all pages for maxdim,q in [(2200,78),(1800,72),(1500,66),(1300,60),(1100,55),(900,50),(750,45)]: pages=[] for img in images: x=img.copy(); x.thumbnail((maxdim,maxdim),Image.Resampling.LANCZOS); pages.append(x) bio=io.BytesIO(); pages[0].save(bio,format='PDF',save_all=True,append_images=pages[1:],resolution=150.0,quality=q,optimize=True) data=bio.getvalue() if len(data)<limit: target.write_bytes(data); return len(data),([p.size for p in pages],q) target.write_bytes(data); return len(data),([p.size for p in pages],q) for p in sorted(src.iterdir()): dest=out/(p.stem+'.pdf') try: if p.suffix.lower() in ['.jpg','.jpeg','.png']: res=save_jpeg_pdf(Image.open(p),dest) elif p.suffix.lower()=='.pdf': # First preserve compliant original PDFs unchanged. if p.stat().st_size < limit: shutil.copy2(p,dest); res=(dest.stat().st_size,'original') else: res=save_multi_pdf(pdf_to_images(p),dest) else: continue print(p.name,'=>',dest.name,round(dest.stat().st_size/1024,1),'KB',res) except Exception as e: print('ERROR',p.name,repr(e)) PY python - <<'PY' from pathlib import Path from pypdf import PdfReader p=Path('/tmp/workspace/nertpg') for f in sorted(p.glob('*.pdf')): print(f.name, f.stat().st_size, len(PdfReader(str(f)).pages)) PY
python - <<'PY' from pathlib import Path from pypdf import PdfReader folder=Path('/tmp/workspace/nertpg') limit=400*1024 files=sorted(folder.glob('*.pdf')) assert len(files)==10, f'Expected 10 files, found {len(files)}' for f in files: assert f.stat().st_size < limit, f'{f.name} is too large' reader=PdfReader(str(f)) assert len(reader.pages)>=1 print('Verified:', len(files), 'PDFs; every file is below 400 KB.') PY python - <<'PY' from pathlib import Path import zipfile folder=Path('/tmp/workspace/nertpg') archive=Path('/tmp/workspace/nertpg_documents.zip') with zipfile.ZipFile(archive,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=9) as z: for f in sorted(folder.glob('*.pdf')): z.write(f, arcname=f'nertpg/{f.name}') print(archive, archive.stat().st_size) PY ls -lh "/tmp/workspace/nertpg" "/tmp/workspace/nertpg_documents.zip"
nertpg folder. The JPG files were converted to PDF and the larger PDFs were compressed.nertpg documents
Archive · ZIP
Makeit unzip
~/
nertpg folder:file
File ·
Not ready yet it is showing
Photo
PDF Document · PDF
APAAR%20ID
PDF Document · PDF
Score%20card
PDF Document · PDF
Medical%20Fitness
PDF Document · PDF
NCL
PDF Document · PDF
Degree%20certificate
PDF Document · PDF
ICC
PDF Document · PDF
MMC%20Registartion
PDF Document · PDF
Aadhar%20Card
PDF Document · PDF
Admit%20Card
PDF Document · PDF
Not ready yet — try again once the response finishes
file
File ·
Create individually
Photo
PDF Document · PDF
APAAR%20ID
PDF Document · PDF
Score%20card
PDF Document · PDF
Medical%20Fitness
PDF Document · PDF
NCL
PDF Document · PDF
Degree%20certificate
PDF Document · PDF
ICC
PDF Document · PDF
MMC%20Registartion
PDF Document · PDF
Aadhar%20Card
PDF Document · PDF
Admit%20Card
PDF Document · PDF
Score card apar id fitness degree mmc adhar showing Not ready yet — try again once the response finishes
~/nertpg
mkdir -p "/tmp/workspace/individual_pdfs" && cp "/tmp/workspace/nertpg/Score card.pdf" "/tmp/workspace/individual_pdfs/Score_Card.pdf" && cp "/tmp/workspace/nertpg/APAAR ID.pdf" "/tmp/workspace/individual_pdfs/APAAR_ID.pdf" && cp "/tmp/workspace/nertpg/Medical Fitness.pdf" "/tmp/workspace/individual_pdfs/Medical_Fitness.pdf" && cp "/tmp/workspace/nertpg/Degree certificate.pdf" "/tmp/workspace/individual_pdfs/Degree_Certificate.pdf" && cp "/tmp/workspace/nertpg/MMC Registartion.pdf" "/tmp/workspace/individual_pdfs/MMC_Registration.pdf" && cp "/tmp/workspace/nertpg/Aadhar Card.pdf" "/tmp/workspace/individual_pdfs/Aadhar_Card.pdf" && python - <<'PY' from pathlib import Path from pypdf import PdfReader folder=Path('/tmp/workspace/individual_pdfs') for f in sorted(folder.glob('*.pdf')): assert f.stat().st_size < 400*1024 print(f.name, len(PdfReader(str(f)).pages), f.stat().st_size) PY
Score Card
PDF Document · PDF
APAAR ID
PDF Document · PDF
Medical Fitness
PDF Document · PDF
Degree Certificate
PDF Document · PDF
MMC Registration
PDF Document · PDF
Aadhar Card
PDF Document · PDF
And admit card
~/individual_pdfs
~/
cp "/tmp/workspace/nertpg/Admit Card.pdf" "/tmp/workspace/Admit_Card.pdf" && python - <<'PY' from pathlib import Path from pypdf import PdfReader p=Path('/tmp/workspace/Admit_Card.pdf') assert p.exists() and p.stat().st_size < 400*1024 assert len(PdfReader(str(p)).pages) == 2 print(f'Verified: {p.name}, {p.stat().st_size} bytes, 2 pages') PY
Admit Card
PDF Document · PDF
And admit card
~/
Admit Card
PDF Document · PDF
Above promt and rename as score card
~/
~/attachments/3ed117e3-b954-490e-8bff-12cc73fa3d75
python - <<'PY' from pathlib import Path from PIL import Image import io src=Path('/tmp/workspace/attachments/3ed117e3-b954-490e-8bff-12cc73fa3d75/Screenshot_20261008_144442_Samsung Notes.jpg') out=Path('/tmp/workspace/Score_Card.pdf') limit=400*1024 img=Image.open(src).convert('RGB') for max_dim, quality in [(2200,78),(1800,72),(1500,68),(1300,63),(1150,58),(1000,53),(900,48)]: page=img.copy() page.thumbnail((max_dim,max_dim), Image.Resampling.LANCZOS) buf=io.BytesIO() page.save(buf,format='PDF',resolution=150.0,quality=quality,optimize=True) data=buf.getvalue() if len(data)<limit: out.write_bytes(data) print(f'Created {out.name}: {len(data)} bytes, {page.size}px, quality {quality}') break else: raise RuntimeError('Cannot reduce PDF below 400 KB') PY python - <<'PY' from pathlib import Path from pypdf import PdfReader p=Path('/tmp/workspace/Score_Card.pdf') assert p.exists(), 'output missing' assert p.stat().st_size < 400*1024, 'size limit failed' assert len(PdfReader(str(p)).pages)==1, 'page validation failed' print(f'Verified: {p.name}, {p.stat().st_size/1024:.1f} KB, 1 page') PY
Score Card
PDF Document · PDF