# Builds tiny EPUB and PDF files for tools/books-test.js and manual checks: python3 tools/mkbooks.py import sys, zipfile, os out = sys.argv[1]; os.makedirs(out, exist_ok=True) with zipfile.ZipFile(os.path.join(out, 'test.epub'), 'w') as z: z.writestr('mimetype', 'application/epub+zip', compress_type=zipfile.ZIP_STORED) z.writestr('META-INF/container.xml', '') z.writestr('OEBPS/content.opf', 'x1Rabbit TalesA. Writeren') z.writestr('OEBPS/nav.xhtml', '') para = ' '.join(['The quick brown rabbit jumps over the lazy dog and keeps hopping through the green meadow.'] * 40) for i, n in ((1, 'One'), (2, 'Two')): z.writestr(f'OEBPS/c{i}.xhtml', f'{n}

Chapter {n}

' + ''.join(f'

{para} Marker{i}x{k}.

' for k in range(8)) + '') # minimal 3-page PDF objs = [] def add(s): objs.append(s); return len(objs) add('<< /Type /Catalog /Pages 2 0 R >>'); add('<< /Type /Pages /Kids [3 0 R 5 0 R 7 0 R] /Count 3 >>') for i in range(3): pid = add(f'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents {4+i*2} 0 R /Resources << /Font << /F1 9 0 R >> >> >>') text = f'BT /F1 24 Tf 72 700 Td (Page {i+1} of the rabbit manual) Tj 0 -40 Td (Carrots are great on page {i+1}.) Tj ET' add(f'<< /Length {len(text)} >>\nstream\n{text}\nendstream') add('<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>') data = '%PDF-1.4\n'; offs = [] for i, o in enumerate(objs): offs.append(len(data)); data += f'{i+1} 0 obj\n{o}\nendobj\n' x = len(data); data += f'xref\n0 {len(objs)+1}\n0000000000 65535 f \n' + ''.join(f'{o:010d} 00000 n \n' for o in offs) + f'trailer\n<< /Size {len(objs)+1} /Root 1 0 R >>\nstartxref\n{x}\n%%EOF\n' open(os.path.join(out, 'test.pdf'), 'w').write(data)