|
| 1 | +<?php |
| 2 | +//////////////////////////////////////////////////////////////////////////////////////////////// |
| 3 | +// PDF_Label |
| 4 | +// |
| 5 | +// Class to print labels in Avery or custom formats |
| 6 | +// |
| 7 | +// Copyright (C) 2003 Laurent PASSEBECQ (LPA) |
| 8 | +// Based on code by Steve Dillon |
| 9 | +// |
| 10 | +//--------------------------------------------------------------------------------------------- |
| 11 | +// VERSIONS: |
| 12 | +// 1.0: Initial release |
| 13 | +// 1.1: + Added unit in the constructor |
| 14 | +// + Now Positions start at (1,1).. then the first label at top-left of a page is (1,1) |
| 15 | +// + Added in the description of a label: |
| 16 | +// font-size : defaut char size (can be changed by calling Set_Char_Size(xx); |
| 17 | +// paper-size: Size of the paper for this sheet (thanx to Al Canton) |
| 18 | +// metric : type of unit used in this description |
| 19 | +// You can define your label properties in inches by setting metric to |
| 20 | +// 'in' and print in millimiters by setting unit to 'mm' in constructor |
| 21 | +// Added some formats: |
| 22 | +// 5160, 5161, 5162, 5163, 5164: thanks to Al Canton |
| 23 | +// 8600 : thanks to Kunal Walia |
| 24 | +// + Added 3mm to the position of labels to avoid errors |
| 25 | +// 1.2: = Bug of positioning |
| 26 | +// = Set_Font_Size modified -> Now, just modify the size of the font |
| 27 | +// 1.3: + Labels are now printed horizontally |
| 28 | +// = 'in' as document unit didn't work |
| 29 | +// 1.4: + Page scaling is disabled in printing options |
| 30 | +// 1.5: + Added 3422 format |
| 31 | +//////////////////////////////////////////////////////////////////////////////////////////////// |
| 32 | + |
| 33 | +/** |
| 34 | + * PDF_Label - PDF label editing |
| 35 | + * @package PDF_Label |
| 36 | + * @author Laurent PASSEBECQ |
| 37 | + * @copyright 2003 Laurent PASSEBECQ |
| 38 | +**/ |
| 39 | + |
| 40 | +require_once('fpdf.php'); |
| 41 | + |
| 42 | +class PDF_Label extends FPDF { |
| 43 | + |
| 44 | + // Private properties |
| 45 | + var $_Margin_Left; // Left margin of labels |
| 46 | + var $_Margin_Top; // Top margin of labels |
| 47 | + var $_X_Space; // Horizontal space between 2 labels |
| 48 | + var $_Y_Space; // Vertical space between 2 labels |
| 49 | + var $_X_Number; // Number of labels horizontally |
| 50 | + var $_Y_Number; // Number of labels vertically |
| 51 | + var $_Width; // Width of label |
| 52 | + var $_Height; // Height of label |
| 53 | + var $_Line_Height; // Line height |
| 54 | + var $_Padding; // Padding |
| 55 | + var $_Metric_Doc; // Type of metric for the document |
| 56 | + var $_COUNTX; // Current x position |
| 57 | + var $_COUNTY; // Current y position |
| 58 | + |
| 59 | + // List of label formats |
| 60 | + var $_Avery_Labels = array( |
| 61 | + '5160' => array('paper-size'=>'letter', 'metric'=>'mm', 'marginLeft'=>1.762, 'marginTop'=>10.7, 'NX'=>3, 'NY'=>10, 'SpaceX'=>3.175, 'SpaceY'=>0, 'width'=>66.675, 'height'=>25.4, 'font-size'=>8), |
| 62 | + '5161' => array('paper-size'=>'letter', 'metric'=>'mm', 'marginLeft'=>0.967, 'marginTop'=>10.7, 'NX'=>2, 'NY'=>10, 'SpaceX'=>3.967, 'SpaceY'=>0, 'width'=>101.6, 'height'=>25.4, 'font-size'=>8), |
| 63 | + '5162' => array('paper-size'=>'letter', 'metric'=>'mm', 'marginLeft'=>0.97, 'marginTop'=>20.224, 'NX'=>2, 'NY'=>7, 'SpaceX'=>4.762, 'SpaceY'=>0, 'width'=>100.807, 'height'=>35.72, 'font-size'=>8), |
| 64 | + '5163' => array('paper-size'=>'letter', 'metric'=>'mm', 'marginLeft'=>1.762, 'marginTop'=>10.7, 'NX'=>2, 'NY'=>5, 'SpaceX'=>3.175, 'SpaceY'=>0, 'width'=>101.6, 'height'=>50.8, 'font-size'=>8), |
| 65 | + '5164' => array('paper-size'=>'letter', 'metric'=>'in', 'marginLeft'=>0.148, 'marginTop'=>0.5, 'NX'=>2, 'NY'=>3, 'SpaceX'=>0.2031, 'SpaceY'=>0, 'width'=>4.0, 'height'=>3.33, 'font-size'=>12), |
| 66 | + '8600' => array('paper-size'=>'letter', 'metric'=>'mm', 'marginLeft'=>7.1, 'marginTop'=>19, 'NX'=>3, 'NY'=>10, 'SpaceX'=>9.5, 'SpaceY'=>3.1, 'width'=>66.6, 'height'=>25.4, 'font-size'=>8), |
| 67 | + 'L7163'=> array('paper-size'=>'A4', 'metric'=>'mm', 'marginLeft'=>5, 'marginTop'=>15, 'NX'=>2, 'NY'=>7, 'SpaceX'=>25, 'SpaceY'=>0, 'width'=>99.1, 'height'=>38.1, 'font-size'=>9), |
| 68 | + '3422' => array('paper-size'=>'A4', 'metric'=>'mm', 'marginLeft'=>7, 'marginTop'=>12, 'NX'=>3, 'NY'=>8, 'SpaceX'=>3, 'SpaceY'=>0, 'width'=>63.5, 'height'=>33.9, 'font-size'=>9) |
| 69 | + ); |
| 70 | + |
| 71 | + // Constructor |
| 72 | + function PDF_Label($format, $unit='mm', $posX=1, $posY=1) { |
| 73 | + if (is_array($format)) { |
| 74 | + // Custom format |
| 75 | + $Tformat = $format; |
| 76 | + } else { |
| 77 | + // Built-in format |
| 78 | + if (!isset($this->_Avery_Labels[$format])) |
| 79 | + $this->Error('Unknown label format: '.$format); |
| 80 | + $Tformat = $this->_Avery_Labels[$format]; |
| 81 | + } |
| 82 | + |
| 83 | + parent::__construct('P', $unit, $Tformat['paper-size']); |
| 84 | + $this->_Metric_Doc = $unit; |
| 85 | + $this->_Set_Format($Tformat); |
| 86 | + $this->SetFont('Arial'); |
| 87 | + $this->SetMargins(0,0); |
| 88 | + $this->SetAutoPageBreak(false); |
| 89 | + $this->_COUNTX = $posX-2; |
| 90 | + $this->_COUNTY = $posY-1; |
| 91 | + } |
| 92 | + |
| 93 | + function _Set_Format($format) { |
| 94 | + $this->_Margin_Left = $this->_Convert_Metric($format['marginLeft'], $format['metric']); |
| 95 | + $this->_Margin_Top = $this->_Convert_Metric($format['marginTop'], $format['metric']); |
| 96 | + $this->_X_Space = $this->_Convert_Metric($format['SpaceX'], $format['metric']); |
| 97 | + $this->_Y_Space = $this->_Convert_Metric($format['SpaceY'], $format['metric']); |
| 98 | + $this->_X_Number = $format['NX']; |
| 99 | + $this->_Y_Number = $format['NY']; |
| 100 | + $this->_Width = $this->_Convert_Metric($format['width'], $format['metric']); |
| 101 | + $this->_Height = $this->_Convert_Metric($format['height'], $format['metric']); |
| 102 | + $this->Set_Font_Size($format['font-size']); |
| 103 | + $this->_Padding = $this->_Convert_Metric(3, 'mm'); |
| 104 | + } |
| 105 | + |
| 106 | + // convert units (in to mm, mm to in) |
| 107 | + // $src must be 'in' or 'mm' |
| 108 | + function _Convert_Metric($value, $src) { |
| 109 | + $dest = $this->_Metric_Doc; |
| 110 | + if ($src != $dest) { |
| 111 | + $a['in'] = 39.37008; |
| 112 | + $a['mm'] = 1000; |
| 113 | + return $value * $a[$dest] / $a[$src]; |
| 114 | + } else { |
| 115 | + return $value; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Give the line height for a given font size |
| 120 | + function _Get_Height_Chars($pt) { |
| 121 | + $a = array(6=>2, 7=>2.5, 8=>3, 9=>4, 10=>5, 11=>6, 12=>7, 13=>8, 14=>9, 15=>10); |
| 122 | + if (!isset($a[$pt])) |
| 123 | + $this->Error('Invalid font size: '.$pt); |
| 124 | + return $this->_Convert_Metric($a[$pt], 'mm'); |
| 125 | + } |
| 126 | + |
| 127 | + // Set the character size |
| 128 | + // This changes the line height too |
| 129 | + function Set_Font_Size($pt) { |
| 130 | + $this->_Line_Height = $this->_Get_Height_Chars($pt); |
| 131 | + $this->SetFontSize($pt); |
| 132 | + } |
| 133 | + |
| 134 | + // Print a label |
| 135 | + function Add_Label($text) { |
| 136 | + $this->_COUNTX++; |
| 137 | + if ($this->_COUNTX == $this->_X_Number) { |
| 138 | + // Row full, we start a new one |
| 139 | + $this->_COUNTX=0; |
| 140 | + $this->_COUNTY++; |
| 141 | + if ($this->_COUNTY == $this->_Y_Number) { |
| 142 | + // End of page reached, we start a new one |
| 143 | + $this->_COUNTY=0; |
| 144 | + $this->AddPage(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + $_PosX = $this->_Margin_Left + $this->_COUNTX*($this->_Width+$this->_X_Space) + $this->_Padding; |
| 149 | + $_PosY = $this->_Margin_Top + $this->_COUNTY*($this->_Height+$this->_Y_Space) + $this->_Padding; |
| 150 | + $this->SetXY($_PosX, $_PosY); |
| 151 | + |
| 152 | + $this->MultiCell($this->_Width - $this->_Padding, $this->_Line_Height, $text, 0, 'L'); |
| 153 | + } |
| 154 | + |
| 155 | + function _putcatalog() |
| 156 | + { |
| 157 | + parent::_putcatalog(); |
| 158 | + // Disable the page scaling option in the printing dialog |
| 159 | + $this->_out('/ViewerPreferences <</PrintScaling /None>>'); |
| 160 | + } |
| 161 | + |
| 162 | +} |
| 163 | +?> |
0 commit comments