Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

1.7 KB
Raw Blame
"""Generate deterministic text PDFs in memory without extra dependencies."""

from io import BytesIO

from pypdf import PdfWriter
from pypdf.generic import DecodedStreamObject, DictionaryObject, NameObject


def synthetic_pdf(
    document: int, *, pages: int = 2, lines: int = 40, words: int = 24
) -> bytes:
    """Repeated words create bulk; page/line markers make loss detectable."""
    if not 1 <= pages <= 500 or not 1 <= lines <= 100 or not 1 <= words <= 1000:
        raise ValueError("Synthetic PDF size is outside the test limits")
    writer = PdfWriter()
    font = DictionaryObject(
        {
            NameObject("/Type"): NameObject("/Font"),
            NameObject("/Subtype"): NameObject("/Type1"),
            NameObject("/BaseFont"): NameObject("/Helvetica"),
        }
    )
    for number in range(pages):
        page = writer.add_blank_page(width=612, height=792)
        page[NameObject("/Resources")] = DictionaryObject(
            {
                NameObject("/Font"): DictionaryObject(
                    {NameObject("/F1"): writer._add_object(font)}
                ),
            }
        )
        content = ["BT /F1 8 Tf 10 TL 24 768 Td"]
        for line in range(lines):
            marker = f"synthetic-document-{document:06d}-page-{number:04d}-line-{line:03d}"
            text = marker + " " + "vector research alpha beta " * words
            content.append(f"({text}) Tj T*")
        content.append("ET")
        stream = DecodedStreamObject()
        stream.set_data("\n".join(content).encode("ascii"))
        page[NameObject("/Contents")] = writer._add_object(stream)
    with BytesIO() as output:
        writer.write(output)
        result = output.getvalue()
    writer.close()
    return result