Create single pdf from multiple images using tcpdf php

I am working on to merge multiple images to create a single pdf. There are images for every transaction and there can be multiple transaction under one order. Each transaction have mostly 3 images or maybe 2 in some cases. Now i need to create single pdf for every order from these images. My code is working fine but in some cases it’s showing next transaction image on previous pdf page. I want to create single pdf from transaction images but every transaction image should start on new pdf page. e.g. there are 3 transaction under one order and every transaction have 3 images. when create single pdf then there should be 6 pages like 2 image on first pdf page next image on 2nd page and next transaction images should start on pdf 3rd page.

    $pdfNameArr = $_POST['assignmultiplvalue'];
$List = implode(',', $pdfNameArr);
$achno = $_POST['sendachnumber'];
$expPdf = explode(',',$List);
$path = $file = FCPATH."upload/";
$expPdf = explode(',',$List);
$path = $file = FCPATH."upload/";
foreach($expPdf as $pdfVal)
{
foreach(glob("$path/{$pdfVal}*") as $imgfilename)
{

$ext = substr(strrchr($imgfilename, '.'), 1);
if($ext=='jpeg')
{
$imgName[] = basename($imgfilename);

}   

}

}
$count = count($imgName);
$filelocation = FCPATH."/upload/multigenerate"; // directory to save new pdf after merge
$time = date("d-m-Y")."-".time();
$mainfilename="ACH".$achno."-multipdf-".$time;
$filename=$mainfilename.".pdf";
$fileNL = $filelocation."/".$filename;
$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('TestUser');
$pdf->SetSubject('Invoice Pdf');
$pdf->SetKeywords('Invoice Pdf');
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(2, 10, 2,true);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, 10);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('times', 'BI', 10);
$pdf->my_header_title="";
$html="";
$html .= '<table>';
for($i=0; $i<=$count;$i++)
{
if($imgName[$i]=="")
continue;
$img11=base_url()."upload/".$imgName[$i];
$html .= '
<tr>
<td>
<img src="'.$img11.'">
</td>
</tr>';
}   
$html .= '</table>';
$pdf->AddPage();        
$pdf->writeHtml($html);
$pdf->Output($fileNL, 'F');

`

  • Simply trigger $pdf->AddPage(); when the transaction changes

    – 




  • i have added but it’s not working

    – 

  • It appears you have added it at the very end, after all of the data has already been processed – that of course makes very little sense. Also, please properly indent the code, this is hard to read in terms of where blocks start and end.

    – 




Leave a Comment