query('select * from features') as $rec) { $address = array(); for($line = 0; $line < $labelLines; $line++) { $address[$line] = $lineTemplate[$line]; foreach($rec as $k=>$v) { $address[$line] = str_replace('%'.$k.'%', trim($v), $address[$line]); } $address[$line] = trim($address[$line]); } array_push($allAddresses, $address); # build the virtual csv file $current_address = $CONFIGURATION['csv_record']; foreach($rec as $k=>$v) { $current_address = str_replace('%'.$k.'%', trim($v), $current_address); } $csv_addresses[] = $current_address; } # So I wrote this to just test the functionality. It seems to work pretty well. if($output == 'html') { print ""; $i = 0; $column = 0; $row = 0; while($i < sizeof($csv_addresses)) { if($row == 0 and $column == 0) { print ""; } if($column == 0) { print ""; } print ""; $i++; $column++; if($column >= $maxColumns) { print ""; $column = 0; $row++; } if($row >= $maxRows) { print "
"; for($line = 0; $line < $labelLines; $line++) { print $allAddresses[$i][$line]."
"; } print "
"; print "

"; $row = 0; $column = 0; print ""; } } print "
"; print ""; } elseif($output == 'csv') { header('Content-type: text/csv'); header('Content-Disposition: attachment; filename="mailing_labels.csv"'); #header("Content-type: text/plain"); if($CONFIGURATION['csv_header'] != NULL and $csv_addresses[0] != NULL) { print $CONFIGURATION['csv_header']."\n"; for($i = 0; $i < sizeof($csv_addresses); $i++) { print $csv_addresses[$i]."\n"; } } else { $columnTitles = array(); for($i = 0; $i < $labelLines; $i++) { $columnTitles[$i] = "LINE".($i+1); } $str = '"'.implode('","',$columnTitles)."\"\n"; print $str; foreach($allAddresses as $label) { $str = '"'.implode('","',$label)."\"\n"; print $str; } } } elseif($output == 'pdf') { header('Content-type: application/pdf'); # New PDF Object $PageSize = array(11,8.5); $pdf = new FPDF('L','in', $PageSize); $pdf->SetAutoPageBreak(false); $LABEL_FONT = $CONFIGURATION['label_font']; $LABEL_FONT_SIZE = (float)$CONFIGURATION['label_font_size']; $LABEL_ORIGIN_X = (float)$CONFIGURATION['label_origin_x']; $LABEL_ORIGIN_Y = (float)$CONFIGURATION['label_origin_y']; $LABEL_WIDTH = (float)$CONFIGURATION['label_width']; $LABEL_HEIGHT = (float)$CONFIGURATION['label_height']; $pdf->AddPage(); $pdf->SetMargins(.5,.5,.5); $pdf->SetFont($LABEL_FONT,'',$LABEL_FONT_SIZE); $pdf->SetTextColor(0,0,0); $CurrentX = $LABEL_ORIGIN_X; $CurrentY = $LABEL_ORIGIN_Y; $CurrentRow = 0; $CurrentColumn = 0; foreach($allAddresses as $label) { $lineY = $CurrentY; foreach($label as $line) { $lineFontSize = $LABEL_FONT_SIZE; $pdf->SetXY($CurrentX,$lineY); $pdf->Cell(0,.25,$line,0,0,'L'); # Make the lines even despite variable sizes $lineY = $lineY + $LABEL_FONT_SIZE/72; } $CurrentY += $LABEL_HEIGHT; $CurrentRow++; if($CurrentRow >= $maxRows) { $CurrentRow = 0; $CurrentColumn ++; $CurrentX+=$LABEL_WIDTH; $CurrentY = $LABEL_ORIGIN_Y; if($CurrentColumn >= $maxColumns) { $CurrentColumn = 0; $CurrentRow = 0; $CurrentY = $LABEL_ORIGIN_Y; $CurrentX = $LABEL_ORIGIN_X; $pdf->AddPage(); $pdf->SetMargins(.5,.5,.5); } } } # $pdf->Output('/tmp/www/out.pdf'); $pdf->Output(); } ?>