<?php
error_reporting(E_ALL);
require_once 'tcpdf/tcpdf.php';

class MYPDF extends TCPDF {
public function Footer() {
    	if ($this->page <= 4) {
	    	$this->SetY(-20);
			        $footer_text = '
			        <table style="vertical-align: middle;">
						<tbody>
						    <tr>
								<td width="100%" style="width:100%;height:80px;line-height:80px;" align="left" >
									<table width="100%" style="width: 100%;">
										<tbody>
											<tr>
												<td></td>
											</tr>
										</tbody>
									</table>
									 
								</td>
							</tr>
						</tbody>
					</table>
			        ';

			        $this->writeHTML($footer_text , false, true, false, true); 
	    }           
    }
}


$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(true);
header('Access-Control-Allow-Origin: *');


if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

$fontname = TCPDF_FONTS::addTTFfont('font/Spectral-Regular.ttf', 'TrueTypeUnicode', '', 32);
$fontname1 = TCPDF_FONTS::addTTFfont('font/Spectral-Medium.ttf', 'TrueTypeUnicode', '', 32);	


$fontname2 = TCPDF_FONTS::addTTFfont('font/Lato-Black.ttf', 'TrueTypeUnicode', '', 32);
$fontname3 = TCPDF_FONTS::addTTFfont('font/Lato-BlackItalic.ttf', 'TrueTypeUnicode', '', 32);	
$fontname4 = TCPDF_FONTS::addTTFfont('font/Lato-Bold.ttf', 'TrueTypeUnicode', '', 32);
$fontname5 = TCPDF_FONTS::addTTFfont('font/Lato-BoldItalic.ttf', 'TrueTypeUnicode', '', 32);	

$fontname6 = TCPDF_FONTS::addTTFfont('font/Lato-Italic.ttf', 'TrueTypeUnicode', '', 32);
$fontname7 = TCPDF_FONTS::addTTFfont('font/Lato-Light.ttf', 'TrueTypeUnicode', '', 32);	
$fontname8 = TCPDF_FONTS::addTTFfont('font/Lato-LightItalic.ttf', 'TrueTypeUnicode', '', 32);
$fontname9 = TCPDF_FONTS::addTTFfont('font/Lato-Regular.ttf', 'TrueTypeUnicode', '', 32);	 
$fontname10 = TCPDF_FONTS::addTTFfont('font/Spectral-Italic.ttf', 'TrueTypeUnicode', '', 32);	

$pdf->SetMargins(5, 5, 5, 5);
// $pdf->SetFooterMargin(50);

$pdf->AddFont($fontname, '', 10, '', false); 
$pdf->AddFont($fontname1, '', 10, '', false);
$pdf->AddFont($fontname2, '', 10, '', false); 
$pdf->AddFont($fontname3, '', 10, '', false);
$pdf->AddFont($fontname4, '', 10, '', false); 
$pdf->AddFont($fontname5, '', 10, '', false);
$pdf->AddFont($fontname6, '', 10, '', false); 
$pdf->AddFont($fontname7, '', 10, '', false);
$pdf->AddFont($fontname8, '', 10, '', false); 
$pdf->AddFont($fontname9, '', 10, '', false);
$pdf->AddFont($fontname10, '', 10, '', false); 

$i = 0;

$pdf->AddPage();

