生成HTML到PDF,并使用Laravel将生成的PDF上传到数据库?

I am making an online exam web app. Where students will log in, and there will be an exam packet. Now the packets already have questions stored in the database, I have already converted the questions into HTML to PDF format and it's available to be downloaded. The question is, how will I able to get the generated PDF to also be uploaded to the database? Because the questions for each user will be in random order, and I would like each user to continue the previous exam packet once they log in, so it doesn't regenerate again.

You dont need to store it in database if u already have a html template of the data that are going to be included in pdf

Step 1) Download install this package composer require barryvdh/laravel-dompdf

Step 2) fter install pdf package for laravel then we must be configure it. just following littel thing. oprn your config/app.php file and set following value for providers array and aliases array

'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
],

Then run php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

Step 3) Create a route

Route::get('generate-pdf', 'PdfGenerateController@pdfview')->name('generate-pdf');

Step 4) Add the logic to download a view as pdf:

$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();

As you already have the PDF file, instead of upload it to database (bad approach) you could do this:

  • (ok) Generate and download the PDF on demand. Every time student clicks to download, you will generate and download the PDF file.
  • (good) Generate the PDF only at the very first time. Save the generated name on database. On further clicks, just get the filename and append it to your PDF's folder. Like this: asset('exams/'.$pdf_filename.'.pdf'); to display/download the PDF.