ProjectCraft / export /pdf_export.py
yania-n's picture
Claude Sonnet 4.6
Initial deployment of ProjectCraft
3c5146b
Raw History Blame Contribute Delete
889 Bytes
"""PDF export — saves Plotly figures as PDF files using kaleido."""
import asyncio
import os
import plotly.graph_objects as go
async def export_pdf(fig: go.Figure, filename: str) -> str:
"""
Export a Plotly figure to PDF. Save to /tmp/{filename}.pdf.
Uses asyncio.to_thread to avoid blocking.
Returns the file path on success, empty string on failure.
"""
path = f"/tmp/{filename}.pdf"
try:
await asyncio.to_thread(
fig.write_image, path, format="pdf", width=1200, height=600
)
return path
except Exception:
return ""
async def export_multiple_pdf(figures: dict[str, go.Figure]) -> dict[str, str]:
"""Export multiple figures to PDF in parallel."""
results = await asyncio.gather(
*[export_pdf(fig, name) for name, fig in figures.items()]
)
return dict(zip(figures.keys(), results))