Submit dynamically generated PDF via PHP

I recently created an online job posting template for our site. Everything is done, it is correctly formatted in the browser, it automatically sends bla bla bla to our website.

The last part that I create is to provide the administrator with several options for posting the publication in different places (via email) in a consistent, convenient way. I created a PHP page that creates a PDF document on the fly using the TCPDF library. When downloading pdf.php? Id = X The PDF file is displayed on the page with the contents of the X location. This means that I never save the PDF file on the server, just creating it on the fly every time it is called.

But I want to attach this PDF file to email and send it to various colleges, internal mailing lists, etc. Should I attach pdf.php? id = x to the email, it will not add the PDF file, it will attach what appears to be an empty file with the above name.

Is it possible to associate this with email without storing it on the server?


Below is added based on JM4's answer for further troubleshooting. I included the PDF file in the function and put it in the include file to simplify management.

// random hash necessary to send mixed content $separator = md5(time()); $eol = PHP_EOL; // attachment name $filename = "_Desiredfilename.pdf"; include_once('pdf.php'); // encode data (puts attachment in proper format) $pdfdoc = job_posting_to_pdf($posting_id); $attachment = chunk_split(base64_encode($pdfdoc)); ///////////HEADERS INFORMATION//////////// // main header (multipart mandatory) message $headers = "From: Sender_Name<valid_email@mydomain.com>".$eol; //$headers .= "Bcc: email@domain.com".$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."--"; //Email message if(mail('valid_email@mydomain.com', 'test job posting', 'message body goes here', $headers)) { echo 'mail sent'; } else { echo 'error in email'; } 

Here is a stripped down version of pdf.php:

 function job_posting_to_pdf($job_id) { require_once(ROOT . 'assets/libs/tcpdf/config/lang/eng.php'); require_once(ROOT . 'assets/libs/tcpdf/tcpdf.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(''); $pdf->SetTitle('OPL Job Posting'); $pdf->SetSubject('Job Posting'); $pdf->SetKeywords('TCPDF, PDF, example, test, guide'); // remove default header/footer $pdf->setPrintHeader(false); $pdf->setPrintFooter(false); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); //set margins $pdf->SetMargins(11, PDF_MARGIN_TOP, 11); //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 $pdf->setLanguageArray($l); // --------------------------------------------------------- $pdf->SetFont('times', 'I', 9); $pdf->AddPage(); $left_cell_width = 60; $row_height = 6; $pdf->Image(ROOT . 'assets/gfx/logos/OPL-Logo.jpg', 0, 5, null, 16, null, null, 'N', false, null,'R'); $pdf->Ln('3'); if(!$row['internal']) { $pdf->Cell(0,0,'This position will be posted internally and externally, concurrently.',0,2,'C'); } else { $pdf->Cell(0,0,'Internal posting only.',0,2,'C'); } //Remainder of actual PDF creation removed to keep things simple return $pdf->Output("", "S"); } 
+7
php email pdf pdf-generation tcpdf
source share
6 answers

If I fully understand what you are asking for, it is quite simple. I assume that you already have a PDF file using something like fdpf or tcpdf. In this case, just use the following code:

 <?php // random hash necessary to send mixed content $separator = md5(time()); $eol = PHP_EOL; // attachment name $filename = "_Desiredfilename.pdf"; // encode data (puts attachment in proper format) $pdfdoc = $pdf->Output("", "S"); $attachment = chunk_split(base64_encode($pdfdoc)); ///////////HEADERS INFORMATION//////////// // main header (multipart mandatory) message $headers = "From: Sender_Name<sender@domain.com>".$eol; $headers .= "Bcc: email@domain.com".$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."--"; //Email message mail($emailto, $emailsubject, $emailbody, $headers); ?> 
+5
source share

I just needed to understand this, and my eyeballs were definitely sore until the end ...

1) You need to install PHPMailer on the php server.

2) Include the PHPmailer class in your TCPDF script, for example (your path may differ):

 require_once('../PHPMailer_v5.1/class.phpmailer.php'); 

3) Now, after your pdf code, just talk to PHPMailer as follows:

 $filename = "custompdf_$name_$time.pdf"; $pdf->Output($filename, 'F'); // save the pdf under filename $mail = new PHPMailer(); $mail->IsSMTP(); $mail->Host = "mail.yourhost.com"; $mail->SMTPAuth = true; // enable SMTP authentication $mail->Port = 26; // set the SMTP port for the GMAIL server $mail->Username = "user+yourhost.com"; // SMTP account username $mail->Password = "topsecret"; // SMTP account password $mail->From = "noreply@yourhost.com"; $mail->FromName = "Stack Overflower"; $mail->AddAddress( $email, $name ); // in this case the variable has been passed $mail->AddCC( "person@somehost.net", "Johnny Person"); // in this case we just hard code it $mail->SMTPDebug = 0; // use 2 for debugging the email send $pdf_content = file_get_contents($filename); $mail->WordWrap = 50; $mail->AddStringAttachment($pdf_content, "custompdf_for_$name_$time.pdf", "base64", "application/pdf"); // note second item is name of emailed pdf $mail->IsHTML(true); $mail->Subject = "Your pdf is here"; $mail->Body = "Dear $name,<br> Here is your custom generated pdf generated at $t.<br><br> Thank you"; if(!$mail->Send()) { echo "Sorry ... EMAIL FAILED"; } else { echo "Done. . . Email sent to $email at $t."; } unlink($filename); // this will delete the file off of server 

Of course, you have many options for the sent message, for example, without using html, or sending both html and text, adding many recipients and / or cc, etc.

EDIT: this temporarily saves the file on the server, but it clears the unlink command after itself.

+1
source share

Take a look at this page that discusses advanced email in PHP.

http://articles.sitepoint.com/article/advanced-email-php/5

They take the downloaded file and load the binary data into $ data, but you can just start from there.

0
source share

You can also see how to send it as an attachment via PEAR Mail_Mime . It can accept an attachment as a data string.

The RMail package also looks as if it were doing the same with the stringAttachment class. Youโ€™ll need Google because Iโ€™m a new user and therefore can only send one link at a time.

0
source share

I agree to use the mail library instead of manually creating mime messages using the default mail () function. SwiftMailer is another good open source PHP library. Here is sample code for using dynamic content as an attachment without having to save it to the file system first.

0
source share

Your headers seem a bit:

  • application/octet-stream should become application/octetstream

  • Content-Disposition: attachment .. should become Content-Disposition: attachment; filename="' . basename($filename) . '" Content-Disposition: attachment; filename="' . basename($filename) . '"

Here's what the attachment headers look like:

 // attachment $headers .= "--".$separator.$eol; $headers .= "Content-Type: application/octetstream;".$eol; //Fixed $headers .= "Content-Transfer-Encoding: base64".$eol; $headers .= 'Content-Disposition: attachment; filename="' . basename(filename).'"'.$eol; $headers .= 'Content-ID: <' . basename($filename) . '>' . $eol . $eol //EOL X2 Before $headers .= $attachment; //Run the above in a loop for multiple attachments, after add the final line $headers .= '--' . $separator . '--' . $eol; 

This was taken from one of my working applications, this is a loop if you want to see it:

 foreach ($this->attachments as $attachment) { if (file_exists($attachment['file'])) { $handle = fopen($attachment['file'], 'r'); $content = fread($handle, filesize($attachment['file'])); fclose($handle); $message .= '--' . $boundary . $this->newline; $message .= 'Content-Type: application/octetstream' . $this->newline; $message .= 'Content-Transfer-Encoding: base64' . $this->newline; $message .= 'Content-Disposition: attachment; filename="' . basename($attachment['filename']) . '"' . $this->newline; $message .= 'Content-ID: <' . basename($attachment['filename']) . '>' . $this->newline . $this->newline; $message .= chunk_split(base64_encode($content)); } } $message .= '--' . $boundary . '--' . $this->newline; 
0
source share

All Articles