Spaces:
Sleeping
Sleeping
Add detailed logging for LaTeX to PDF conversion
Browse files- app/api/data/route.ts +25 -8
app/api/data/route.ts
CHANGED
|
@@ -122,44 +122,61 @@ export async function POST(request: NextRequest) {
|
|
| 122 |
|
| 123 |
// Auto-convert .tex files to PDF
|
| 124 |
if (fileName.endsWith('.tex')) {
|
| 125 |
-
console.log('Detected .tex file, auto-converting to PDF...')
|
| 126 |
try {
|
|
|
|
|
|
|
|
|
|
| 127 |
// Compile LaTeX to PDF using LaTeX.Online
|
| 128 |
const compileResponse = await fetch('https://latexonline.cc/compile', {
|
| 129 |
method: 'POST',
|
| 130 |
headers: {
|
| 131 |
'Content-Type': 'text/plain',
|
| 132 |
},
|
| 133 |
-
body: content
|
|
|
|
| 134 |
})
|
| 135 |
|
|
|
|
|
|
|
| 136 |
if (compileResponse.ok) {
|
| 137 |
const pdfBuffer = await compileResponse.arrayBuffer()
|
| 138 |
const pdfFileName = fileName.replace('.tex', '.pdf')
|
| 139 |
const pdfFilePath = path.join(targetDir, pdfFileName)
|
| 140 |
|
|
|
|
|
|
|
|
|
|
| 141 |
await writeFile(pdfFilePath, Buffer.from(pdfBuffer))
|
| 142 |
-
console.log(
|
| 143 |
|
| 144 |
return NextResponse.json({
|
| 145 |
success: true,
|
| 146 |
pdfGenerated: true,
|
| 147 |
-
pdfFileName: pdfFileName
|
|
|
|
| 148 |
})
|
| 149 |
} else {
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
return NextResponse.json({
|
| 152 |
success: true,
|
| 153 |
pdfGenerated: false,
|
| 154 |
-
error:
|
|
|
|
| 155 |
})
|
| 156 |
}
|
| 157 |
} catch (pdfError) {
|
| 158 |
-
console.error('Error generating PDF:', pdfError)
|
|
|
|
|
|
|
| 159 |
return NextResponse.json({
|
| 160 |
success: true,
|
| 161 |
pdfGenerated: false,
|
| 162 |
-
error: 'LaTeX file saved but PDF generation failed'
|
|
|
|
| 163 |
})
|
| 164 |
}
|
| 165 |
}
|
|
|
|
| 122 |
|
| 123 |
// Auto-convert .tex files to PDF
|
| 124 |
if (fileName.endsWith('.tex')) {
|
| 125 |
+
console.log('β¨ Detected .tex file, auto-converting to PDF...')
|
| 126 |
try {
|
| 127 |
+
console.log('π‘ Sending LaTeX content to LaTeX.Online API...')
|
| 128 |
+
console.log(` Content length: ${content.length} characters`)
|
| 129 |
+
|
| 130 |
// Compile LaTeX to PDF using LaTeX.Online
|
| 131 |
const compileResponse = await fetch('https://latexonline.cc/compile', {
|
| 132 |
method: 'POST',
|
| 133 |
headers: {
|
| 134 |
'Content-Type': 'text/plain',
|
| 135 |
},
|
| 136 |
+
body: content,
|
| 137 |
+
signal: AbortSignal.timeout(30000) // 30 second timeout
|
| 138 |
})
|
| 139 |
|
| 140 |
+
console.log(`π₯ Response status: ${compileResponse.status} ${compileResponse.statusText}`)
|
| 141 |
+
|
| 142 |
if (compileResponse.ok) {
|
| 143 |
const pdfBuffer = await compileResponse.arrayBuffer()
|
| 144 |
const pdfFileName = fileName.replace('.tex', '.pdf')
|
| 145 |
const pdfFilePath = path.join(targetDir, pdfFileName)
|
| 146 |
|
| 147 |
+
console.log(`πΎ PDF size: ${pdfBuffer.byteLength} bytes`)
|
| 148 |
+
console.log(`π Saving PDF to: ${pdfFilePath}`)
|
| 149 |
+
|
| 150 |
await writeFile(pdfFilePath, Buffer.from(pdfBuffer))
|
| 151 |
+
console.log(`β
PDF generated successfully: ${pdfFileName}`)
|
| 152 |
|
| 153 |
return NextResponse.json({
|
| 154 |
success: true,
|
| 155 |
pdfGenerated: true,
|
| 156 |
+
pdfFileName: pdfFileName,
|
| 157 |
+
message: `LaTeX file saved and converted to ${pdfFileName}`
|
| 158 |
})
|
| 159 |
} else {
|
| 160 |
+
const errorText = await compileResponse.text()
|
| 161 |
+
console.error('β PDF compilation failed:')
|
| 162 |
+
console.error(' Status:', compileResponse.status)
|
| 163 |
+
console.error(' Error:', errorText)
|
| 164 |
return NextResponse.json({
|
| 165 |
success: true,
|
| 166 |
pdfGenerated: false,
|
| 167 |
+
error: `LaTeX file saved but PDF generation failed: ${compileResponse.status}`,
|
| 168 |
+
details: errorText.substring(0, 500)
|
| 169 |
})
|
| 170 |
}
|
| 171 |
} catch (pdfError) {
|
| 172 |
+
console.error('β Error generating PDF:', pdfError)
|
| 173 |
+
console.error(' Error type:', pdfError instanceof Error ? pdfError.name : typeof pdfError)
|
| 174 |
+
console.error(' Error message:', pdfError instanceof Error ? pdfError.message : String(pdfError))
|
| 175 |
return NextResponse.json({
|
| 176 |
success: true,
|
| 177 |
pdfGenerated: false,
|
| 178 |
+
error: 'LaTeX file saved but PDF generation failed',
|
| 179 |
+
details: pdfError instanceof Error ? pdfError.message : String(pdfError)
|
| 180 |
})
|
| 181 |
}
|
| 182 |
}
|