PDF generator package barryvdh/laravel-dompdf
, the PDF is working fine. I have this code:
public function fun_pdf($test_id) {
$test = Test::where('id', $test_id)->first();
$questions = (new TestQuestionsController)->questionwithanswers($test_id, $randomorder = 1);
$test_info = (new TestInfoController)->testInfo($test_id);
$pdf = PDF::loadView('website.tests_pdf.take-test', ['test_id' => $test_id, 'questions' => $questions, 'test' => $test, 'test_info' => $test_info]);
$user_email = Auth::user()->email;
Mail::to($user_email)->send(new PdfTest($test));
return $pdf->stream('document.pdf');
}
I want to send the PDF to email and also open with a click of button. I also have this code for email, in the folder Mail
in a file I have this code:
public $test;
public function __construct(Test $test) {
$this->test = $test;
}
/**
* Build the message.
*
* @return $this
*/
public function build() {
return $this->view('website.tests_pdf.take-test');
}
Can anyone please help me how to I do this?
You can try this (I've added comments)
public function fun_pdf($test_id) {
$test = Test::where('id', $test_id)->first();
$questions = (new TestQuestionsController)->questionwithanswers($test_id, $randomorder = 1);
$test_info = (new TestInfoController)->testInfo($test_id);
$pdf = PDF::loadView('website.tests_pdf.take-test', ['test_id' => $test_id, 'questions' => $questions, 'test' => $test, 'test_info' => $test_info]);
$user_email = Auth::user()->email;
// output pdf as a string, so you can attach it to the email
$pdfHtml = $pdf->output();
// pass pdf string
Mail::to($user_email)->send(new PdfTest($test, $pdfHtml));
return $pdf->stream('document.pdf');
}
From barryvdh/laravel-dompdf readme
If you need the output as a string, you can get the rendered PDF with the output() function, so you can save/output it yourself.
To attach the pdf to email look into Raw Data Attachments in laravel mail documentation
namespace App\Mail;
use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class PdfTest extends Mailable
{
use Queueable, SerializesModels;
public $test;
public $pdfHtml;
public function __construct(Test $test, $pdfHtml) {
$this->test = $test;
$this->pdfHtml = $pdfHtml;
}
/**
* Build the message.
*
* @return $this
*/
public function build() {
return $this->view('website.tests_pdf.take-test')
// attach the pdf to email
->attachData($this->pdfHtml, 'name.pdf', [
'mime' => 'application/pdf',
]);;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.