TCPDF 和 PHPMailer PDF 已损坏

TCPDF and PHPMailer PDF is brocken

我在这里和那里阅读了很多,但不知何故我找不到解决方案。
我正在尝试发送一个用 TCPDF 生成的 PDF,作为与 PHPMailer 一起使用的附件。 我一尝试就收到消息: 警告:base64_encode() 要求参数 1 为字符串,第 3179 行 C:\xampp\htdocs\pap KK\Root\phpmailer-master\src\PHPMailer.php 中给出的对象”,但邮件总是发送。不幸的是,PDF 已损坏。它不是空的。只要我使用: $pdf->Output('e-tickets.pdf', 'D'); 它保存了一个完美的 PDF 副本,

求助,我到底哪里做错了... V

这是代码

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

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

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('KK Abt HC');
$pdf->SetTitle('Zuweiserbrief');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData('');

// 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));

//remove header footer
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);

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

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$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', '', 10);

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

// define some HTML content with style
require_once('print.php');

// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');

//Close and output PDF document

//$pdf->Output('no_name.pdf', 'I');
$pdf->Output('e-tickets.pdf', 'S');

//set random filename to pdf
$file_name = md5(rand()) . '.pdf';

//add mailer manually
require '../phpmailer-master/src/Exception.php';
require '../phpmailer-master/src/PHPMailer.php';
require '../phpmailer-master/src/SMTP.php';

//create new PHPMailer class
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$email = new PHPMailer(TRUE);

//create new PHPMailer object
$mail = new PHPMailer();

//SMTP configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '*******'; 
$mail->Password = '*******'; 
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

//PHPMailer headers
$mail->setFrom('mail.1@gmail.com', 'Robot');
$mail->addReplyTo('mail.2@gmail.com', 'Robot');
$mail->addAddress('mustermann@mail.de', 'Mustermann'); 
$mail->addCC('someone@yahoo.com', 'Yahoo');
$mail->addBCC('', '');

//HTML Format
$mail->isHTML(true);
$bodyContent = '<p>Hallo zusammen,</p>';
$bodyContent .= '<p>Im Anhang finden Sie bitte ein Brief</p>';
$bodyContent .= '<p>Mit freundlichen Grüßen</p>';
$bodyContent .= '<p><br></p>';
$bodyContent .= '<p>Das ist einen automatischen Mail</p>';

//Attachment
$mail->AddStringAttachment($pdf, $file_name);

//Subject
$mail->Subject = 'Email from Localhost Testing with YAHOO!';
$mail->Body    = $bodyContent;

//Debugging: 
/*
$mail->SMTPDebug = N; where N is (1) = client; (2) = client and server; (recommended); (3) = client, server, and connection; (4) = low-level information. 
*/

$mail->SMTPDebug = 3;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

这是因为您传递的是 PDF 对象,而不是它的字符串表示形式。

这样做:

$pdfdata = $pdf->Output('e-tickets.pdf', 'S');
$mail->AddStringAttachment($pdfdata, $file_name);