from pathlib import Path
import sys
p=Path(sys.argv[1]);p.mkdir(exist_ok=True)
objs=[]
def obj(s):
 objs.append(s.encode());return len(objs)
obj('<< /Type /Catalog /Pages 2 0 R >>')
obj('<< /Type /Pages /Kids [4 0 R 6 0 R 8 0 R 10 0 R 12 0 R] /Count 5 >>')
obj('<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>')
for page in range(1,6):
 obj(f'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 3 0 R >> >> /Contents {5+(page-1)*2} 0 R >>')
 lines=['BT /F1 14 Tf 40 800 Td 19 TL',f'(Page {page} top - Android PDF preview) Tj']
 for i in range(1,39): lines += ['T*',f'(Page {page} line {i:02}: Sharp text should remain still at four times zoom.) Tj']
 lines += ['T*',f'(Page {page} bottom - PDF tile stability demonstration) Tj','ET']
 s='\n'.join(lines);obj(f'<< /Length {len(s)} >>\nstream\n{s}\nendstream')
b=bytearray(b'%PDF-1.4\n');offsets=[0]
for i,o in enumerate(objs,1):
 offsets.append(len(b));b+=f'{i} 0 obj\n'.encode()+o+b'\nendobj\n'
x=len(b);b+=f'xref\n0 {len(objs)+1}\n0000000000 65535 f \n'.encode()
for o in offsets[1:]:b+=f'{o:010} 00000 n \n'.encode()
b+=f'trailer << /Size {len(objs)+1} /Root 1 0 R >>\nstartxref\n{x}\n%%EOF'.encode()
(p/'sample.pdf').write_bytes(b)
