2011-05-06 12 views
5

मैं उपयोगकर्ताओं के लिए एक फॉर्म में कुछ जानकारी दर्ज करने और इसे mail() के माध्यम से भेजने के लिए ablity देख रहा हूं।एक पीडीएफ बनाना और ईमेल द्वारा भेजना

मुझे जो चाहिए वह पीडीएफ अटैचमेंट के रूप में भेजे जाने वाले विवरणों के लिए है। संभवतः प्रेषक के नाम और दिनांक/समय के साथ। test_user_06052011.pdf

मेरे पास पीडीएफ के लिए एक डिज़ाइन है, लेकिन मुझे यकीन नहीं है कि मैं PHP में पीडीएफ बनाने के लिए इस डिज़ाइन को कैसे एकीकृत करूं।

क्या किसी के पास ऐसे उदाहरण या तरीके हैं जिनमें मैं यह कर सकता हूं? http://www.fpdf.org/ - -

+2

http://stackoverflow.com/questions/2132015/best-way-to की संभावित डुप्लिकेट है -create-a-pdf-with-php –

+1

SO, sipher_z में आपका स्वागत है! प्रश्न शीर्षक में टैग लिखना बंद करो। आपने इसे अपने 11 प्रश्नों के लिए किया है। >< –

+0

@ शीर्षक में टैग के लिए टोमालक गेरेकल की माफ़ी। मैं सुनिश्चित करूँगा कि मैं भविष्य में ऐसा नहीं करता –

उत्तर

1

FPDF में एक नज़र यह मुफ़्त है और पैदा करने के लिए एक महान उपकरण पीडीएफ

वहाँ एक पीडीएफ जनरेटर जो PHP द्वारा अनुशंसा की जाती है, फिर भी बहुत महंगा यह चाहते है और नाम मुझे अब elludes हालांकि, मैंने बड़ी सफलता के साथ कई बार एफपीडीएफ का उपयोग किया है।

4

पीडीएफ सर्वर साइट बनाने का बहुत आसान तरीका wkhtmltopdf का उपयोग कर रहा है। हालांकि, इसे सेट अप करने के लिए आपको सर्वर तक एक खोल पहुंच की आवश्यकता होगी।

पीडीएफ बनाने के लिए आपको दो फाइलों की आवश्यकता है: एक PHP है जो HTML उत्पन्न करता है जिसे आप पीडीएफ में कनवर्ट करना चाहते हैं। मान लें कि इस invoice.php है:

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

और एक दूसरे को, जो चालान लाने और इसे पीडीएफ में कन्वर्ट wkhtmltopdf का उपयोग कर देगा:

<?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 से मक्खी पर पीडीएफ़ बनाने के लिए ... यह ज के रूप में (सीमित) एचटीएमएल पीडीएफ कार्यक्षमता अंतर्निहित और उपयोग करने में बहुत आसान है (बस उदाहरणों को देखें)। और एक और बड़ा लाभ: यह अभी भी सक्रिय विकास में है (शायद कुछ के लिए थोड़ा सक्रिय: पी)।

+0

टीसीपीडीएफ ने मेरे लिए नौकरी की - धन्यवाद! –

3

यदि आपने एक्रोबैट के साथ एक भरने योग्य पीडीएफ बनाया है, तो यहां कोड का एक अच्छा हिस्सा है जो आपको प्रारंभ करने में मदद करेगा। इस कोड को 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 बस पीएचपी मेलर डाउनलोड करने और अपने पसंद के हिसाब से ऊपर कोड समायोजित करें। एक भरने योग्य पीडीएफ बनाने के तरीके के बारे में अधिक जानकारी के लिए http://www.adobe.com/products/acrobatpro/create-fillable-pdf-forms.html

शुभकामनाएं! मुझे याद है कि 3+ दिन एक बहुत ही समान समस्या का पता लगाने की कोशिश कर रहे हैं!

1

आज देशी mail() फ़ंक्शन का उपयोग करने के कारण नहीं मिल सकता है। ज्यादातर छोटी परिस्थितियों में हम PHPMailer लाइब्रेरी का उपयोग कर सकते हैं, जो ओओपी शैली में हमें हेडर की समझ के बिना भी ईमेल भेजने का मौका देता है। समाधान भी भौतिक फ़ाइल सहेजे बिना

$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'; 
    }