Spaces:
Sleeping
Sleeping
| import { NextRequest, NextResponse } from 'next/server' | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const { latex, filename } = await request.json() | |
| if (!latex) { | |
| return NextResponse.json( | |
| { error: 'LaTeX content is required' }, | |
| { status: 400 } | |
| ) | |
| } | |
| console.log('π Compiling LaTeX to PDF...') | |
| console.log(` Content length: ${latex.length} characters`) | |
| // Try LaTeX compilation API with better error handling | |
| try { | |
| const compileResponse = await fetch('https://texlive.net/cgi-bin/latexcgi', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| }, | |
| body: new URLSearchParams({ | |
| filecontents: latex, | |
| filename: 'document.tex', | |
| engine: 'pdflatex', | |
| return: 'pdf' | |
| }), | |
| signal: AbortSignal.timeout(60000) | |
| }) | |
| // Check if response is actually a PDF | |
| const contentType = compileResponse.headers.get('content-type') | |
| if (compileResponse.ok && contentType?.includes('pdf')) { | |
| const pdfBuffer = Buffer.from(await compileResponse.arrayBuffer()) | |
| console.log(`β LaTeX compiled successfully! PDF size: ${pdfBuffer.byteLength} bytes`) | |
| const response = new NextResponse(pdfBuffer) | |
| response.headers.set('Content-Type', 'application/pdf') | |
| response.headers.set('Content-Disposition', `attachment; filename="${filename || 'document.pdf'}"`) | |
| return response | |
| } else { | |
| // API returned error or HTML | |
| const errorText = await compileResponse.text() | |
| console.error('β LaTeX compilation failed:', errorText.substring(0, 200)) | |
| return NextResponse.json( | |
| { | |
| error: 'LaTeX compilation service unavailable', | |
| details: 'The external LaTeX compiler is currently unavailable.', | |
| message: 'LaTeX compilation services are temporarily unavailable. Your .tex file has been saved - you can download and compile it locally.' | |
| }, | |
| { status: 503 } | |
| ) | |
| } | |
| } catch (fetchError) { | |
| console.error('β API fetch error:', fetchError) | |
| return NextResponse.json( | |
| { | |
| error: 'LaTeX compilation service unavailable', | |
| details: 'Could not reach LaTeX compilation service', | |
| message: 'LaTeX compilation services are temporarily unavailable. Your .tex file has been saved - you can download and compile it locally.' | |
| }, | |
| { status: 503 } | |
| ) | |
| } | |
| } catch (error) { | |
| console.error('β Error compiling LaTeX:', error) | |
| console.error(' Error type:', error instanceof Error ? error.constructor.name : typeof error) | |
| console.error(' Error message:', error instanceof Error ? error.message : String(error)) | |
| return NextResponse.json( | |
| { | |
| error: 'Failed to compile LaTeX', | |
| details: error instanceof Error ? error.message : String(error), | |
| message: 'Network error or compilation timeout. Please try again.' | |
| }, | |
| { status: 500 } | |
| ) | |
| } | |
| } |