example_014.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. //============================================================+
  3. // File name : example_014.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2010-08-08
  6. //
  7. // Description : Example 014 for TCPDF class
  8. // Javascript Form and user rights (only works on Adobe Acrobat)
  9. //
  10. // Author: Nicola Asuni
  11. //
  12. // (c) Copyright:
  13. // Nicola Asuni
  14. // Tecnick.com s.r.l.
  15. // Via Della Pace, 11
  16. // 09044 Quartucciu (CA)
  17. // ITALY
  18. // www.tecnick.com
  19. // info@tecnick.com
  20. //============================================================+
  21. /**
  22. * Creates an example PDF TEST document using TCPDF
  23. * @package com.tecnick.tcpdf
  24. * @abstract TCPDF - Example: Javascript Form and user rights (only works on Adobe Acrobat)
  25. * @author Nicola Asuni
  26. * @since 2008-03-04
  27. */
  28. require_once('../config/lang/eng.php');
  29. require_once('../tcpdf.php');
  30. // create new PDF document
  31. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  32. // set document information
  33. $pdf->SetCreator(PDF_CREATOR);
  34. $pdf->SetAuthor('Nicola Asuni');
  35. $pdf->SetTitle('TCPDF Example 014');
  36. $pdf->SetSubject('TCPDF Tutorial');
  37. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  38. // set default header data
  39. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 014', PDF_HEADER_STRING);
  40. // set header and footer fonts
  41. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  42. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  43. // set default monospaced font
  44. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  45. //set margins
  46. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  47. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  48. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  49. //set auto page breaks
  50. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  51. //set image scale factor
  52. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  53. //set some language-dependent strings
  54. $pdf->setLanguageArray($l);
  55. // ---------------------------------------------------------
  56. // IMPORTANT: disable font subsetting to allow users editing the document
  57. $pdf->setFontSubsetting(false);
  58. // set font
  59. $pdf->SetFont('helvetica', '', 10, '', false);
  60. // add a page
  61. $pdf->AddPage();
  62. /*
  63. It is possible to create text fields, combo boxes, check boxes and buttons.
  64. Fields are created at the current position and are given a name.
  65. This name allows to manipulate them via JavaScript in order to perform some validation for instance.
  66. */
  67. // set default form properties
  68. $pdf->setFormDefaultProp(array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 200), 'strokeColor'=>array(255, 128, 128)));
  69. $pdf->SetFont('helvetica', 'BI', 18);
  70. $pdf->Cell(0, 5, 'Example of Form', 0, 1, 'C');
  71. $pdf->Ln(10);
  72. $pdf->SetFont('helvetica', '', 12);
  73. // First name
  74. $pdf->Cell(35, 5, 'First name:');
  75. $pdf->TextField('firstname', 50, 5);
  76. $pdf->Ln(6);
  77. // Last name
  78. $pdf->Cell(35, 5, 'Last name:');
  79. $pdf->TextField('lastname', 50, 5);
  80. $pdf->Ln(6);
  81. // Gender
  82. $pdf->Cell(35, 5, 'Gender:');
  83. $pdf->ComboBox('gender', 30, 5, array(array('', '-'), array('M', 'Male'), array('F', 'Female')));
  84. $pdf->Ln(6);
  85. // Drink
  86. $pdf->Cell(35, 5, 'Drink:');
  87. //$pdf->RadioButton('drink', 5, array('readonly' => 'true'), array(), 'Water');
  88. $pdf->RadioButton('drink', 5, array(), array(), 'Water');
  89. $pdf->Cell(35, 5, 'Water');
  90. $pdf->Ln(6);
  91. $pdf->Cell(35, 5, '');
  92. $pdf->RadioButton('drink', 5, array(), array(), 'Beer', true);
  93. $pdf->Cell(35, 5, 'Beer');
  94. $pdf->Ln(6);
  95. $pdf->Cell(35, 5, '');
  96. $pdf->RadioButton('drink', 5, array(), array(), 'Wine');
  97. $pdf->Cell(35, 5, 'Wine');
  98. $pdf->Ln(6);
  99. $pdf->Cell(35, 5, '');
  100. $pdf->RadioButton('drink', 5, array(), array(), 'Milk');
  101. $pdf->Cell(35, 5, 'Milk');
  102. $pdf->Ln(10);
  103. // Newsletter
  104. $pdf->Cell(35, 5, 'Newsletter:');
  105. $pdf->CheckBox('newsletter', 5, true, array(), array(), 'OK');
  106. $pdf->Ln(10);
  107. // Adress
  108. $pdf->Cell(35, 5, 'Address:');
  109. $pdf->TextField('address', 60, 18, array('multiline'=>true, 'lineWidth'=>0, 'borderStyle'=>'none'), array('v'=>'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'dv'=>'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'));
  110. $pdf->Ln(19);
  111. // Listbox
  112. $pdf->Cell(35, 5, 'List:');
  113. $pdf->ListBox('listbox', 60, 15, array('', 'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7'), array('multipleSelection'=>'true'));
  114. $pdf->Ln(20);
  115. // E-mail
  116. $pdf->Cell(35, 5, 'E-mail:');
  117. $pdf->TextField('email', 50, 5);
  118. $pdf->Ln(6);
  119. // Date of the day
  120. $pdf->Cell(35, 5, 'Date:');
  121. $pdf->TextField('date', 30, 5, array(), array('v'=>date('Y-m-d'), 'dv'=>date('Y-m-d')));
  122. $pdf->Ln(10);
  123. $pdf->SetX(50);
  124. // Button to validate and print
  125. $pdf->Button('print', 30, 10, 'Print', 'Print()', array('lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>array(128, 196, 255), 'strokeColor'=>array(64, 64, 64)));
  126. // Reset Button
  127. $pdf->Button('reset', 30, 10, 'Reset', array('S'=>'ResetForm'), array('lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>array(128, 196, 255), 'strokeColor'=>array(64, 64, 64)));
  128. // Submit Button
  129. $pdf->Button('submit', 30, 10, 'Submit', array('S'=>'SubmitForm', 'F'=>'http://localhost/printvars.php', 'Flags'=>array('ExportFormat')), array('lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>array(128, 196, 255), 'strokeColor'=>array(64, 64, 64)));
  130. // Form validation functions
  131. $js = <<<EOD
  132. function CheckField(name,message) {
  133. var f = getField(name);
  134. if(f.value == '') {
  135. app.alert(message);
  136. f.setFocus();
  137. return false;
  138. }
  139. return true;
  140. }
  141. function Print() {
  142. if(!CheckField('firstname','First name is mandatory')) {return;}
  143. if(!CheckField('lastname','Last name is mandatory')) {return;}
  144. if(!CheckField('gender','Gender is mandatory')) {return;}
  145. if(!CheckField('address','Address is mandatory')) {return;}
  146. print();
  147. }
  148. EOD;
  149. // Add Javascript code
  150. $pdf->IncludeJS($js);
  151. // ---------------------------------------------------------
  152. //Close and output PDF document
  153. $pdf->Output('example_014.pdf', 'I');
  154. //============================================================+
  155. // END OF FILE
  156. //============================================================+