Search code examples
phpcodeigniterhtml2pdf

Email a PDF generated with html2pdf from a dynamically generated page


So I work with codeigniter and I generate a page.
I want to send a mail with the page attached as PDF but I also want not to save it on the server.
I found 2 code resources that look like doing exactly what I need:

<?php
    $content = "
<page>
    <h1>Exemple d'utilisation</h1>
    <br>
    Ceci est un <b>exemple d'utilisation</b>
    de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";

    require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
    $html2pdf = new HTML2PDF('P','A4','fr');
    $html2pdf->WriteHTML($content);
    $html2pdf->Output('exemple.pdf');
?>

For creating a PDF using html2pdf library

<?php 
require_once (dirname)(__FILE__).'/html2pdf/html2pdf.class.php');
require_once (dirname)(__FILE__).'/pjmail/pjmail.class.php');
?>
<?php 
$pdf = new HTML2PDF ('P', 'A4');
$pdf->WriteHTML($content);
$content_pdf = $pdf->Output('document.pdf', true);

$mail = new PJmail();
mail->setAllFrom('webmaster@my_site.net',  "My Site");
$mail->addrecipient('mymail@my_site.net');
$mail->addsubject("test");
$mail->text"Insert some text here...";
$mail->addbinattachement('test.pdf', $content_pdf);
echo $mail->sendmail();
?>

For sending the email with a PDF attachment.

Now I have to say that I am a beginner in PHP and I KIND OF understand what the code does. I don't know how to pass the link to the $content variable.
Someone told me that I should use a function like curl or file_get_contents but it is not clear to me at all. Could someone explain me a little?


Solution

  • I send you code with dom pdf in which email send with pdf and after sent email pdf delete

        ob_start();
    set_include_path(get_include_path() . PATH_SEPARATOR . "/path/to/dompdf");
    require_once APPPATH . "core/dom_pdf/dompdf_config.inc.php";
    
    
    $dompdf = new DOMPDF();
    $dompdf->set_paper(DEFAULT_PDF_PAPER_SIZE, 'A4');
    $dompdf->load_html($message);
    
    $dompdf->render();
    
    $output = $dompdf->output();
    file_put_contents('pdf_name.pdf', $output);
    
    $this->load->library('email');
    $config = array (
    'mailtype' => 'html',
    'charset'  => 'utf-8',
    'priority' => '1'
    );
    $this->email->initialize($config);
    
    $this->email->from('admin@admin.com', 'Admin');
    $this->email->to(Reciever email); 
    $this->email->subject('Subject');
    $this->email->message($message); 
    
    $this->email->attach(FCPATH."pdf_name.pdf");
    $this->email->send();
       <!-- If you want to delete file after send email --> 
    unlink(FCPATH."pdf_name.pdf");