Welcome Guest, Not a member yet? Register   Sign In
Email a FPDF attachment
#1

[eluser]ridley1012[/eluser]
Hi has anyone managed to send an email using the codeigniter email libraries which attaches a FPDF pdf document created on the fly. I've written my code to create the pdf document which when outputting to screen displays correctly, the code is as follows:

Code:
$invoice_index = $this->uri->segment(4);
$this->load->model('invoices_model', 'invoices');
$invoice = $this->invoices->get_invoice_details($invoice_index);
$invoice_items = $this->invoices->get_invoice_items($invoice_index);
$invoice_total = $this->invoices->get_invoice_total($invoice_index);
$this->load->library('fpdf');
  
$this->fpdf->Open();
$this->fpdf->SetLeftMargin(5);
$this->fpdf->AddPage();
$this->fpdf->SetFont('Arial','',10);
$this->fpdf->Image(base_url('/assets/images/pdf_logo.jpg'),54,10,102,15);
if(!empty($invoice->name)) {$name = $invoice->name;} else {$name =$invoice->title . " " . $invoice->firstname . " " . $invoice->surname;}
$this->fpdf->SetY('35');
$this->fpdf->Cell(70,6, $name,0,0, 'L');
$this->fpdf->Cell(105,6,'Invoice Number:',0,0, 'R');
$this->fpdf->Cell(25,6,$invoice_index,0,1, 'L');

if($invoice->type == 'Tenant')
{
$this->fpdf->Cell(70,6, $invoice->add_line_1,0,0, 'L');
}
else
{
$this->fpdf->Cell(70,6, $invoice->address_line_1,0,0, 'L');
}
$this->fpdf->Cell(105,6,'Invoice Date:',0,0, 'R');
$this->fpdf->Cell(25,6, timestamp_to_date($invoice->invoice_date),0,1, 'L');

if($invoice->type == 'Tenant')
{
$this->fpdf->Cell(70,6,$invoice->add_line_2,0,0, 'L');
}
else
{
$this->fpdf->Cell(70,6,$invoice->address_line_2,0,0, 'L');
}
$this->fpdf->Cell(105,6,'Terms:',0,0, 'R');
$this->fpdf->Cell(25,6,$invoice->terms . ' Days',0,1, 'L');

if($invoice->type == 'Tenant')
{
$this->fpdf->Cell(70,6,$invoice->propcounty,0,0, 'L');
}
else
{
$this->fpdf->Cell(70,6,$invoice->county,0,0, 'L');
}
$this->fpdf->Cell(105,6,'',0,0, 'R');
$this->fpdf->Cell(25,6,'',0,1, 'L');

if($invoice->type == 'Tenant')
{  
$this->fpdf->Cell(70,6,$invoice->proppost,0,0, 'L');
}
else
{
$this->fpdf->Cell(70,6,$invoice->postcode,0,0, 'L');
}
$this->fpdf->Cell(105,6,'',0,0, 'R');
$this->fpdf->Cell(25,6,'',0,1, 'L');

$this->fpdf->SetFont('Arial','B',18);
$this->fpdf->Cell(200,12,'INVOICE',0,1, 'C');

$this->fpdf->SetFont('Arial','B',10);

$this->fpdf->Cell(150,6,'Description',1,0, 'C');
$this->fpdf->Cell(50,6,'Total',1,1, 'C');

if($invoice->type == 'Landlord')
{
$this->fpdf->Cell(200,6, $invoice->address_line_1,0,1, 'L');
}
else
{
$this->fpdf->Cell(200,6,'',0,1, 'L');
}
$this->fpdf->SetFont('Arial','',8);

foreach($invoice_items as $items)
{
$this->fpdf->Cell(150,6,str_replace('£', '' . substr('£', 1, 1) . '', $items->description),1,0, 'C');
$this->fpdf->Cell(50,6,pdf_money_conversion($items->amount),1,1, 'C');
}
$this->fpdf->SetFont('Arial','b',8);

$this->fpdf->Cell(150,6,'Total:   ', 0,0, 'R');

$this->fpdf->Cell(50,6,pdf_money_conversion($invoice_total->total_amount),1,1, 'C');

$this->fpdf->Cell(200, 20, '', 0, 1, 'C');
$this->fpdf->Cell(200, 6, 'PAYMENT TERMS - STRICTLY '.$invoice->terms.' DAYS', 0, 1, 'C');

$this->load->library('email');

$this->email->from('[email protected]', 'test');
$this->email->to('[email protected]');    
$this->email->subject('test');      
$this->email->message('Please find your invoice attached.');
  $pdfdoc = $this->fpdf->Output("", "S");

$this->email->attach($pdfdoc);  
if($this->email->send())
{
  echo 'Your email was sent, successfully.';
}
  
else
{
  show_error($this->email->print_debugger());
}
}

As you can see, I've attempted to return the pdf as a string and attach it that way (the way I've previously done it with standard php):

Code:
$pdfdoc = $this->fpdf->Output("", "S");
$this->email->attach($pdfdoc);

but this does not work, I'm presuming this has something to do with the way the headers are built within the codeigniter email class but have no clue where to start changing/updating them. Would appreciate any help if someone has managed to overcome this problem.

Thanks
#2

[eluser]vitoco[/eluser]
From the userguide
Code:
$this->email->attach()
recieves a file path to add, and for from the other side ,
Code:
$pdfdoc = $this->fpdf->Output("", "S");
returns the full document as a string . So no chance of add it that way

So first store the pdf somewhere and then add it
Code:
$path_to_pdf = '/path/to/pdf/file.pdf'

$this->fpdf->Output( $path_to_pdf , "F");
$this->email->attach($path_to_pdf);

Slds




Theme © iAndrew 2016 - Forum software by © MyBB