|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Create a minimal PDF with Brotli compression for testing purposes. |
| 4 | +
|
| 5 | +This script generates a simple PDF file that uses Brotli compression |
| 6 | +for the content stream, allowing for testing of the BrotliDecode filter |
| 7 | +in pypdf. |
| 8 | +""" |
| 9 | + |
| 10 | +import brotli |
| 11 | +import os |
| 12 | + |
| 13 | +# Simple PDF structure with Brotli-compressed content stream |
| 14 | +# The content stream will contain a simple "Hello, Brotli!" text |
| 15 | +content_stream = b"BT /F1 24 Tf 100 700 Td (Hello, Brotli!) Tj ET" |
| 16 | +compressed_content = brotli.compress(content_stream, quality=5) |
| 17 | + |
| 18 | +# PDF structure |
| 19 | +pdf = [ |
| 20 | + b"%PDF-1.7\n", |
| 21 | + b"1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n", |
| 22 | + b"2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n", |
| 23 | + b"3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>\nendobj\n", |
| 24 | + b"4 0 obj\n<< /Length " + str(len(compressed_content)).encode() + b" /Filter /BrotliDecode >>\nstream\n" + compressed_content + b"\nendstream\nendobj\n", |
| 25 | + b"5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>\nendobj\n", |
| 26 | + b"xref\n0 6\n0000000000 65535 f \n0000000010 00000 n \n0000000060 00000 n \n0000000115 00000 n \n0000000234 00000 n \n" + |
| 27 | + (b"0000000" + str(334 + len(compressed_content)).encode() + b" 00000 n \n"), |
| 28 | + b"trailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n" + str(400 + len(compressed_content)).encode() + b"\n%%EOF" |
| 29 | +] |
| 30 | + |
| 31 | +# Write PDF to file |
| 32 | +# Define paths relative to the script's location (resources/) |
| 33 | +script_dir = os.path.dirname(__file__) |
| 34 | +output_dir = os.path.join(script_dir, "brotli-test") |
| 35 | +output_path = os.path.join(output_dir, "brotli-compressed.pdf") |
| 36 | + |
| 37 | +# Ensure the output directory exists |
| 38 | +os.makedirs(output_dir, exist_ok=True) |
| 39 | +with open(output_path, "wb") as f: |
| 40 | + for part in pdf: |
| 41 | + f.write(part) |
| 42 | + |
| 43 | +print(f"Created test PDF with Brotli compression at: {output_path}") |
0 commit comments