2014-04-11 44 views
1

我可以生成一個PDF查看和輕鬆下載。我正在嘗試發送附有pdf的電子郵件。我瀏覽了幾個小時幾小時的谷歌,沒有任何東西讓我接近這個鏈接,sending an email attachment using TCPDF。提問者在鏈接問題上遇到了同樣的問題。該電子郵件與附件發送很好,但pdf是空的,「出現損壞」根據土坯。發送tcpdf作爲附件使用phpmailer

共識似乎是phpmailer是最好的方式去,所以我已經安裝了phpmailer,我有以下代碼。現在我得到這個錯誤,致命錯誤:調用一個成員函數Output()在一個非對象上,第122行。如果任何人都可以請幫助,我將不勝感激!

<?php 
include ('../../include/connect.php'); 
$action=$_GET['a']; 
/*----------*/ 

// Include the main TCPDF library (search for installation path). 
require_once('tcpdf_include.php'); 


// Extend the TCPDF class to create custom Header and Footer 
class MYPDF extends TCPDF { 

    //Page header 
    public function Header() { 
     // Logo 
     //$image_file = K_PATH_IMAGES.'name_bar.gif'; 
     //$this->Image($image_file, 30, 5, 150, '', 'GIF', '', 'T', false, 300, '', false, false, 0, false, false, false); 
     // Set font 
     $this->SetFont('helvetica', 'B', 20); 
     // Title 
     //$this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M'); 
    } 

    // Page footer 
    public function Footer() { 
     // Position at 15 mm from bottom 
     $this->SetY(-15); 
     // Set font 
     $this->SetFont('helvetica', '', 8); 
     // Page number 
     $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M'); 
    } 
} 

// create new PDF document 
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('TEST'); 
$pdf->SetTitle($title); 

// set default header data 
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); 

// set header and footer fonts 
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 

// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

// set margins 
$pdf->SetMargins(10, 10, 10); 
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); 
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 

// set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 

// set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

// set some language-dependent strings (optional) 
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { 
    require_once(dirname(__FILE__).'/lang/eng.php'); 
    $pdf->setLanguageArray($l); 
} 

// --------------------------------------------------------- 

// set font 
$pdf->SetFont('helvetica', 'B', 20); 

// add a page 
$pdf->AddPage(); 

// set JPEG quality 
$pdf->setJPEGQuality(300); 

// Image method signature: 
// Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false) 

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

// Example of Image from data stream ('') 
$imgdata = base64_decode('iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABlBMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDrEX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg=='); 

// The '@' character is used to indicate that follows an image data stream and not an image file name 
//$pdf->Image('@'.$imgdata); 

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

// Image example with resizing 

//$pdf->Write(0, 'Example of HTML tables', '', 0, 'L', true, 0, false, false, 0); 

$pdf->SetFont('helvetica', '', 8); 

// ----------------------------------------------------------------------------- 
$tbl = <<<EOD 

test 

EOD; 

$pdf->writeHTML($tbl, true, false, false, false, ''); 

// ----------------------------------------------------------------------------- 

//Close and output PDF document 
if($action=='I'){ 
    $pdf->Output($title, 'I'); 
}elseif($action=='D'){ 
    $pdf->Output($title, 'D'); 
} 

//============================================================+ 
// END OF FILE 
//============================================================+ 
if($action=='E'){ 
$attachment = $makepdf->Output($title, 'E'); 
SENDmail($attachment); 

function SENDmail($pdf) { 
require_once('../../include/class.phpmailer.php'); 
$mailer = new PHPMailer(); 

$mailer->AddReplyTo('[email protected]', 'Reply To'); 
$mailer->SetFrom('[email protected]', 'Sent From'); 
$mailer->AddReplyTo('[email protected]', 'Reply To'); 
$mailer->AddAddress('email', 'Send To'); 
$mailer->Subject = 'Message with PDF'; 
$mailer->AltBody = "To view the message, please use an HTML compatible email viewer"; 
$mailer->MsgHTML('<p>Message contents</p>'); 
if ($pdf) {$mailer->AddStringAttachment($pdf, $title);} 

$mailer->Send(); 
} 

} 
?> 

回答

2

The reason you're getting that error is simply because you are referring to $makepdf而不是$pdf

0

存儲PDF作爲字符串:

$attachement = $pdf->Output('hotel4cast.pdf', 'S');

然後用PHPMailer的附加爲:

$mail->AddStringAttachment($attachment, 'filename.pdf', 'base64', 'application/pdf');