if (isset($_POST['title']) ) {

$title = $_POST["title"];
$title1 = json_decode($title);
// echo($title1);
$html = '';


$pageCount = 0; 
$isHeader = true;
$isLessvalue = true;

$arrLen = count($title1);
$total = intval($arrLen / 6);


$header = ' 
	<table style="  width:100%; " cellpadding="0" cellspacing="0">
			<tbody>
				<tr>
					<td>
						<table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0">
							<tbody> 
								<tr>
									<td width="100%" style="width:100%;">
										<table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0">
											<tbody> 
												<tr>
													<td  width="40%" style="width: 40%;" valign="middle">
														<a target="_blank" href="#" style="text-decoration:none;">
													 		<img src="./images/Logo_color.png"  width="200" height="28" >
													 	</a> 	
													</td>
													<td width="60%" style="width: 60%; font-size:20px;font-weight:900;font-family:Lato-Black;" cellpadding="0" cellspacing="0" valign="middle" >
														 Accessories
													</td>
												</tr>
											</tbody>
										</table>
									</td>
								</tr>  
								<tr>
									<td width="100%" style="line-height:10px;height:10px;">
									</td>
								</tr> 
								<tr>
									<td width="100%" style="width:100%;" >
										<table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0">
											<tbody> 
												<tr>
													<td width="100%" style="width: 100%;" >
														<table width="100%" style="width: 100%;" cellpadding="5" cellspacing="0">
															<tbody> ';

$footer = '</tbody>
	</table>
	</td>
	</tr>
	</tbody>
	</table>
	</td>
	</tr> 
	</tbody>
	</table>

	</td>
	</tr>
	</tbody>
	<span style="text-align:right">'.$pdf->getAliasNumPage().'</span>
	</table>
'; 

foreach ($title1 as $arr) {

		$html .= '<tr>';
foreach ($arr as $ary) {
	$file_name = basename($ary->img); 
	$url = $ary->img;
	// getImage($url,$file_name);
	copy($url,'images/'.$file_name);
	$i += 1;

	$html .= '<td width="50%" style="width:50%;">
<table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0">
	<tbody> 
		<tr>
			<td width="100%"  height="110px" style="width: 100%; height:110px;border-top:1px solid #777; border-left:1px solid #777; border-right:1px solid #777; border-bottom:1px solid #777;" valign="top">
				<table width="100%" style="width: 100%;" cellpadding="5" cellspacing="0">
					<tbody> 
						<tr> 
							<td width="100%" style="width:100%;">
								 <table width="100%" style="width: 100%;" cellspacing="0">
									<tbody> 
										<tr> 
											<td width="80px" style="width:80px;">
												<img src="images/'.$file_name.'">
											</td>
											<td width="180px" style="width:180px;border-left:1px solid #777;padding-left:20px; ">
												<table width="100%" style="width: 100%;" cellspacing="0">
													<tbody> 
														<tr> 
															<td width="100%" style="width: 100%;font-family:Lato-Black;font-size:9px;" >
																'.$ary->title.'
															</td>
														</tr>
														<tr>
															<td width="100%" style="line-height:5px;height:5px;">
															</td>
														</tr> 
														<tr> 
															<td width="100%" style="width: 100%;font-family:Lato;font-size:8px;color: #a6192e;" >
																 '.$ary->tag.' 
															</td>
														</tr>
														<tr>
															<td width="100%" style="line-height:5px;height:5px;">
															</td>
														</tr> 
														<tr> 
															<td width="100%" style="width: 100%;font-family:Lato;font-size:7px; color: #000; " >
																<ul style="margin: 0px; padding: 0px;">
																	'.$ary->desc1.'
																</ul>
															</td>
														</tr>
													</tbody>
												</table>
											</td>
										</tr>
									</tbody>
								</table>
							</td> 
						</tr>
					</tbody>
				</table>
			</td>
		</tr>
	</tbody>
</table>	
</td>';

}
		$html .= '</tr>';

		if($i == 12){
			$pageCount += 1;
			echo $pageCount;
			$pdf->SetAutoPageBreak(true, 0);
			if($isHeader){
				$pdf->writeHTML($header.$html.$footer, true, false, true, false, '');
				$isHeader = false;
			}else{
				$pdf->writeHTML('<table style="  width:100%; " cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0"> <tbody><tr> <td width="100%" style="line-height:10px;height:10px;"> </td> </tr> <tr> <td width="100%" style="width:100%;" > <table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0"> <tbody> <tr> <td width="100%" style="width: 100%;" > <table width="100%" style="width: 100%;" cellpadding="5" cellspacing="0"> <tbody>'.$html.$footer
				, true, false, true, false, '');
			}
			
				$pdf->lastPage();
				if($total !== $pageCount){
					$pdf->addPage();					
				}
				
				$i = 0;
				$html = '';
		}
		
		
}
if($arrLen < 12 && $isLessvalue){
	if($isHeader){
		$pdf->writeHTML($header.$html.$footer, true, false, true, false, '');
		$isHeader = false;
		$isLessvalue = false;
	}else{
		$pdf->writeHTML('<table style="  width:100%; " cellpadding="0" cellspacing="0"> <tbody> <tr> <td> <table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0"> <tbody><tr> <td width="100%" style="line-height:10px;height:10px;"> </td> </tr> <tr> <td width="100%" style="width:100%;" > <table width="100%" style="width: 100%;" cellpadding="0" cellspacing="0"> <tbody> <tr> <td width="100%" style="width: 100%;" > <table width="100%" style="width: 100%;" cellpadding="5" cellspacing="0"> <tbody>'.$html.$footer
		, true, false, true, false, '');
		$isLessvalue = false;
	}

}

ob_end_clean();
 
$pdf->Output('htmlout.pdf', 'I');
}

?>