example_014.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. //============================================================+
  3. // File name : example_014.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2013-05-14
  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 LTD
  15. // www.tecnick.com
  16. // info@tecnick.com
  17. //============================================================+
  18. /**
  19. * Creates an example PDF TEST document using TCPDF
  20. * @package com.tecnick.tcpdf
  21. * @abstract TCPDF - Example: Javascript Form and user rights (only works on Adobe Acrobat)
  22. * @author Nicola Asuni
  23. * @since 2008-03-04
  24. * @group javascript
  25. * @group form
  26. * @group pdf
  27. */
  28. // Include the main TCPDF library (search for installation path).
  29. require_once('tcpdf_include.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 (optional)
  54. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  55. require_once(dirname(__FILE__).'/lang/eng.php');
  56. $pdf->setLanguageArray($l);
  57. }
  58. // ---------------------------------------------------------
  59. // IMPORTANT: disable font subsetting to allow users editing the document
  60. $pdf->setFontSubsetting(false);
  61. // set font. 'helvetica' MUST be used to avoid a PHP notice from PHP 7.4+
  62. $pdf->setFont('helvetica', '', 10, '', false);
  63. // add a page
  64. $pdf->AddPage();
  65. /*
  66. It is possible to create text fields, combo boxes, check boxes and buttons.
  67. Fields are created at the current position and are given a name.
  68. This name allows to manipulate them via JavaScript in order to perform some validation for instance.
  69. */
  70. // set default form properties
  71. $pdf->setFormDefaultProp(array('lineWidth'=>1, 'borderStyle'=>'solid', 'fillColor'=>array(255, 255, 200), 'strokeColor'=>array(255, 128, 128)));
  72. $pdf->setFont('helvetica', 'BI', 18);
  73. $pdf->Cell(0, 5, 'Example of Form', 0, 1, 'C');
  74. $pdf->Ln(10);
  75. $pdf->setFont('helvetica', '', 12);
  76. // First name
  77. $pdf->Cell(35, 5, 'First name:');
  78. $pdf->TextField('firstname', 50, 5);
  79. $pdf->Ln(6);
  80. // Last name
  81. $pdf->Cell(35, 5, 'Last name:');
  82. $pdf->TextField('lastname', 50, 5);
  83. $pdf->Ln(6);
  84. // Gender
  85. $pdf->Cell(35, 5, 'Gender:');
  86. $pdf->ComboBox('gender', 30, 5, array(array('', '-'), array('M', 'Male'), array('F', 'Female')));
  87. $pdf->Ln(6);
  88. // Drink
  89. $pdf->Cell(35, 5, 'Drink:');
  90. //$pdf->RadioButton('drink', 5, array('readonly' => 'true'), array(), 'Water');
  91. $pdf->RadioButton('drink', 5, array(), array(), 'Water');
  92. $pdf->Cell(35, 5, 'Water');
  93. $pdf->Ln(6);
  94. $pdf->Cell(35, 5, '');
  95. $pdf->RadioButton('drink', 5, array(), array(), 'Beer', true);
  96. $pdf->Cell(35, 5, 'Beer');
  97. $pdf->Ln(6);
  98. $pdf->Cell(35, 5, '');
  99. $pdf->RadioButton('drink', 5, array(), array(), 'Wine');
  100. $pdf->Cell(35, 5, 'Wine');
  101. $pdf->Ln(6);
  102. $pdf->Cell(35, 5, '');
  103. $pdf->RadioButton('drink', 5, array(), array(), 'Milk');
  104. $pdf->Cell(35, 5, 'Milk');
  105. $pdf->Ln(10);
  106. // Newsletter
  107. $pdf->Cell(35, 5, 'Newsletter:');
  108. $pdf->CheckBox('newsletter', 5, true, array(), array(), 'OK');
  109. $pdf->Ln(10);
  110. // Address
  111. $pdf->Cell(35, 5, 'Address:');
  112. $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.'));
  113. $pdf->Ln(19);
  114. // Listbox
  115. $pdf->Cell(35, 5, 'List:');
  116. $pdf->ListBox('listbox', 60, 15, array('', 'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7'), array('multipleSelection'=>'true'));
  117. $pdf->Ln(20);
  118. // E-mail
  119. $pdf->Cell(35, 5, 'E-mail:');
  120. $pdf->TextField('email', 50, 5);
  121. $pdf->Ln(6);
  122. // Date of the day
  123. $pdf->Cell(35, 5, 'Date:');
  124. $pdf->TextField('date', 30, 5, array(), array('v'=>date('Y-m-d'), 'dv'=>date('Y-m-d')));
  125. $pdf->Ln(10);
  126. $pdf->setX(50);
  127. // Button to validate and print
  128. $pdf->Button('print', 30, 10, 'Print', 'Print()', array('lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>array(128, 196, 255), 'strokeColor'=>array(64, 64, 64)));
  129. // Reset Button
  130. $pdf->Button('reset', 30, 10, 'Reset', array('S'=>'ResetForm'), array('lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>array(128, 196, 255), 'strokeColor'=>array(64, 64, 64)));
  131. // Submit Button
  132. $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)));
  133. // Form validation functions
  134. $js = <<<EOD
  135. function CheckField(name,message) {
  136. var f = getField(name);
  137. if(f.value == '') {
  138. app.alert(message);
  139. f.setFocus();
  140. return false;
  141. }
  142. return true;
  143. }
  144. function Print() {
  145. if(!CheckField('firstname','First name is mandatory')) {return;}
  146. if(!CheckField('lastname','Last name is mandatory')) {return;}
  147. if(!CheckField('gender','Gender is mandatory')) {return;}
  148. if(!CheckField('address','Address is mandatory')) {return;}
  149. print();
  150. }
  151. EOD;
  152. // Add Javascript code
  153. $pdf->IncludeJS($js);
  154. // ---------------------------------------------------------
  155. //Close and output PDF document
  156. $pdf->Output('example_014.pdf', 'D');
  157. //============================================================+
  158. // END OF FILE
  159. //============================================================+