tex2pdf_docker/main.py

26 lines
764 B
Python

import asyncio
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import StreamingResponse
from io import BytesIO
app = FastAPI()
@app.post("/tex2pdf")
async def convert_to_pdf(file: UploadFile = File(...)):
input_file = BytesIO(await file.read())
process = await asyncio.create_subprocess_exec(
'pdflatex', '-f', 'pdf',
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
output_pdf, error = await process.communicate(input=input_file.getvalue())
if process.returncode == 0:
return StreamingResponse(BytesIO(output_pdf), media_type='application/pdf')
else:
return {"error": "Conversion failed.", "details": error.decode()}