Adonis 5中PDF文件无法查看及下载的解决方案咨询
Hey there! I get that it's frustrating when PDFs aren't working like your images do in Adonis 5, especially since Drive was removed from the newer version. Let's walk through a couple of solid solutions to get PDF viewing and downloading up and running:
1. Manually Serve PDFs with Custom Response Headers
This approach gives you full control over whether the browser displays the PDF inline or triggers a download. Here's how to implement it in a controller:
First, create a controller (e.g., PdfController.ts) with methods for viewing and downloading:
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import fs from 'fs' import path from 'path' export default class PdfController { // Method to view PDF directly in the browser public async view({ response, params }: HttpContextContract) { // Build the full path to your PDF in the public directory const pdfFilePath = path.join(__dirname, '../../public', params.filename) // Check if the file exists if (!fs.existsSync(pdfFilePath)) { return response.notFound('Requested PDF file does not exist') } // Set headers to tell the browser to display the PDF inline response.header('Content-Type', 'application/pdf') response.header('Content-Disposition', `inline; filename="${params.filename}"`) // Send the file to the client return response.download(pdfFilePath) } // Method to trigger PDF download public async download({ response, params }: HttpContextContract) { const pdfFilePath = path.join(__dirname, '../../public', params.filename) if (!fs.existsSync(pdfFilePath)) { return response.notFound('Requested PDF file does not exist') } // Set headers to force a download response.header('Content-Type', 'application/pdf') response.header('Content-Disposition', `attachment; filename="${params.filename}"`) return response.download(pdfFilePath) } }
Then add routes for these methods in start/routes.ts:
// View PDF in browser Route.get('/pdfs/view/:filename', 'PdfController.view') // Download PDF Route.get('/pdfs/download/:filename', 'PdfController.download')
Now you can access a PDF like my-document.pdf by visiting your-app-url/pdfs/view/my-document.pdf to view it, or your-app-url/pdfs/download/my-document.pdf to download it.
2. Fix Static File MIME Type Configuration
If you want to serve PDFs directly from the public directory (just like your images), make sure Adonis 5's static file service recognizes the PDF MIME type.
Edit config/static.ts and add the PDF mapping to the mimeTypes object:
export default { enabled: true, dotFiles: 'ignore', etag: true, lastModified: true, mimeTypes: { // Add this line to map .pdf files to the correct MIME type 'pdf': 'application/pdf', }, }
After saving this change, restart your Adonis server. Now when you visit your-app-url/my-document.pdf, the browser should automatically render the PDF instead of treating it as an unknown file type.
Quick Notes
- Make sure your PDF files in the
publicdirectory have read permissions so the server can access them. - Double-check the file paths in your controller to ensure they point correctly to the
publicdirectory (adjust thepath.joinarguments if your controller is in a different subfolder).
内容的提问来源于stack exchange,提问作者Gario3

