$row = $database->loadObject();

		if (pathinfo($row->storage_path,PATHINFO_EXTENSION) != 'pdf') {
			die('This is not a pdf file.');
		}

		@ini_set('memory_limit','512M');
    	$database = &JFactory::getDBO();
	    $document = &JFactory::getDocument();
	    $doc = &JDocument::getInstance('raw');
	    $document = $doc;
	    
		require_once(JPATH_ADMINISTRATOR . DS .'components' . DS. 'com_docmanpaypal' . DS . 'fpdi' . DS . 'FPDI_Protection.php');
		$pdf = new FPDI_Protection();
		if ($dm->cfg['pdfOrientation'] == 'portrait') {
		$pdf->FPDF('P', 'in', array('8.267','11.692'));
		} else {
		$pdf->FPDF('P', 'in', array('11.692','8.267'));
		}
		$sourceFile = $dm->docman_path . DS . $row->storage_path;
		
		$pdfPreviewPath = dirname($sourceFile) . DS . basename($sourceFile,'.pdf') . "_preview.pdf";
		
		//die($pdfPreviewPath);
		
		if (file_exists($pdfPreviewPath)) {
			$sourceFile = $pdfPreviewPath;
		}
		$pagecount = $pdf->setSourceFile($sourceFile);
		for ($loop = 1; $loop <= $dm->cfg['pdfPreviewPages']; $loop++) {
		$tplidx = $pdf->importPage($loop);
 function doEmailDelivery($email, $doc_id, $order_id, $key)
 {
     @ini_set('memory_limit', '1024M');
     $database = JFactory::getDBO();
     @($x = explode(",", $doc_id));
     if (is_array($x) == false) {
         $x[] = $doc_id;
         $mode = 'single';
     } else {
         $mode = 'cart';
     }
     $mail = JFactory::getMailer();
     $mail->IsHTML(true);
     $app = JFactory::getApplication();
     //$conf = JFactory::getConfig();
     $emailBody = $this->cfg['emailDeliveryBody'];
     $maxEmailDeliverySize = $this->cfg['emailDeliveryMaxSizeInMB'] * 1024 * 1024;
     $downloadLink = JURI::base() . "index.php?option=com_docmanpaypal&task=doc_download&gid=" . $doc_id . "&order_id={$order_id}&key={$key}&mode={$mode}&Itemid=" . (int) JRequest::getVar('Itemid');
     foreach ($x as $doc_id) {
         $database->setQuery("select storage_path from #__docman_documents where docman_document_id = {$doc_id} limit 1");
         $dmfilename = $database->loadResult();
         $attachment = $this->docman_path . DS . $dmfilename;
         //3.2.2 Encrypted PDF files
         if ($this->cfg['encryptPDF'] == 1 && pathinfo($attachment, PATHINFO_EXTENSION) == 'pdf') {
             //password for the pdf file (I suggest using the email adress of the purchaser)
             $password = $email;
             //name of the original file (unprotected)
             $origFile = $attachment;
             //name of the destination file (password protected and printing rights removed)
             $destFile = dirname($attachment) . DS . basename($attachment, '.pdf') . "_protected.pdf";
             @unlink($destFile);
             require_once 'fpdi' . DS . 'FPDI_Protection.php';
             $pdf = new FPDI_Protection();
             if ($this->cfg['pdfOrientation'] == 'portrait') {
                 $pdf->FPDF('P', 'in', array('8', '11'));
             } else {
                 $pdf->FPDF('P', 'in', array('11', '8'));
             }
             $pagecount = $pdf->setSourceFile($attachment);
             for ($loop = 1; $loop <= $pagecount; $loop++) {
                 $tplidx = $pdf->importPage($loop);
                 $pdf->addPage();
                 $pdf->useTemplate($tplidx);
             }
             $pdf->SetProtection(array(), $password);
             $pdf->Output($destFile, 'F');
             //pdfEncrypt($origFile, $password, $destFile );
             $attachment = $destFile;
             //mail('*****@*****.**','DEbug',"$attachment, $origFile, $destFile");
         }
         if ($maxEmailDeliverySize > filesize($attachment)) {
             $mail->addAttachment($attachment);
         } else {
             $theFileIsLarge = true;
         }
     }
     if ($this->cfg['emailDeliveryDownloadLink'] == 0) {
         $emailBody = str_replace('%downloadlink%', '', $emailBody);
     }
     if ($this->cfg['emailDeliveryDownloadLink'] == 1) {
         $emailBody = str_replace('%downloadlink%', $downloadLink, $emailBody);
     }
     if ($this->cfg['emailDeliveryDownloadLink'] == 2) {
         if ($theFileIsLarge) {
             $emailBody = str_replace('%downloadlink%', $downloadLink, $emailBody);
         } else {
             $emailBody = str_replace('%downloadlink%', '', $emailBody);
         }
     }
     if ($this->cfg['emailDeliveryToAdmin'] == 1) {
         $database->setQuery("SELECT #__users.email, #__usergroups.title from #__users left join #__user_usergroup_map on #__users.id = #__user_usergroup_map.user_id left join #__usergroups on #__usergroups.id = #__user_usergroup_map.group_id where #__usergroups.title = 'Super Users';");
         $result = $database->loadAssocList();
         foreach ($result as $row) {
             $mail->addRecipient($row['email']);
         }
     }
     $mail->setBody($emailBody);
     $mail->setSubject($this->cfg['emailDeliverySubject']);
     if ($app->getCfg('mailer') == 'smtp') {
         $mail->useSMTP($app->getCfg('smtpauth'), $app->getCfg('smtphost'), $app->getCfg('smtpuser'), $app->getCfg('smtppass'), $app->getCfg('smtpsecure'), $app->getCfg('smtpport'));
     }
     $mail->setSender(array($app->getCfg('mailfrom'), $app->getCfg('fromname')));
     $mail->addRecipient($email);
     $mail->Send();
 }