2011-05-06 238 views
5

我正在查看用戶輸入一些信息到表單中的能力,並通過mail()發送。創建PDF並通過電子郵件發送

我想要的是將詳細信息作爲PDF附件發送。可能與發件人的姓名和日期/時間有關。 test_user_06052011.pdf

我有一個PDF的設計,但我不知道如何將此設計集成在PHP中創建一個PDf。

有沒有人有我可以做到這一點的例子或方式?

+2

http://stackoverflow.com/questions/2132015/best-way-to的可能的複製-create-a-pdf-with-php – 2011-05-06 08:23:29

+1

歡迎來到SO,sipher_z!請停止在問題標題中撰寫標籤。你已經完成了11個問題中的大部分。 >。< – 2011-05-06 08:28:48

+0

@Tomalak Geret'kal對標題中的標籤表示歉意。我會確保我今後不會這樣做 – 2011-05-06 08:32:51

回答

1

看一看到FPDF - http://www.fpdf.org/ - 它是免費的,併產生一個偉大的工具PDF的

有是由PHP recomended一個PDF生成,但它會非常昂貴,但名字現在elludes我,我幾次使用過FPDF,並取得了巨大的成功。

4

創建PDF服務器站點的非常簡單的方法是使用wkhtmltopdf。但是,您需要通過shell訪問服務器才能進行設置。

要創建PDF,您需要兩個文件:一個是生成要轉換爲PDF的HTML的PHP​​。比方說,這是invoice.php:

<?php 
    $id = (int) $_GET['id']; 
?> 
<h1>This is invoice <?= $id ?></h1> 
<p>some content...</p> 

而另外一個,這將獲取發票和使用wkhtmltopdf它轉換成PDF:

<?php 

$tempPDF = tempnam('/tmp', 'generated-invoice'); 
$url = 'http://yoursite.xx/invoice.php?id=123'; 

exec("wkhtmltopdf $url $tempPDF"); 

header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename=invoice.pdf'); 

echo file_get_contents($tempPDF); 
unlink($tempPDF); 

一旦你創建一個PDF文件,你也可以發送郵件帶附件這樣:

<?php 

$to = "[email protected]"; 
$subject = "mail with attachment"; 

$att = file_get_contents('generated.pdf'); 
$att = base64_encode($att); 
$att = chunk_split($att); 

$BOUNDARY="anystring"; 

$headers =<<<END 
From: Your Name <[email protected]> 
Content-Type: multipart/mixed; boundary=$BOUNDARY 
END; 

$body =<<<END 
--$BOUNDARY 
Content-Type: text/plain 

See attached file! 

--$BOUNDARY 
Content-Type: application/pdf 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="your-file.pdf" 

$att 
--$BOUNDARY-- 
END; 

mail($to, $subject, $body, $headers); 
1

根據您的要求,你也可以看看TCPDF,我用了很多對從PHP動態創建PDF文件...這^ h作爲(有限)HTML內置的PDF功能,並且非常易於使用(只需查看示例)。還有一個主要的好處:它仍然處於積極的發展階段(對於某些人來說可能過於活躍:p)。

+0

TCPDF爲我做了這份工作 - 謝謝! – 2017-09-12 02:09:14

3

如果您使用Acrobat製作了Fillable PDF,以下是一大段幫助您入門的代碼。這段代碼需要最新版本的phpmailer才能正常工作,所以只需下載它並將其放入同一目錄中的類文件夾中,然後將此代碼提交到包含此代碼的頁面。

/* Branden Sueper 2012 
// PDF to Email - PHP 5 
// Includes: PHPMailer 5.2.1 
*/ 
<?php 
if(!isset($HTTP_RAW_POST_DATA)) { 
    echo "The Application could not be sent. Please save the PDF and email it manually."; 
    exit; 
} 
echo "<html><head></head><body><img src='loading.gif'>"; 

//Create PDF file with data 
$semi_rand = md5(time()); 
$pdf = $HTTP_RAW_POST_DATA; 

$file = $semi_rand . ".pdf"; 
$handle = fopen($file, 'w+'); 
fwrite($handle, $pdf); 
fclose($handle); 
// 

