简体   繁体   中英

How to send fillable pdf (filled with pdftk) via PHPMailer?

I have a form on a website that customers fill. When user filles the form and clicks submit data is sent to a pdf(invoice) I've already created and certain spots in pdf are filled with that data. I did this using PDFTK library:

public function generate($data)
    {
        $filename =  date("d-m-Y-His") . ".pdf";

        $pdf = new Pdf('./form.pdf');
        $pdf->fillForm($data)
        ->flatten()
        ->saveAs('./completed/' . $filename);

        $path = './completed/' .$filename;

        return $path;
    }

The problem is, i dont know how to send this filled pdf via PHPMailer library, as pdf is recquired to be a string in order to work with phpmailer.

$pdf = new GeneratePDF;
$response = $pdf->generate($data);

$email = new PHPMailer();
$email->SetFrom('you@example.com', 'Your Name'); 
$email->Subject   = 'Test';
$email->Body      = 'Test';
$email->AddAddress( 'mail exmp' );


$email->AddAttachment( $response , 'NameOfFile.pdf' );

$email->Send();

And how to actually save file in cpanel server, as when i do this it doesnt work on a server.

pdf is recquired to be a string in order to work with phpmailer

This is not true. addAttachment() requires that you pass in a path to a local file on disk, so you could do:

$email->addAttachment(generate(), 'NameOfFile.pdf');

You can pass in a binary string using addStringAttachment() , but that's not what you're asking for here.

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