2016-12-12 3 views
0

私は次のコードを使ってpdfファイルで画像をループしています。最初のpdfページだけで正しく動作しています。ページ数が2ページ目までです。何故ですか?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

私の要件は、生成されたQrコードの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'); 
} 

私はより良い理解のためにあそこに多くのコメントを書いていますが、画像サイズの必要条件に従って数の面でロジックを変更する必要があります。

関連する問題