Spaces:
Sleeping
Sleeping
Fix LaTeX compile API to use node-pdflatex and improve error messages
Browse files- app/api/latex/compile/route.ts +19 -28
- app/components/TextEditor.tsx +12 -3
app/api/latex/compile/route.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
| 2 |
|
| 3 |
export async function POST(request: NextRequest) {
|
| 4 |
try {
|
|
@@ -11,47 +12,37 @@ export async function POST(request: NextRequest) {
|
|
| 11 |
)
|
| 12 |
}
|
| 13 |
|
| 14 |
-
console.log('Compiling LaTeX to PDF using
|
|
|
|
| 15 |
|
| 16 |
-
//
|
| 17 |
-
const
|
| 18 |
-
method: 'POST',
|
| 19 |
-
headers: {
|
| 20 |
-
'Content-Type': 'text/plain',
|
| 21 |
-
},
|
| 22 |
-
body: latex
|
| 23 |
-
})
|
| 24 |
-
|
| 25 |
-
if (!compileResponse.ok) {
|
| 26 |
-
const errorText = await compileResponse.text()
|
| 27 |
-
console.error('LaTeX compilation failed:', errorText)
|
| 28 |
-
return NextResponse.json(
|
| 29 |
-
{
|
| 30 |
-
error: 'LaTeX compilation failed',
|
| 31 |
-
details: errorText
|
| 32 |
-
},
|
| 33 |
-
{ status: 500 }
|
| 34 |
-
)
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
// Get the PDF buffer
|
| 38 |
-
const pdfBuffer = await compileResponse.arrayBuffer()
|
| 39 |
|
| 40 |
-
console.log(
|
| 41 |
|
| 42 |
// Return the PDF
|
| 43 |
-
const response = new NextResponse(
|
| 44 |
response.headers.set('Content-Type', 'application/pdf')
|
| 45 |
response.headers.set('Content-Disposition', `attachment; filename="${filename || 'document.pdf'}"`)
|
| 46 |
|
| 47 |
return response
|
| 48 |
|
| 49 |
} catch (error) {
|
| 50 |
-
console.error('Error compiling LaTeX:', error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return NextResponse.json(
|
| 52 |
{
|
| 53 |
error: 'Failed to compile LaTeX',
|
| 54 |
-
details:
|
|
|
|
| 55 |
},
|
| 56 |
{ status: 500 }
|
| 57 |
)
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from 'next/server'
|
| 2 |
+
import pdflatex from 'node-pdflatex'
|
| 3 |
|
| 4 |
export async function POST(request: NextRequest) {
|
| 5 |
try {
|
|
|
|
| 12 |
)
|
| 13 |
}
|
| 14 |
|
| 15 |
+
console.log('📄 Compiling LaTeX to PDF using node-pdflatex...')
|
| 16 |
+
console.log(` Content length: ${latex.length} characters`)
|
| 17 |
|
| 18 |
+
// Compile LaTeX to PDF using node-pdflatex
|
| 19 |
+
const pdfBuffer = await pdflatex(latex)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
console.log(`✅ LaTeX compiled successfully! PDF size: ${pdfBuffer.byteLength} bytes`)
|
| 22 |
|
| 23 |
// Return the PDF
|
| 24 |
+
const response = new NextResponse(pdfBuffer)
|
| 25 |
response.headers.set('Content-Type', 'application/pdf')
|
| 26 |
response.headers.set('Content-Disposition', `attachment; filename="${filename || 'document.pdf'}"`)
|
| 27 |
|
| 28 |
return response
|
| 29 |
|
| 30 |
} catch (error) {
|
| 31 |
+
console.error('❌ Error compiling LaTeX:', error)
|
| 32 |
+
console.error(' Error type:', error instanceof Error ? error.constructor.name : typeof error)
|
| 33 |
+
console.error(' Error message:', error instanceof Error ? error.message : String(error))
|
| 34 |
+
|
| 35 |
+
// Extract more detailed error info if available
|
| 36 |
+
let errorDetails = error instanceof Error ? error.message : String(error)
|
| 37 |
+
if (error && typeof error === 'object' && 'stderr' in error) {
|
| 38 |
+
errorDetails = (error as any).stderr || errorDetails
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
return NextResponse.json(
|
| 42 |
{
|
| 43 |
error: 'Failed to compile LaTeX',
|
| 44 |
+
details: errorDetails,
|
| 45 |
+
message: 'There may be a syntax error in your LaTeX code. Check the console for details.'
|
| 46 |
},
|
| 47 |
{ status: 500 }
|
| 48 |
)
|
app/components/TextEditor.tsx
CHANGED
|
@@ -128,12 +128,21 @@ export function TextEditor({
|
|
| 128 |
const url = URL.createObjectURL(blob)
|
| 129 |
setPdfUrl(url)
|
| 130 |
} else {
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
} catch (error) {
|
| 135 |
console.error('Compilation error:', error)
|
| 136 |
-
alert(
|
| 137 |
} finally {
|
| 138 |
setIsCompiling(false)
|
| 139 |
}
|
|
|
|
| 128 |
const url = URL.createObjectURL(blob)
|
| 129 |
setPdfUrl(url)
|
| 130 |
} else {
|
| 131 |
+
// Try to get error details from response
|
| 132 |
+
const contentType = response.headers.get('content-type')
|
| 133 |
+
if (contentType?.includes('application/json')) {
|
| 134 |
+
const errorData = await response.json()
|
| 135 |
+
console.error('PDF compilation failed:', errorData)
|
| 136 |
+
alert(`Failed to compile LaTeX!\n\nError: ${errorData.error || 'Unknown error'}\n\nDetails: ${errorData.details || 'Check console for more info'}`)
|
| 137 |
+
} else {
|
| 138 |
+
const errorText = await response.text()
|
| 139 |
+
console.error('PDF compilation failed:', errorText)
|
| 140 |
+
alert(`Failed to compile LaTeX!\n\n${errorText.substring(0, 200)}`)
|
| 141 |
+
}
|
| 142 |
}
|
| 143 |
} catch (error) {
|
| 144 |
console.error('Compilation error:', error)
|
| 145 |
+
alert(`Failed to compile LaTeX!\n\nError: ${error instanceof Error ? error.message : String(error)}`)
|
| 146 |
} finally {
|
| 147 |
setIsCompiling(false)
|
| 148 |
}
|