2017-10-19 118 views
0

PDF生成包barryvdh/laravel-dompdf,PDF工作正常。我有這樣的代碼:打開PDF併發送電子郵件附件只需點擊一下

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'); 
} 

我想發送PDF到電子郵件,也可以點擊按鈕打開。我也有一個電子郵件的代碼,在一個文件夾中Mail我有這樣的代碼:

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'); 
} 

誰能請幫助我如何我做到這一點?

+0

你嘗試過這麼遠嗎? –

回答

0

你可以試試這個(我已經添加了評論)

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'); 
} 

barryvdh/laravel-dompdf readme

如果需要輸出作爲一個字符串,可以與輸出得到渲染PDF()功能,所以你可以自己保存/輸出。

要安裝PDF格式的電子郵件看看原始數據附件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', 
        ]);; 
    } 
}