-1

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.

Aleksa
  • 1
  • 2

1 Answers1

0

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • I edited my questions code, can you see it now. Do you think it will work like that on server? As i need to do all that on server, not with local file – Aleksa Aug 28 '20 at 21:16
  • Your revised code won’t help. You are still trying to pass the generated PDF as a path when it doesn’t have one. – Synchro Aug 29 '20 at 09:44
  • i made it return a path now, but it still not working on a live server – Aleksa Aug 31 '20 at 21:30
  • You need to define "not working". We have no information to work with here. Check the path that it generates, make sure that it points at a real file, make sure that it contains valid PDF data, and do all that *before* you attempt to send it via email. Debug one thing at a time. – Synchro Sep 01 '20 at 09:46
  • Mail sending works fine. All paths are good, i checked them, and i can reach form.pdf properly, the only problem is fillpdf(+flatten, +saveas) part, it does not generate pdf on a server as it did on my local pc. As if the whole pdftk library is not working unlike PHPmailer library which works fine – Aleksa Sep 01 '20 at 21:10
  • Ok, so it looks like you need to check ownership and permissions of where you’re trying to save it, or switch to generating an in-memory string instead. – Synchro Sep 01 '20 at 22:01