example_014.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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', 10, 5, array('', 'M', 'F'));
  84. $pdf->ComboBox('gender', 30, 5, array(array('', '-'), array('M', 'Male'), array('F', 'Female')));
  85. $pdf->Ln(6);
  86. // Drink
  87. $pdf->Cell(35, 5, 'Drink:');
  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(10);
  99. // Listbox
  100. $pdf->Cell(35, 5, 'List:');
  101. $pdf->ListBox('listbox', 60, 15, array('', 'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7'), array('multipleSelection'=>'true'));
  102. $pdf->Ln(20);
  103. // Adress
  104. $pdf->Cell(35, 5, 'Address:');
  105. $pdf->TextField('address', 60, 18, array('multiline'=>true));
  106. $pdf->Ln(19);
  107. // E-mail
  108. $pdf->Cell(35, 5, 'E-mail:');
  109. $pdf->TextField('email', 50, 5);
  110. $pdf->Ln(6);
  111. // Newsletter
  112. $pdf->Cell(35, 5, 'Newsletter:');
  113. $pdf->CheckBox('newsletter', 5, true, array(), array(), 'OK');
  114. $pdf->Ln(10);
  115. // Date of the day
  116. $pdf->Cell(35, 5, 'Date:');
  117. $pdf->TextField('date', 30, 5, array(), array('v'=>date('Y-m-d'), 'dv'=>date('Y-m-d')));
  118. $pdf->Ln(10);
  119. $pdf->SetX(50);
  120. // Button to validate and print
  121. $pdf->Button('print', 30, 10, 'Print', 'Print()', array('lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>array(128, 196, 255), 'strokeColor'=>array(64, 64, 64)));
  122. // Reset Button
  123. $pdf->Button('reset', 30, 10, 'Reset', array('S'=>'ResetForm'), array('lineWidth'=>2, 'borderStyle'=>'beveled', 'fillColor'=>array(128, 196, 255), 'strokeColor'=>array(64, 64, 64)));
  124. // Submit Button
  125. $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)));
  126. // Form validation functions
  127. $js = <<<EOD
  128. function CheckField(name,message) {
  129. var f = getField(name);
  130. if(f.value == '') {
  131. app.alert(message);
  132. f.setFocus();
  133. return false;
  134. }
  135. return true;
  136. }
  137. function Print() {
  138. if(!CheckField('firstname','First name is mandatory')) {return;}
  139. if(!CheckField('lastname','Last name is mandatory')) {return;}
  140. if(!CheckField('gender','Gender is mandatory')) {return;}
  141. if(!CheckField('address','Address is mandatory')) {return;}
  142. print();
  143. }
  144. EOD;
  145. // Add Javascript code
  146. $pdf->IncludeJS($js);
  147. // ---------------------------------------------------------
  148. //Close and output PDF document
  149. $pdf->Output('example_014.pdf', 'I');
  150. //============================================================+
  151. // END OF FILE
  152. //============================================================+