You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
6.1 KiB

4 years ago
  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. */
  25. // Include the main TCPDF library (search for installation path).
  26. require_once('tcpdf_include.php');
  27. // create new PDF document
  28. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  29. // set document information
  30. $pdf->SetCreator(PDF_CREATOR);
  31. $pdf->SetAuthor('Nicola Asuni');
  32. $pdf->SetTitle('TCPDF Example 014');
  33. $pdf->SetSubject('TCPDF Tutorial');
  34. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  35. // set default header data
  36. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 014', PDF_HEADER_STRING);
  37. // set header and footer fonts
  38. $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  39. $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  40. // set default monospaced font
  41. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  42. // set margins
  43. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  44. $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
  45. $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
  46. // set auto page breaks
  47. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  48. // set image scale factor
  49. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  50. // set some language-dependent strings (optional)
  51. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  52. require_once(dirname(__FILE__).'/lang/eng.php');
  53. $pdf->setLanguageArray($l);
  54. }
  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. // Address
  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', 'D');
  154. //============================================================+
  155. // END OF FILE
  156. //============================================================+