I am using dompdf to generate a pdf and I am taking the raw string, encoding it for saving it to the database later and then decoding it so I can send it as a attachment for the email, but I cannot do it anyhow. I tried both the examples here PHP Send email with PDF attachment without creating the file? and PHP get pdf file from base64 encoded data string but I could not get it working.
So here is the code
public function generateCreditAggreement(){
$this->load->helper('file');
require_once(APPPATH.'third_party/dompdf/dompdf_config.inc.php');
$dompdf = new Dompdf();
$msg = $this->load->view('credit_agreement_view', '', true);
$html = mb_convert_encoding($msg, 'HTML-ENTITIES', 'UTF-8');
$dompdf->load_html($html);
// $dompdf->set_paper('A4', 'Potrait');
$paper_orientation = 'Potrait';
$dompdf->set_paper($paper_orientation);
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
// $dompdf->stream('document.pdf');
// $id = "sup";
// $dompdf->stream("$id.pdf", array("Attachment" => false));
$pdf_string = $dompdf->output();
$new_pdf = write_file('name', $pdf_string);
$pdf_content = base64_encode($pdf_string);
// $this->load->model('agreement_page');
// $pushString = $this->agreement_page->storePdfString($pdf_content);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ('credit_agreement.pdf','w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
// sending the pdf to email
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'support@aurorax.co';
$config['smtp_pass'] = '';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not
$this->email->initialize($config);
$this->email->from('support@aurorax.co', 'aurora exchange');
$this->email->to('masnad@aurorax.co');
$this->email->subject('pdf');
$this->email->attach('myfile.pdf' , $pdf,'application/pdf');
$this->email->message('Your pdf is here');
// $this->email->attach($pdf);
$this->email->send();
}
Struggling for a 2 days now, cant get it working.