简体   繁体   中英

How to send blade as attachment in pdf format in laravel?

I want to send blade file as attachment in mail with laravel. I am writing below code for this but its not working.

Below is my controller where i am getting data from form and then send this data to another controller where my attachment function is called.

$data = array('shareholders'=>$request->com_shareholder_count,'contract_send'=>$request->contract_send);
$to = $mail_log->to_email_id = $request->email_id;
$mail = Mail::to($to)->send(new SendMailable($data));

This is my SendMailable controller :

$director_info_pdf = view('directors_info',compact('data'))->render();

On return this variable it shows me error :

message: "Invalid view.", exception: "InvalidArgumentException",…}
exception: "InvalidArgumentException"
file: "C:\xampp\htdocs\caps_admin\vendor\laravel\framework\src\Illuminate\Mail\Mailer.php"
line: 285
message: "Invalid view."

After this line i am writing code to attach my files. where i am sending some files other 3 files are directly send from folder. And last one is attached from blade file.

->attachdata($director_info_pdf, 'dynamic_data.pdf')
        ->attach( $public_path.'/'.'contract.pdf', [
                        'as' => 'contract.pdf',
                        'mime' => 'application/pdf',
                    ])
        ->attach($public_path.'/'.'HMRC.pdf',[
                        'as' => 'HMRC.pdf',
                        'mime' => 'application/pdf',
                    ])
         ->attach($public_path.'/'.'clientR3.pdf',[
                        'as' => 'contract1.pdf',
                        'mime' => 'application/pdf',
                    ]);

I am able to send mail with all 4 files as attachment. But when i am trying to open my files in mail rest 3 files are working as pdf. but ->attachdata($director_info_pdf, 'dynamic_data.pdf') this file get corrupted.

I dont know how to first change this file into pdf and then send as attachment. I am using snappy for pdf.

I think you want to send pdf as an attachment in your email without saving it in your system .

your can refer the following code to do this .

public function sendmail()
{
        $data["email"]='useremail@gmail.com';
        $data["client_name"]='user_name';
        $data["subject"]='sending pdf as attachment';

        //Generating PDF with all the post details

        $pdf = PDF::loadView('userdata');  // this view file will be converted to PDF

        //Sending Mail to the corresponding user

        Mail::send([], $data, function($message)use($data,$pdf) {
        $message->to($data["email"], $data["client_name"])
        ->subject($data["subject"])
        ->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
                   'mime' => 'application/pdf',
               ])
        ->setBody('Hi, welcome user! this is the body of the mail');
        });
}

hope it helped you .

for better understanding you can follow this article

how to send pdf as an attachment in email in laravel

I dont know how to first change this file into pdf and then send as attachment. I am using snappy for pdf.

So that's the problem right there. You are attaching an HTML file, and simply naming it as if it's a PDF. That won't work. You do indeed need to generate this PDF from the HTML view that you render.

Here is the documentation for snappy:

https://github.com/barryvdh/laravel-snappy#usage

I suggest you to read up on it, as the examples given there are pretty straight forward and easy to understand.

This is untested, but if you've set up Snappy correctly , based on your code, something like this should work:

$snappy = App::make('snappy.pdf');
$director_info_pdf = $snappy->getOutputFromHtml(view('directors_info',compact('data'))->render());

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM