Hello all, here is a quick tutorial on how to create a PDF file, save it to disk, save into Salesforce as an attachment and then send it through email.
What you need:
1.To create the PDF file I used the FPDF class.
Download/See more info here.
2.To send email I used phpmailer.
Download/See more info here.
Although I won’t discuss in detail the functionality of those classes I will add code so you can have a general scope on how they work.
Creating the PDF.
All the values were read from SF and passed from prior webpage
PHP Code:
require_once ('./includes/soapclient/SforcePartnerClient.php');
require_once ('./includes/soapclient/SforceHeaderOptions.php');
// Retrieve session attributes
session_start();
$location = $_SESSION['location'];
$sessionId = $_SESSION['sessionId'];
$wsdl = $_SESSION['wsdl'];
$username = $_SESSION['usr'];
$client = new SforcePartnerClient();
$sforceSoapClient = $client->createConnection($wsdl);
$client->setEndpoint($location);
$client->setSessionHeader($sessionId);
require('pdfClass/fpdf.php');
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',8);
$pdf->Ln(5);
$pdf->SetX(155);
$pdf->Cell(80,7,'Quote:'.$quotename,0,1,'L');
///////////////////////////////
$pdf->SetX(20);
$pdf->SetFont('Arial','B',8);
$pdf->Cell(80,3,$firstname." ".$lastname,0,1,'L');
$pdf->SetFont('Arial','',8);
$pdf->SetX(20);
$pdf->Cell(80,3,$company,0,1,'L');
$pdf->SetX(20);
$pdf->Cell(80,3,$street,0,1,'L');
$pdf->SetX(20);
$pdf->Cell(80,3,$city.", ".$state." ".$zipcode,0,1,'L');
$pdf->SetX(20);
$pdf->Cell(80,3,$country,0,1,'L');
//////////////////////////////
$pdf->Ln(3);
$pdf->SetX(20);
$pdf->SetFont('Arial','',11);
$pdf->MultiCell(190,4,"Greeting Message",0,1,'L');
$pdf->AddPage();
//------------------------------------------------------------------- IMAGES
if (file_exists('imgs/products/'.$PId.'-1.jpg'))
{
$pdf->Image('imgs/products/'.$PId.'-1.jpg',38,50,59.94,40.89);
}
if (file_exists('imgs/products/'.$PId.'-1.gif'))
{
$pdf->Image('imgs/products/'.$PId.'-1.gif',38,50,59.94,40.89);
}
//-------------------------------------------------------------------
if (file_exists('imgs/products/'.$PId.'-2.jpg'))
{
$pdf->Image('imgs/products/'.$PId.'-2.jpg',108,50,59.94,40.89);
}
if (file_exists('imgs/products/'.$PId.'-2.gif'))
{
$pdf->Image('imgs/products/'.$PId.'-2.gif',108,50,59.94,40.89);
}
$pdf->Ln();
//-------------------------------------------------------------------
if (file_exists('imgs/products/'.$PId.'-3.jpg'))
{
$pdf->Image('imgs/products/'.$PId.'-3.jpg',38,100,59.94,40.89);
}
if (file_exists('imgs/products/'.$PId.'-3.gif'))
{
$pdf->Image('imgs/products/'.$PId.'-3.gif',38,100,59.94,40.89);
}
//-------------------------------------------------------------------
// PREVIEW THE QUOTE
if($_GET['action']=="preview")
{
// SEND THE FILE TO THE BROWSER with "I"
$pdf->Output('quotes/'.$quotename.'.pdf','I');
}else{
//SAVE THE FILE TO DISK with "F"
$pdf->Output('quotes/'.$quotename.'.pdf','F');
// SAVE TO CONTACT OR LEAD
//------------------------------------------------------------------------------------------------------------------------------------------------
// open the file
$filehandle = fopen( 'quotes/'.$quotename.'.pdf', 'r' );
// get the size
$fileSize = filesize('quotes/'.$quotename.'.pdf');
// specify the type so salesforce can put it in the header
$fileType = 'application/pdf';
// Get the original content
$fileData = fread( $filehandle, $fileSize );
//echo $RecipientId."<br>";
//echo $fileSize."<br>";
//echo $fileType."<br>";
//echo $RecipientId."<br>";
//echo $fileData."<br>";
//
//
if($RecipientId != "0")
{
$sObject1 = new SObject();
$sObject1->type = 'Attachment';
$handle = fopen( 'quotes/'.$quotename.'.pdf', 'r' );
$file_content = fread( $handle, $fileSize );
fclose($handle);
// you need to encode the data (content) with base64
$encoded = chunk_split(base64_encode($file_content));
$sObject1->body = $encoded;
$createFields = array (
'Name' => $quotename.'.pdf',
'ParentId' => $RecipientId, // RecipientId is the ID value of the Lead or contact, because its a reference field you just need to specify the complete value, SF will know if its a Contact or a Lead
'Body' => $encoded
);
$sObject1->fields = $createFields;
$createResponse = $client->create(array($sObject1));
}else{
echo "<br>Quote not saved as attachment due to inexistent contact in Salesforce.com<br>";
}
require("phpMailer/class.phpmailer.php");
$mail = new PHPMailer;
// Now you only need to add the necessary stuff
$mail->FromName = "From Name";
$mail->From = "web@yourdomain.com";
$mail->Username = "usr";
$mail->Password = "pwd";
$mail->AddAddress($email, "");
// in case you use several email addresses from amiltuple select
// for ($i = 0; $i < count($_POST['lstEmails']); $i++) {
// $mail->AddBCC($_POST['lstEmails'][$i], "");
// }
$mail->Subject = " Inventory Quote";
$mail->Body = $content;
// read the saved file
$mail->AddAttachment('quotes/'.$quotename.'.pdf', $quotename.'.pdf');
if(!$mail->Send())
{
echo "<br><br><br>There was an error sending the message<br><br><br>";
exit;
}
}
So it is really very simple to do, this code was used in an application that required to create a quote where you can preview it or save it to Salesforce and send it by email to the Lead (by client request) or Contact.
There you go, hope it helps
Saludos!!!!!