from pdfitdown.pdfconversion import Converter
from markdown_pdf import MarkdownPdf, Section
from google import genai
from pathlib import Path
client = genai.Client()
def conversion_callback(input_file: str, output_file: str, title: str | None = None, overwrite: bool = True):
if Path(input_file).suffix not in [".json", ".yaml", ".yml"]:
raise ValueError("File is not an OpenAPI spec document")
uploaded_file = client.files.upload(file=Path(input_file))
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=["Can you please provide a human-readable and elegant description of the attached OpenAPI spec, with routes and associated names, paths, and request/response formats?", uploaded_file],
)
content = response.text
pdf = MarkdownPdf(toc_level=0)
pdf.add_section(Section(content))
pdf.meta["title"] = title or f"{input_file} - OpenAPI Spec"
pdf.save(output_file)
return output_file
converter = Converter(conversion_callback=conversion_callback)
converter.convert(file_path="openapi.json", output_path="openapi_spec.pdf")