使用PHP通过邮件发送PDF文件

时间:2013-01-23 09:40:47

标签: php pdf pdf-generation pdflib

我有以下PHP代码:

<?php
if (!empty($_POST)) {

// Used for later to determine result
$success = $error = false;

// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;

// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
    $post->$key = trim(strip_tags($_POST[$key]));

// Check for blank fields
if (empty($post->subject) OR empty($post->email) OR empty($post->about))
    $error = true;

else {

    // Get this directory, to include other files from
    $dir = dirname(__FILE__);

    // Get the contents of the pdf into a variable for later
    //require($dir.'/pdf.php');
    ob_start();
    //$html = file_get_contents('../pdfFile.html');
    require_once('../pdfFile.html');
    $pdf_html = ob_get_contents();
    ob_end_clean();     

     // Load the dompdf files  
    require_once($dir.'/dompdf/dompdf_config.inc.php');  

    $dompdf = new DOMPDF(); // Create new instance of dompdf  
    $dompdf->load_html($pdf_html); // Load the html  
    $dompdf->render(); // Parse the html, convert to PDF  
    $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later  

    // Get the contents of the HTML email into a variable for later
    ob_start();
    require_once($dir.'/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();

    // Load the SwiftMailer files
    require_once($dir.'/swift/swift_required.php');

    $mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer

    $message = Swift_Message::newInstance()
                   ->setSubject(array($post->subject)) // Message subject
                   ->setTo(array($post->email => $post->subject)) // Array of people to send to
                   ->setFrom(array('maly.nu_cute@ymail.com' => 'Maly')) // From:
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                   ->attach(Swift_Attachment::newInstance($pdf_content,'sejour.pdf', 'application/pdf')); // Attach the generated PDF from earlier

    // Send the email, and show user message
    if ($mailer->send($message))
        $success = true;
    else
        $error = true;

}

} ?&GT;

事实上,我收到了错误消息:Fatal error: Uncaught exception 'PDFlibException' with message 'Handle parameter or option of type 'image' has bad value 0' in /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/pdflib_adapter.cls.php:662 Stack trace: #0 /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/pdflib_adapter.cls.php(662): PDFlib->fit_image(0, 231, 752, 'boxsize={150 30...') #1 /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/php_evaluator.cls.php(66) : eval()'d code(23): PDFLib_Adapter->image('images/logo.png', 'png', 231, 10, 150, 30) #2 /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/php_evaluator.cls.php(66): eval() #3 /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/php_evaluator.cls.php(70): PHP_Evaluator->evaluate('?if ( isset($pd...') #4 /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/renderer.cls.php(180): PHP_Evaluator->render(Object(Null_Frame_Decorator)) #5 /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/renderer.cls.php(120): Renderer->_render_frame(' in /homez.537/archigra/www/arcancianev/pdf_email/dompdf/include/pdflib_adapter.cls.php on line 662

我使用此代码生成pdf并将其发送到邮件以供用户使用,但是它收到了如上所述的错误消息。 我不知道它来自哪里?任何人都请帮助我,谢谢。

2 个答案:

答案 0 :(得分:1)

或试试这个

<?php
// download fpdf class (http://fpdf.org)
require("/fpd/fpdf.php");
// fpdf object
$pdf = new FPDF();
// generate a simple PDF (for more info, see http://fpdf.org/en/tutorial/)
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(40,10, "this is a pdf example");
// email stuff (change data below)
$to = "target@domain.com";
$from = "me@domain.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "example.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
// send message
mail($to, $subject, "", $headers);
?>

答案 1 :(得分:0)

使用此代码可以正常工作

<?php 
    $content="<html>html content here</html>"  ;    
    $html2pdf = Yii::app()->ePdf->HTML2PDF();
    $html2pdf->WriteHTML($content);
    $to = "dheerajchouhan85@gmail.com";
    $from = "no-reply@email.com";
    $subject = "Thank you for your Contribution";
    $message = "<p>Your Message</p>";

    $separator = md5(time());
    $eol = PHP_EOL;
    $filename = "example.pdf";
    $pdfdoc = $html2pdf->Output('', 'S');
    $attachment = chunk_split(base64_encode($pdfdoc));
    $headers = "From: " . $from . $eol;
    $headers .= "MIME-Version: 1.0" . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;

    $body .= "Content-Transfer-Encoding: 7bit" . $eol;
    $body .= "This is a MIME encoded message." . $eol; 
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
    $body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
    $body .= $message . $eol;
    $body .= "--" . $separator . $eol;
    $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
    $body .= "Content-Transfer-Encoding: base64" . $eol;
    $body .= "Content-Disposition: attachment" . $eol . $eol;
    $body .= $attachment . $eol;
    $body .= "--" . $separator . "--";
    mail($to, $subject, $body, $headers);
?>