2

I am trying to create a pdf document using mpdf and in the first step I tried to save it in my downloads folder and then I tried to open it and it worked perfect. I can see what I need in the pdf document.

In the next step I tried to send the pdf as an attachment and the email was sent successfully with the pdf attachment but when I tried to open it from the mail its returning me an error something as "Adobe cannot open the pdf document because it is neither a supported file type or the file is damaged.......".

I searched for different questions in stackoverflow and I tried their valuable answers too but none of them helped me.

The below is the same question but the answer posted there too didn't helped me.

Stackoverflow questions related to my post

Few Links Which I followed

Here's my code:

 $mpdf=new mPDF( ); 
 $mpdf->SetDisplayMode('fullpage');
 $mpdf->list_indent_first_level = 0;    
 $mpdf->WriteHTML($html,2);

 ob_clean(); // **Tried this from Stackoverflow answers**

 $mpdf->Output($filename.'.pdf', 'I'); 

 $mailto   = $row['Email1'].",".$row['Email5'];
 $path  =  'C:\\Users\\Downloads\\';
 $file  = $path."/".$filename;

 $subject   = 'Test Email';
 $message   = 'Test <br> Case';

 $content = file_get_contents($file);
 $content = chunk_split(base64_encode($content));

 $separator = md5(time());

 $eol = PHP_EOL;

 $headers = "From: John <john.xxxxx@gmail.com>".$eol;
 $headers .= "MIME-Version: 1.0".$eol;
 $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"".$eol.$eol;         

 // message
 $body .= "Content-Transfer-Encoding: 7bit".$eol;
 $body .= "This is a MIME encoded message.".$eol;
 $body = "--" . $separator.$eol;
 $body .= "Content-Type: text/html; charset=\"UTF-8\"".$eol;
 $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
 $body .= $message.$eol;

 // attachment
 $body .= "--" . $separator.$eol;
 $body .= "Content-Type: application/octet-stream; name=\"" . $filename . '.pdf' . "\"".$eol;
 $body .= "Content-Transfer-Encoding: base64".$eol;
 $body .= "Content-Disposition: attachment".$eol.$eol ;
 $body .= $content.$eol;
 $body .= "--" . $separator . "--";

 //SEND Mail
 if (mail($mailto, $subject, $body, $headers)) {
 //echo "mail send ... OK"; // or use booleans here
 } 
John
  • 565
  • 8
  • 23
  • Have you checked your error logs? – Epodax Apr 11 '18 at 15:14
  • Try opening the faulty PDF with a text editor, maybe there's an error message or other hint about what went wrong inside. – Cobra_Fast Apr 11 '18 at 15:15
  • @Cobra_Fast I tried to open the pdf document from mail in text editor and it is showing me empty (blank) – John Apr 11 '18 at 15:16
  • Any help on this please? – John Apr 12 '18 at 07:25
  • 1
    Start by using a proper mailer solution (like PHPMailer or SwiftMailer), instead of trying to assemble this manually. Those have methods to attach files implemented already, so that you don’t have to do this yourself. – CBroe Apr 16 '18 at 14:53

1 Answers1

1

Have you tried changing the mime-type you set for the attachment? The standard mime-type for pdf files is:

application/pdf

I noticed you are trying to load a file from local file system. Does the script run on the same machine? Or on a webserver?

  • If it runs on a webserver you might need to upload the file to the webserver first. You'll need to change the $path and $file variables once uploaded.

    $path  =  'www/images/[some-path]';
    $file  = '/'.$filename;
    
  • If it runs on the same machine as where the file is located, you might need to update the $file variable so it uses a backslash \ instead of the forward slash.

    $path  =  'C:\\Users\\Downloads\\';
    $file  = $path.'\\'.$filename;
    

Another error in your script: You never set the mpdf contents as the content for the email attachment. Try something like:

$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'I')));

You'll have to check if the I in the Output() function of mpdf is correct for this use. I'm not sure of that.

EDIT

// First save it to the server somewhere 
$mpdf->Output($filename.'.pdf', 'F'); // Make sure the path is valid and writing permissions are set correctly to prevent writing errors.

// Then get the contents from the PDF
$content = chunk_split(base64_encode($mpdf->Output($filename.'.pdf', 'S')));
Brainfeeder
  • 2,604
  • 2
  • 19
  • 37
  • Please check updated answer for other possible solutions – Brainfeeder Apr 16 '18 at 14:50
  • Yes, 1) I am working on the webserver now. I got it corrected. The main reason is because of the path. 2) I tried use it with "F" in the output it's showing me "Unable to create the pdf file in [Path]". When I try it with "S" the mail is sent with the attachment file (Perfect for me) and it is not saved (Will have to look on it). – John Apr 17 '18 at 13:25
  • Glad I could help. For the `$mpdf->Output()`, I think it is fine to first save it to the webserver and then use the same `$mpdf` object to attach it to an email. At [the docs](https://mpdf.github.io/reference/mpdf-functions/output.html#parameters) you'll need the `::FILE` and `::STRING_RETURN` options. The docs however only mention the new way of doing this. I'm guessing F is the `::FILE` and S is the `::STRING_RETURN` option. I'll add to my answer. – Brainfeeder Apr 17 '18 at 13:40