Persits Software, Inc. Knowledge Base Articles

HOWTO: Use Persits Components under PHP

Problem Description

This article provides short code samples demonstrating the usage of AspEmail and AspPDF in PHP. The other Persits Software components can be used in a Windows-based PHP environment in a similar manner.

We thank Tom Gugger and Shahirasul Halim for providing these useful code samples.

Solution

The following code snippet uses AspEmail to send 20 emails to the message queue:

<?php

for ($i = 1; $i <= 20; $i++)
{
   $mailer = new COM("Persits.MailSender.4") or die("Unable to instantiate aspemail");
   $mailer->host = "mail.host.com";
   $mailer->from = "from@myhost.com";
   $mailer->addaddress("me@domain.com");
   $mailer->subject = "email # ".$i;
   $mailer->body = "this is email # ".$i;

   // authentication
   $mailer->Username = "email@host.com";
   $mailer->Password = "mypass";

   $mailer->Queue = 1; // use emailagent queue
   if(!$mailer->Send()) {
      echo "Error: ".$mailer->ErrorInfo;
   }else{
      echo "success! Mail #".$i."<br>";
   }
}

?>

The following code snippet uses AspPDF's HTML-to-PDF functionality to convert a URL to PDF, and then draws page numbers on each page of the newly created document:

<?php

$pageprint = '_print.php?cid='.$catid.'&uid='.$uid;
$downloadname = "filename " . date("YmdGi") . ".pdf";

$aspPDF = new COM("Persits.Pdf") or die("Unable to instantiate AspPDF");

$docPDF = $aspPDF->CreateDocument();
$docPDF->Title = $downloadname;
$docPDF->Creator = "Creator name";

$margin = 0.5 * 72;

$urlparam =
   "PageWidth=" . 8.267 * 72 .
   ",PageHeight=" . 11.692 * 72 .
   ",LeftMargin=" . $margin .
   ",RightMargin=" . $margin .
   ",TopMargin=" . $margin/2 .
   ",BottomMargin=" . $margin/2;

$docPDF->ImportFromUrl($pageprint, $urlparam);

$totalpage = $docPDF->Pages->Count;
for ($i=1; $i<=$totalpage; $i++) {
   $str = "Page " . $i . " of " . $totalpage;
   $docPDF->Pages($i)->Canvas->DrawText($str, "x=500; y=15; size=10", $docPDF->Fonts("Courier"));
}

$directory = dirname(__FILE__) . '/asppdf/';
$filename = $directory . $downloadname;
$docPDF->Save($filename, False);

?>

To enable COM objects in php, the following change in the php.ini file is required:

com.autoregister_typelib = true