我使用以下代码通过pdf文件循环图像,它仅对第一个pdf页面正常工作,直到第二页每页打印一个图像。这是为什么?
$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 :(得分: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');
}
我在那里写了很多评论以便更好地理解,但你需要根据你的图像大小要求修改数字方面的逻辑。