Jump to content

TCPDF file generation problem


DarkPrince2005

Recommended Posts

Hi everyone

 

I'm trying to generate a pdf file using TCPDF through a foreach loop. My problem is that its only generating the first entry in the array and not the second. (I'm doing this through MODx)

<?php
ini_set("display_errors", 1);
    $action = $_REQUEST['action'];
    
    switch($action) {
        case "generate_pdf":
            $id = (int) $_POST['id'];
            $html ='';
            require_once( $modx->config['base_path'].'/assets/tcpdf/examples/tcpdf_include.php');

            // create new PDF document
            
            $pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

            // set document information
            $pdf->SetCreator(PDF_CREATOR);
            $pdf->SetAuthor('Focus on Health');
            $pdf->SetTitle('CPD Activity Record');
            $pdf->SetSubject('CPD Activity Record');
            $pdf->SetKeywords('CPD, Activity, Record');
            
            // remove default header/footer
            $pdf->setPrintHeader(false);
            $pdf->setPrintFooter(false);
            
            // set default monospaced font
            $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
            
            // set margins
            $pdf->SetMargins(5, 5, 5);
            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
            
            // 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 (optional)
            if (@file_exists($modx->config['base_path'].'/assets/tcpdf/lang/eng.php')) {
                require_once($modx->config['base_path'].'/assets/tcpdf/lang/eng.php');
                $pdf->setLanguageArray($l);
            }
            
            // ---------------------------------------------------------
            
            // set font
            $pdf->SetFont('helvetica', '', 9);
            
            $pdf->AddPage();
            


$html .= '<h1>Number of Active CEU's</h1>';

$ik = 1;
$gr_arr = array(1=>'CLINICAL CEU\'S',2=>'ETHICS CEU\'S');

$data = array();

foreach($gr_arr as $group_id => $group_title){
$level1_total = 0;
$level2_total = 0;
$level3_total = 0;

$sql = "SELECT c.*,uc.certificate_file FROM foh_conferences c
        LEFT JOIN foh_users_certification uc ON c.attachment_id=uc.id
        WHERE c.user_id = '".$_POST["id"]."' AND group_id = '".$group_id."' and (conference_accured + INTERVAL 2 YEAR) > NOW();";
        
        $query = new xPDOCriteria($modx, $sql);
        $query->prepare();
        $query->stmt->execute();
        $data["conferences"] = array();
        $conferences = array();
        while($row = $query->stmt->fetch(PDO::FETCH_OBJ)) {
         $conferences[] = $row;   
         $data["conferences"][] = $row;
        }
        
        /*$html .= '<pre>';
        var_dump($conferences);
        $html .= '</pre>';*/
        
        $sql2 = "SELECT e.*, e.exam_id AS renew_exam_id,s.title,s.show_level,a.pass_marks,a.total_marks,a.stotalmarks,s.accreditation_number,s.short_description, s.description,a.finished,g.title,s.number_ceu,s.group_clinical,s.group_ethics,s.date_accured,s.date_valid
        FROM foh_exams e
        INNER JOIN foh_survey s ON s.sid=e.sid
        LEFT JOIN foh_professions g ON s.sid = g.id
        INNER JOIN foh_answers a ON a.a_id=e.aid
        WHERE e.user_id = '".$_POST["id"]."'";
        
        $query2 = new xPDOCriteria($modx, $sql2);
        $query2->prepare();
        $query2->stmt->execute();
        $data["exams"] = array();
        $exams = array();
        while($row2 = $query2->stmt->fetch(PDO::FETCH_OBJ)) {
         $exams[] = $row2;   
         $data["exams"][] = $row2;
        }

    $ik++;
    
    $data["group_name"] = $group_title;
    $data["key"] = $group_id;
        
        /*$html .= '<pre>';
        var_dump($data);
        $html .= '</pre>';*/
            
$html .= '<table cellpadding="3" cellspacing="0" border="0" style="border:1px solid #ccc;width:960px" class="ui-widget ui-corner-all">
<tr>
    	<td colspan="9" style="text-align:center" class="ui-widget-header">
			<h3>TOTAL '.$data["group_name"].'</h3>
		</td>
	</tr>
	<tr>
		<td rowspan="2" class="ui-widget-header" bgcolor="#cccccc">Name of provider</td>
		<td rowspan="2" class="ui-widget-header" bgcolor="#cccccc">Descriptions Of Activity</td>
		<td rowspan="2" class="ui-widget-header" bgcolor="#cccccc" width="100">Accreditation Number</td>
		<td rowspan="1" class="ui-widget-header" colspan="2"  bgcolor="#cccccc">Date</td>
		<td rowspan="2" class="ui-widget-header" colspan="1" bgcolor="#cccccc">Level1</td>
		<td rowspan="2" class="ui-widget-header" colspan="1" bgcolor="#cccccc">Level2</td>
		<td rowspan="2" class="ui-widget-header" bgcolor="#cccccc">Level3</td>
		<td rowspan="2" class="ui-widget-header" bgcolor="#cccccc">Total</td>
	</tr>
	<tr>
		 <td  bgcolor="#cccccc" class="ui-widget-header">Date Accrued</td>
		 <td  bgcolor="#cccccc" class="ui-widget-header">Date Valid</td>
	</tr>';
		foreach($data["exams"] as $exam)
		{

		if(($exam->status == 'failed'||$exam->status == 'completed') && (($exam->group_clinical==1 && $data["key"]==1) || ($exam->group_ethics==1 && $data["key"]==2))  )
		{
		
			$html .= '<tr>
				<td>'.$exam->title.'</td>
				<td>'.$exam->short_description.'</td>
				<td>'.$exam->accreditation_number.'</td>
				
				<td align="center">'.($exam->status == 'completed'||$exam->status == 'failed'?date('Y/m/d',strtotime($exam->date_accured.'')):'----/--/--').'</td>
				<td  align="center">'.($exam->status == 'completed'||$exam->status == 'failed'?date('Y/m/d',strtotime($exam->date_valid)):'----/--/--').'</td>
				<td class="center_points">';
					
						if($exam->status == 'completed' && $exam->show_level == 1)
						{
							$html .= $exam->number_ceu;
							$level1_total += $exam->number_ceu;
						}else{
							$html .= '--';
						}						
					
				$html .= '</td>
				<td class="center_points">';
					
						if($exam->status == 'completed' && $exam->show_level == 2)
						{
							$html .= $exam->number_ceu;
							$level2_total += $exam->number_ceu;
						}else{
							$html .= '--';
						}						
				
				$html .= '</td>
				<td class="center_points">';
					
						if($exam->status == 'completed' && $exam->show_level == 3)
						{
							$html .= $exam->number_ceu;
							$level3_total += $exam->number_ceu;
						}else{
							$html .= '--';
						}						
					
				$html .= '</td>
				<td class="center_points">';
				
				
				if($exam->status == 'completed')
						{
							$html .= $exam->number_ceu;
						
						}else{
							$html .= '--';
						}
								
				$html .= '</td>
			</tr>';
		
		}
		}

		foreach($data["conferences"] as $conf)
		{
		
		if($conf->conference_accured != '0000-00-00')
		{
		
		$conference_accured = strtotime($conf->conference_accured.' +2 year -1 day');
		if($conference_accured > mktime())
		{
		
			$html .= '<tr>
				<td>'.$conf->conference_name.'</td>
				<td>'.$conf->conference_description;
					
					if($conf->attachment_id != 0)
					{
					
					$html .= '<br/>
						<a href="'.base_url().'uploaduser_certificate/'.$conf->certificate_file.'" target="_blank">View File</a>';
					
					}
					
				$html .= '</td>
				<td>'.$conf->conference_number.'</td>
				<td>';
					
					if($conf->conference_accured == '0000-00-00')
					{
						$html .= '----/--/--';
					}else{
						$html .= date('Y/m/d',strtotime($conf->conference_accured.'')); 
					}
					
				$html .= '</td>
				<td>';
					
					if($conf->conference_accured == '0000-00-00')
					{
						$html .= '----/--/--';
					}else{
						$html .= date('Y/m/d',strtotime($conf->conference_accured.' +2 year -1 day'));
					}
					$html .= '</td>
				<td class="center_points">';
					if($conf->level == 1){
						$html .= $conf->points;
						$level1_total += $conf->points;
					}
					
				$html .= '</td>
				<td class="center_points">';
					if($conf->level == 2){
						$html .= $conf->points;
						$level2_total += $conf->points;
					}
					
				$html .= '</td>
				<td class="center_points">';
				
					if($conf->level == 3){
						$html .= $conf->points;
						$level3_total += $conf->points;
					}
					
				
				$html .= '</td>
				<td class="center_points">'.$conf->points.'</td>				
			</tr>';
		
		}
		}
		}
$html .= '<tr>
        <td><b>Total Points</b></td>
		<td> </td>
		<td> </td>
		<td> </td>
		<td> </td>
		<td class="center_points"><b>'.$level1_total.'</b></td>
		<td class="center_points"><b>'.$level2_total.'</b></td>
		<td class="center_points"><b>'.$level3_total.'</b</td>
		<td class="center_points"><b>'.($level1_total+$level2_total+$level3_total).'</b></td>
	</tr>
	<tr>
		<td><b>Points Lapsed</b></td>
		<td> </td>
		<td> </td>
		<td> </td>
		<td> </td>
		<td> </td>
		<td> </td>
		<td> </td>
		<td> </td>
	</tr>
	<tr>
		<td><b>Total Valid Points</b></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td class="center_points"><b>'.$level1_total.'</b></td>
		<td class="center_points"><b>'.$level2_total.'</b></td>
		<td class="center_points"><b>'.$level3_total.'</b></td>
		<td class="center_points"><b>'.($level1_total+$level2_total+$level3_total).'</b></td>
	</tr>';
$html .= '</table><br /><br />';

$level1_total = 0;
$level2_total = 0;
$level3_total = 0;

}
unset($data);
            
            
            $pdf->writeHTML($html, true, 0, true, 0);
            
            $pdf->lastPage();
            
            $filename = $modx->config['base_path'].'/assets/report/'.$_POST["id"].'-'.date("Y-m-d H:i:s").'.pdf';
            
            $fileatt = $pdf->Output($filename, 'F');
    		$json = array("message" => $fileatt);
			

            
            echo $fileatt;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$fileatt1 = $filename; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = $_POST["id"]."-".date("Y-m-d H:i:s").".pdf"; // Filename that will be used for the file as the attachment
$email_from = "sales@mysite.com"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Here is your CPD Activity Report<br>";
$email_message .= "<br>"; // Message that the email has in it
$email_to = ''; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt1,'rb');
$data = fread($file,filesize($fileatt1));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
$message = "Your file was successfully created and has been sent\n to the appropriate email address specified.\n
Make sure to check your junk mail!\n";
} else {
$message = "Sorry but the email could not be sent. Please go back and try again!";
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            echo json_encode(array('message' => $message));
            break;
        
        
        default:
            echo json_encode(array('message' => 'Unknown action'));
            break;
    }
    
die();
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.