security fix in master branch
[openemr.git] / library / classes / PDF_Label.php
blob84f95d9c62d26a9071fe2e1f0de2a35e53bc1d1d
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 // VERSIONS:
11 // 1.0: Initial release
12 // 1.1: + Added unit in the constructor
13 // + Now Positions start at (1,1).. then the first label at top-left of a page is (1,1)
14 // + Added in the description of a label:
15 // font-size : defaut char size (can be changed by calling Set_Char_Size(xx);
16 // paper-size: Size of the paper for this sheet (thanx to Al Canton)
17 // metric : type of unit used in this description
18 // You can define your label properties in inches by setting metric to
19 // 'in' and print in millimiters by setting unit to 'mm' in constructor
20 // Added some formats:
21 // 5160, 5161, 5162, 5163, 5164: thanks to Al Canton
22 // 8600 : thanks to Kunal Walia
23 // + Added 3mm to the position of labels to avoid errors
24 // 1.2: = Bug of positioning
25 // = Set_Font_Size modified -> Now, just modify the size of the font
26 // 1.3: + Labels are now printed horizontally
27 // = 'in' as document unit didn't work
28 // 1.4: + Page scaling is disabled in printing options
29 // 1.5: + Added 3422 format
30 ////////////////////////////////////////////////////////////////////////////////////////////////
32 // Modified to function in Open-emr 8-19-2014 Terry Hill terry@lillysystems.com
34 /**
35 * PDF_Label - PDF label editing
36 * @package PDF_Label
37 * @author Laurent PASSEBECQ
38 * @copyright 2003 Laurent PASSEBECQ
39 **/
43 class eFPDF extends FPDF{
44 function TextWithRotation($x, $y, $txt, $txt_angle, $font_angle=0)
46 $font_angle+=90+$txt_angle;
47 $txt_angle*=M_PI/180;
48 $font_angle*=M_PI/180;
50 $txt_dx=cos($txt_angle);
51 $txt_dy=sin($txt_angle);
52 $font_dx=cos($font_angle);
53 $font_dy=sin($font_angle);
55 $s=sprintf('BT %.2F %.2F %.2F %.2F %.2F %.2F Tm (%s) Tj ET',$txt_dx,$txt_dy,$font_dx,$font_dy,$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
56 if ($this->ColorFlag)
57 $s='q '.$this->TextColor.' '.$s.' Q';
58 $this->_out($s);
63 class PDF_Label extends FPDF {
65 // Private properties
66 var $_Margin_Left; // Left margin of labels
67 var $_Margin_Top; // Top margin of labels
68 var $_X_Space; // Horizontal space between 2 labels
69 var $_Y_Space; // Vertical space between 2 labels
70 var $_X_Number; // Number of labels horizontally
71 var $_Y_Number; // Number of labels vertically
72 var $_Width; // Width of label
73 var $_Height; // Height of label
74 var $_Line_Height; // Line height
75 var $_Padding; // Padding
76 var $_Metric_Doc; // Type of metric for the document
77 var $_COUNTX; // Current x position
78 var $_COUNTY; // Current y position
80 // List of label formats
81 var $_Avery_Labels = array(
82 '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'=>10),
83 '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'=>10),
84 '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'=>10),
85 '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),
86 '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),
87 '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),
88 '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),
89 '3422' => array('paper-size'=>'A4', 'metric'=>'mm', 'marginLeft'=>0, 'marginTop'=>8.5, 'NX'=>3, 'NY'=>8, 'SpaceX'=>0, 'SpaceY'=>0, 'width'=>70, 'height'=>35, 'font-size'=>9)
92 // Constructor
93 function __construct($format, $unit='mm', $posX=1, $posY=1) {
94 if (is_array($format)) {
95 // Custom format
96 $Tformat = $format;
97 } else {
98 // Built-in format
99 if (!isset($this->_Avery_Labels[$format]))
100 $this->Error('Unknown label format: '.$format);
101 $Tformat = $this->_Avery_Labels[$format];
104 parent::__construct('P', $unit, $Tformat['paper-size']);
105 $this->_Metric_Doc = $unit;
106 $this->_Set_Format($Tformat);
107 $this->SetFont('Arial');
108 $this->SetMargins(0,0);
109 $this->SetAutoPageBreak(false);
110 $this->_COUNTX = $posX-2;
111 $this->_COUNTY = $posY-1;
114 function _Set_Format($format) {
115 $this->_Margin_Left = $this->_Convert_Metric($format['marginLeft'], $format['metric']);
116 $this->_Margin_Top = $this->_Convert_Metric($format['marginTop'], $format['metric']);
117 $this->_X_Space = $this->_Convert_Metric($format['SpaceX'], $format['metric']);
118 $this->_Y_Space = $this->_Convert_Metric($format['SpaceY'], $format['metric']);
119 $this->_X_Number = $format['NX'];
120 $this->_Y_Number = $format['NY'];
121 $this->_Width = $this->_Convert_Metric($format['width'], $format['metric']);
122 $this->_Height = $this->_Convert_Metric($format['height'], $format['metric']);
123 $this->Set_Font_Size($format['font-size']);
124 $this->_Padding = $this->_Convert_Metric(3, 'mm');
127 // convert units (in to mm, mm to in)
128 // $src must be 'in' or 'mm'
129 function _Convert_Metric($value, $src) {
130 $dest = $this->_Metric_Doc;
131 if ($src != $dest) {
132 $a['in'] = 39.37008;
133 $a['mm'] = 1000;
134 return $value * $a[$dest] / $a[$src];
135 } else {
136 return $value;
140 // Give the line height for a given font size
141 function _Get_Height_Chars($pt) {
142 $a = array(6=>2, 7=>2.5, 8=>3, 9=>4, 10=>5, 11=>6, 12=>7, 13=>8, 14=>9, 15=>10);
143 if (!isset($a[$pt]))
144 $this->Error('Invalid font size: '.$pt);
145 return $this->_Convert_Metric($a[$pt], 'mm');
148 // Set the character size
149 // This changes the line height too
150 function Set_Font_Size($pt) {
151 $this->_Line_Height = $this->_Get_Height_Chars($pt);
152 $this->SetFontSize($pt);
155 // Print a label
156 function Add_Label($text) {
157 $this->_COUNTX++;
158 if ($this->_COUNTX == $this->_X_Number) {
159 // Row full, we start a new one
160 $this->_COUNTX=0;
161 $this->_COUNTY++;
162 if ($this->_COUNTY == $this->_Y_Number) {
163 // End of page reached, we start a new one
164 $this->_COUNTY=0;
165 $this->AddPage();
169 $_PosX = $this->_Margin_Left + $this->_COUNTX*($this->_Width+$this->_X_Space) + $this->_Padding;
170 $_PosY = $this->_Margin_Top + $this->_COUNTY*($this->_Height+$this->_Y_Space) + $this->_Padding;
171 $this->SetXY($_PosX, $_PosY);
172 $this->MultiCell($this->_Width - $this->_Padding, $this->_Line_Height, $text, 0, 'L');
175 function _putcatalog()
177 parent::_putcatalog();
178 // Disable the page scaling option in the printing dialog
179 $this->_out('/ViewerPreferences <</PrintScaling /None>>');