import sys

def esc(s):
    return s.replace("\\", r"\\").replace("(", r"\(").replace(")", r"\)")

def make(path, linhas):
    conteudo = "BT /F1 10 Tf 50 800 Td 14 TL\n"
    for i, l in enumerate(linhas):
        conteudo += f"({esc(l)}) Tj T*\n" if i < len(linhas) else ""
    conteudo += "ET\n"
    objs = [
        "<< /Type /Catalog /Pages 2 0 R >>",
        "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>",
        f"<< /Length {len(conteudo)} >>\nstream\n{conteudo}endstream",
        "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
    ]
    out = "%PDF-1.4\n"
    offsets = []
    for i, o in enumerate(objs, 1):
        offsets.append(len(out))
        out += f"{i} 0 obj\n{o}\nendobj\n"
    xref_pos = len(out)
    out += f"xref\n0 {len(objs)+1}\n0000000000 65535 f \n"
    for off in offsets:
        out += f"{off:010d} 00000 n \n"
    out += f"trailer\n<< /Size {len(objs)+1} /Root 1 0 R >>\nstartxref\n{xref_pos}\n%%EOF\n"
    open(path, "wb").write(out.encode("latin-1"))

BASE = [
    "PREFEITURA MUNICIPAL DE OEIRAS",
    "NOTA FISCAL DE SERVICOS ELETRONICA - NFS-e",
    "Numero da NFS-e: 000000123",
    "Data de Emissao: 08/08/2026",
    "PRESTADOR DE SERVICOS",
    "SINO COMUNICACAO E MARKETING LTDA",
    "CNPJ: 26.745.054/0001-70",
    "TOMADOR DE SERVICOS",
    "MUNICIPIO DE OEIRAS - PI",
    "DISCRIMINACAO DOS SERVICOS",
]

make("/tmp/nota_ok.pdf", BASE + ["Competencia: 07/2026",
     "Assessoria em comunicacao integrada referente a JULHO de 2026",
     "VALOR TOTAL DA NOTA R$ 5.000,00"])
make("/tmp/nota_mes_errado.pdf", BASE + ["Competencia: 06/2026",
     "Assessoria em comunicacao integrada referente a JUNHO de 2026",
     "VALOR TOTAL DA NOTA R$ 5.000,00"])
make("/tmp/nota_alheia.pdf", ["RECIBO DE PAGAMENTO", "EMPRESA QUALQUER LTDA",
     "CNPJ: 11.222.333/0001-44", "VALOR R$ 90,00", "Servico prestado em MARCO de 2026"])
print("3 PDFs gerados")