require_once('class/class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

$mail = new PHPMailer(false); // the true param means it will throw exceptions on errors, which we need to catch 

$mail->IsSMTP(); // telling the class to use SMTP 

try { 
    $mail->Host  = "mail.xxxxxxx.com"; // SMTP server 
    $mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
    $mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
    $mail->Port  = 465;     // set the SMTP port for the GMAIL server 
    $mail->Username = "[email protected]"; // GMAIL username 
    $mail->Password = "xxxxxxxx";   // GMAIL password 
    $mail->AddAddress('[email protected]', 'First Last'); 
    $mail->SetFrom('[email protected]', 'First Last'); 
    $mail->Subject = 'Your Subject'; 
    $mail->Body = 'Hello!'; 
    $mail->AddAttachment($file); // attachment 
    $mail->Send(); 

    //Delete the temp pdf file then redirect to the success page 
    unlink($file); 
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL="success.php">';  
    exit;  
} catch (phpmailerException $e) { 
    //you can either report the errors here or redirect them to an error page 
    //using the above META tag 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
} 
    //Verify the temporary pdf file got deleted 
    unlink($file); 
?> 

PHPMailer只需下載PHP郵件和上面的代碼調整自己的喜好。有關如何創建可填寫PDF的更多信息,請轉至http://www.adobe.com/products/acrobatpro/create-fillable-pdf-forms.html

祝你好運!我記得花了3天以上試圖找出一個非常類似的問題!

1

今天找不到原生mail()功能的原因。在大多數微不足道的情況下,我們可以使用PHPMailer庫,使用OOP風格,即使不理解標題,我們也有機會發送電子郵件。 不保存物理文件該解決方案甚至

$mail = new PHPMailer(); 
... 
$doc = $pdf->Output('S'); 
$mail->AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf'); 
$mail->Send(); 
1

下面是一個完整的代碼http://codexhelp.blogspot.in/2017/04/php-email-create-pdf-and-send-with.html

/**/ 
    $mailto = $_POST['mailto']; 
    $mailfrom = $_POST['mailfrom']; 
    $mailsubject = $_POST['mailsubject']; 
    $firstname = $_POST['firstname']; 
    $lastname = $_POST['lastname']; 
    $description = $_POST['description']; 


    $description = wordwrap($description, 100, "<br />"); 
    /* break description content every after 100 character. */ 


    $content = ''; 

    $content .= ' 
<style> 
table { 
border-collapse: collapse; 
} 

table{ 
width:800px; 
margin:0 auto; 
} 

td{ 
border: 1px solid #e2e2e2; 
padding: 10px; 
max-width:520px; 
word-wrap: break-word; 
} 


</style> 

'; 
    /* you css */ 



    $content .= '<table>'; 

    $content .= '<tr><td>Mail To</td> <td>' . $mailto . '</td> </tr>'; 
    $content .= '<tr><td>Mail From</td> <td>' . $mailfrom . '</td> </tr>'; 
    $content .= '<tr><td>Mail Subject</td> <td>' . $mailsubject . '</td> </tr>'; 
    $content .= '<tr><td>Firstname</td> <td>' . $firstname . '</td> </tr>'; 
    $content .= '<tr><td>Lastname</td> <td>' . $lastname . '</td> </tr>'; 
    $content .= '<tr><td>Description</td> <td>' . $description . '</td> </tr>'; 

    $content .= '</table>'; 


    require_once('html2pdf/html2pdf.class.php'); 


    $to = $mailto; 
    $from = $mailfrom; 
    $subject = $mailsubject; 

    $html2pdf = new HTML2PDF('P', 'A4', 'fr'); 

    $html2pdf->setDefaultFont('Arial'); 
    $html2pdf->writeHTML($content, isset($_GET['vuehtml'])); 

    $html2pdf = new HTML2PDF('P', 'A4', 'fr'); 
    $html2pdf->WriteHTML($content); 


    $message = "<p>Please see the attachment.</p>"; 
    $separator = md5(time()); 
    $eol = PHP_EOL; 
    $filename = "pdf-document.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 = ''; 

    $body .= "Content-Transfer-Encoding: 7bit" . $eol; 
    $body .= "This is a MIME encoded message." . $eol; //had one more .$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 . "--"; 

    if (mail($to, $subject, $body, $headers)) { 

     $msgsuccess = 'Mail Send Successfully'; 
    } else { 

     $msgerror = 'Main not send'; 
    }