To send an email with a PDF attachment generated from HTML using the html2pdf library in PrestaShop, you can follow these steps:

  1. Generate the PDF using html2pdf: First, you need to generate the PDF from the HTML content using the html2pdf library. Make sure you have already installed and configured the library in your PrestaShop project.

  2. Save the PDF to a File: Once you have generated the PDF using html2pdf, save it to a temporary file on the server. You can use PHP's file_put_contents() function to save the PDF content to a file.

  3. Attach the PDF to the Email: Next, you can use PrestaShop's built-in email sending functions to send the email with the PDF as an attachment. You can use the Mail::send() method to send the email and attach the PDF file using the addAttachment() method.

Here's an example code snippet of how you can achieve this in PrestaShop:

php
use Html2Pdf\Html2Pdf; use Mail; // ... Your PrestaShop controller or script code ... // Generate the PDF using html2pdf $htmlContent = '<html><body><h1>Hello, this is a PDF generated from HTML using html2pdf!</h1></body></html>'; $html2pdf = new Html2Pdf(); $html2pdf->writeHTML($htmlContent); $pdfContent = $html2pdf->output('', 'S'); // Save the PDF to a temporary file $filename = 'my_generated_pdf.pdf'; $file_path = _PS_UPLOAD_DIR_ . $filename; file_put_contents($file_path, $pdfContent); // Send the email with the PDF attachment $to = 'recipient@example.com'; $subject = 'PDF Attachment from PrestaShop'; $message = 'Please find the attached PDF.'; $from = 'sender@example.com'; // Use PrestaShop's Mail::send() to send the email and attach the PDF Mail::send( (int)Configuration::get('PS_LANG_DEFAULT'), 'email_template_name', // Replace with your email template name $subject, array('{pdf}' => $file_path), // Add the PDF file path as a placeholder in your email template $to, null, $from, null, null, null, $file_path // Attach the PDF to the email ); // ... Your PrestaShop controller or script code ...

Make sure to replace email_template_name with the name of your email template that includes the placeholder {pdf} for attaching the PDF file.

With this code, you'll be able to generate a PDF from HTML using html2pdf and send it as an email attachment in PrestaShop. Don't forget to clean up the temporary PDF file after sending the email if it's no longer needed.

Have questions or queries?
Get in Touch