2016-12-12 76 views
0

我使用下面的代码来通过pdf文件循环图像,它只对第一个pdf页面正常工作,直到第二页它每页打印一个图像。这是为什么?使用tcpdf循环图像

$this->load->helper('directory'); 
$dir = "assets/barcode/"; 
$map = directory_map($dir); 

$this->load->library("Pdf"); 
$pdf = new Pdf('P', 'mm', 'A4', false, 'UTF-8', true); 
$pdf->SetFont('helvetica', '', 12, '', true); 
$pdf->SetHeaderMargin(30); 
$pdf->SetTopMargin(20); 
$pdf->setFooterMargin(20); 
$pdf->SetAutoPageBreak(true,0); 


$pdf->AddPage(); 

$x = 0; 
$y = 0; 
foreach($map as $k) { 

    $pdfimage = "assets/barcode/".$k; 
    $pdfimgname = $k; 
    $pdf->Image($pdfimage, 30, 25 + $x, 0, 0, '', '', '', false, 9);   

    $pdf->SetXY(70, 30 + $y); 
    $pdf->writeHTML($pdfimgname,100 , 30, 0, 0, '', '', '', false, 9); 

    $x = $x + 30; 
    $y = $y + 30; 

}   
$pdf->Output(); 
+0

它或者已经达到'$ map'的限制或者它正在获取当前的y轴错误。 – rhavendc

回答

1

我的要求是创建生成的QrCodes的pdf。我写这个逻辑。希望这会帮助你。

/** 
* @param {array} $generated : This is array of qr code images 
*/ 
function tcpdfun($generated) 
{ 
    set_time_limit(0); 
    $your_width = 304.8; 
    $your_height = 457.2; 
    $custom_layout = array($your_width, $your_height); 

    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, $custom_layout, true, 'UTF-8', false); 

    $pdf->SetMargins(0, 0, PDF_MARGIN_RIGHT); 
    $pdf->SetAutoPageBreak(TRUE, 0); 
    $pdf->SetPrintHeader(false); 
    $pdf->SetPrintFooter(false); 
    $pdf->SetFont('helvetica', '', 4); 

    $pdf->AddPage(); 

    $pdf->setJPEGQuality(100); 

    $startX = 8.8; // X-axis margin 
    $startY = 6.8; // Y-axis margin 
    $qrPath = base_url()."qrs/"; // QrCode Images folder 
    $imgHW = 25; // Image Height-Width - Square 
    $gap = 4; // Gap between two images 

    $j = 1; 

    for($i=0; $i < count($generated); $i++, $j++) 
    { 
     $pdf->setXY($startX, $startY); // set XY axis 
     $startX = $startX+$imgHW; // Add image width 

     $border = array('LTRB' => array('width' => 0.2, 'cap' => 'butt', 
      'join' => 'miter', 'dash' => 0, 'color' => array(190, 190, 190))); // image border 

     $pdf->Image($qrPath.$generated[$i]["qr_code"].'.png', '', '', $imgHW, $imgHW, 'PNG', '', 'T', 
         false, 300, '', false, false, $border, false, false, false); // image print in pdf 

     if($j > 0) { $pdf->setXY($startX - $imgHW + 1.5, $startY+$imgHW); } 
     else { $pdf->setXY($startX + 1.5, $startY+$imgHW); } 

     $pdf->Write(1, $generated[$i]["qr_code"], '',false, '', false, 0, false, false, 0, 0, ''); 

     /** I calculate my page size and come to know that I can print only 11 images in a row, 
     so I write the following logic to reset X axis to initial position and add an extra height to Y axis **/ 

     if($j >= 11) 
     { 
      $startY = $startY+$imgHW+$gap; 
      $startX = 8.8; 
      $j = 0; 
     } 

     /** I calculate my page size and come to know that I can print only 165 images per page, 
     so I write following logic to add new page after 165 images, and reset X-Y axis to initial position on new page. **/ 

     if((($i+1)%165) == 0 && $i!=0) 
     { 
      $pdf->AddPage(); 
      $startX = 8.8; 
      $startY = 6.8; 
      $j = 0; 
     } 
    } 

    $file_name = "qrbatch_".date("YmdHis").".pdf"; 
    //Close and output PDF document 
    $pdf->Output($file_name, 'D'); 
} 

我写了很多在那边为了更好地理解意见,但你需要修改逻辑的数目来按您的图片大小要求。