PHP Classes

FPDF and Address Labels

Recommend this page to a friend!

      PHP Print Labels to PDF  >  All threads  >  FPDF and Address Labels  >  (Un) Subscribe thread alerts  
Subject:FPDF and Address Labels
Summary:Compatibility and function
Messages:2
Author:Tim Brockett
Date:2025-03-03 16:24:40
 

 


  1. FPDF and Address Labels   Reply   Report abuse  
Picture of Tim Brockett Tim Brockett - 2025-03-03 16:24:40
Thanks for writing and sharing your scripts.
I am familiar with FPDF and use it to create basic HTML formatted PDFs. I would like to add a PHP script that will produce a PDF that I can print to an Avery 5160 label sheet. US letter size, 30 labels in 3 rows of 10. Will your script work for this project?
Many Thanks,
Tim Brockett

  2. Re: FPDF and Address Labels   Reply   Report abuse  
Picture of Ahmed Abdulla Ahmed Abdulla - 2025-03-17 01:30:05 - In reply to message 1 from Tim Brockett
use this code

<?php
require('fpdf/fpdf.php');

class PDF_Labels extends FPDF {
private $labels = array();
private $margin_left = 5.95; // Left margin in mm (0.234")
private $margin_top = 12.7; // Top margin in mm (0.5")
private $label_width = 66.7; // Label width in mm (2.63")
private $label_height = 25.4; // Label height in mm (1")
private $cols = 3; // Number of columns
private $rows = 10; // Number of rows
private $h_gap = 3.175; // Horizontal gap between labels in mm (0.125")
private $v_gap = 0; // Vertical gap between labels in mm (0")
private $current_col = 0;
private $current_row = 0;

function __construct($orientation = 'P', $unit = 'mm', $format = 'Letter') {
parent::__construct($orientation, $unit, $format);
$this->SetMargins(0, 0, 0);
$this->SetAutoPageBreak(false);
}

function AddLabel($content, $font = 'Arial', $font_size = 10, $font_style = '') {
$this->labels[] = array(
'content' => $content,
'font' => $font,
'font_size' => $font_size,
'font_style' => $font_style
);
}

function GeneratePDF() {
$this->AddPage();

foreach ($this->labels as $label) {
// Calculate position for current label
$x = $this->margin_left + ($this->current_col * ($this->label_width + $this->h_gap));
$y = $this->margin_top + ($this->current_row * ($this->label_height + $this->v_gap));

// Set font for this label
$this->SetFont($label['font'], $label['font_style'], $label['font_size']);

// Position for text
$this->SetXY($x, $y);

// Add content
$this->MultiCell($this->label_width, 5, $label['content'], 0, 'L');

// Move to next position
$this->current_col++;
if ($this->current_col >= $this->cols) {
$this->current_col = 0;
$this->current_row++;

// Check if we need a new page
if ($this->current_row >= $this->rows) {
$this->current_row = 0;
$this->AddPage();
}
}
}
}

// Optional: Add method to draw label borders for debugging
function DrawLabelBorders() {
for ($row = 0; $row < $this->rows; $row++) {
for ($col = 0; $col < $this->cols; $col++) {
$x = $this->margin_left + ($col * ($this->label_width + $this->h_gap));
$y = $this->margin_top + ($row * ($this->label_height + $this->v_gap));
$this->Rect($x, $y, $this->label_width, $this->label_height);
}
}
}
}

// Example usage
$pdf = new PDF_Labels();

// Add labels (replace with your real data)
for ($i = 1; $i <= 30; $i++) {
$pdf->AddLabel("Label #$i\nCompany Name\n123 Street Name\nCity, State ZIP", 'Arial', 9);
}

// Uncomment to enable border drawing (useful for alignment testing)
// $pdf->DrawLabelBorders();

// Generate and output the PDF
$pdf->GeneratePDF();
$pdf->Output('labels.pdf', 'I'); // 'I' displays in browser, 'D' forces download, 'F' saves to server
?>