Spaces:
Sleeping
Sleeping
File size: 3,113 Bytes
ad31128 56ce56a ad31128 fc22af7 b7fb9d7 ad31128 fc22af7 d0eea2e fc22af7 d0eea2e fc22af7 56ce56a fc22af7 ad31128 fc22af7 d0eea2e fc22af7 d0eea2e fc22af7 d0eea2e ad31128 b7fb9d7 ad31128 56ce56a d0eea2e 56ce56a ad31128 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
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 }
)
}
} |