I’m currently using Snappy/wkhtmltopdf to convert my input texts to .pdf(including images/files/text etc.). When I input a text, images and files the page refreshes instead of streaming the pdf view, even downloading the file doesn’t work. I have tested each debug e.g. images/text only and it worked flawlessly until I combined them both.
PDFController
public function generatePDF(Request $request)
{
// Handle image upload
if ($request->hasFile('image')) {
$image = $request->file('image');
$imagePath = $image->store('images', 'public');
$imageUrl = Storage::url($imagePath);
} else {
$imageUrl = null;
}
// Handle PDF upload
if ($request->hasFile('pdf')) {
$pdfFile = $request->file('pdf');
$pdfPath = $pdfFile->store('pdfs', 'public');
$pdfUrl = Storage::url($pdfPath);
} else {
$pdfUrl = null;
}
// Prepare data for the view
$data = [
'text' => $request->input('text'),
'image_url' => $imageUrl,
'pdf_url' => $pdfUrl,
];
// Load the view and pass data
$pdf = Pdf::loadView('pdf-view', $data);
// Stream or download the PDF
return $pdf->stream(); // Use ->download() if you prefer downloading
}
form-pdf.blade.php(The input view)
<form action="{{ route('generate.pdf') }}" method="POST" enctype="multipart/form-data">
@csrf
<label for="text">Text:</label>
<input type="text" name="text" id="text" required>
<label for="image">Image:</label>
<input type="file" name="image" id="image" accept="image/*">
<label for="pdf">PDF:</label>
<input type="file" name="pdf" id="pdf" accept="application/pdf">
<button type="submit" class="bg-green-500 flex-1 px-2 rounded-md">Generate PDF</button>
</form>
I’ve tried changing the routing and the controller functions. It works nicely only when it is a direct path and only input text from users. Tried to do different projects but it still the same problem. I don’t have any database to save the images and files etc.(I’m still new in coding and laravel). Hope someone can explain why it doesn’t stream or download the pdf to local. Even if it means changing my whole coding base. Thanks.