example_016.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. //============================================================+
  3. // File name : example_016.php
  4. // Begin : 2008-03-04
  5. // Last Update : 2013-05-14
  6. //
  7. // Description : Example 016 for TCPDF class
  8. // Document Encryption / Security
  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: Document Encryption / Security
  22. * @author Nicola Asuni
  23. * @since 2008-03-04
  24. * @group security
  25. * @group pdf
  26. */
  27. // Include the main TCPDF library (search for installation path).
  28. require_once('tcpdf_include.php');
  29. // create new PDF document
  30. $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  31. // *** Set PDF protection (encryption) *********************
  32. /*
  33. The permission array is composed of values taken from the following ones (specify the ones you want to block):
  34. - print : Print the document;
  35. - modify : Modify the contents of the document by operations other than those controlled by 'fill-forms', 'extract' and 'assemble';
  36. - copy : Copy or otherwise extract text and graphics from the document;
  37. - annot-forms : Add or modify text annotations, fill in interactive form fields, and, if 'modify' is also set, create or modify interactive form fields (including signature fields);
  38. - fill-forms : Fill in existing interactive form fields (including signature fields), even if 'annot-forms' is not specified;
  39. - extract : Extract text and graphics (in support of accessibility to users with disabilities or for other purposes);
  40. - assemble : Assemble the document (insert, rotate, or delete pages and create bookmarks or thumbnail images), even if 'modify' is not set;
  41. - print-high : Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this is not set, printing is limited to a low-level representation of the appearance, possibly of degraded quality.
  42. - owner : (inverted logic - only for public-key) when set permits change of encryption and enables all other permissions.
  43. If you don't set any password, the document will open as usual.
  44. If you set a user password, the PDF viewer will ask for it before displaying the document.
  45. The master (owner) password, if different from the user one, can be used to get full document access.
  46. Possible encryption modes are:
  47. 0 = RSA 40 bit
  48. 1 = RSA 128 bit
  49. 2 = AES 128 bit
  50. 3 = AES 256 bit
  51. NOTES:
  52. - To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
  53. - To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
  54. - To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
  55. */
  56. $pdf->setProtection(array('print', 'copy'), '', null, 0, null);
  57. // Example with public-key
  58. // To open the document you need to install the private key (tcpdf.p12) on the Acrobat Reader. The password is: 1234
  59. //$pdf->setProtection($permissions=array('print', 'copy'), $user_pass='', $owner_pass=null, $mode=1, $pubkeys=array(array('c' => 'file://../config/cert/tcpdf.crt', 'p' => array('print'))));
  60. // *********************************************************
  61. // set document information
  62. $pdf->setCreator(PDF_CREATOR);
  63. $pdf->setAuthor('Nicola Asuni');
  64. $pdf->setTitle('TCPDF Example 016');
  65. $pdf->setSubject('TCPDF Tutorial');
  66. $pdf->setKeywords('TCPDF, PDF, example, test, guide');
  67. // set default header data
  68. $pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 016', PDF_HEADER_STRING);
  69. // set header and footer fonts
  70. $pdf->setHeaderFont(Array('helvetica', '', PDF_FONT_SIZE_MAIN));
  71. $pdf->setFooterFont(Array('helvetica', '', PDF_FONT_SIZE_DATA));
  72. // set default monospaced font
  73. $pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  74. // set margins
  75. $pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  76. $pdf->setHeaderMargin(PDF_MARGIN_HEADER);
  77. $pdf->setFooterMargin(PDF_MARGIN_FOOTER);
  78. // set auto page breaks
  79. $pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  80. // set image scale factor
  81. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  82. // set some language-dependent strings (optional)
  83. if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
  84. require_once(dirname(__FILE__).'/lang/eng.php');
  85. $pdf->setLanguageArray($l);
  86. }
  87. // ---------------------------------------------------------
  88. // set font
  89. $pdf->setFont('times', '', 16);
  90. // add a page
  91. $pdf->AddPage();
  92. // set some text to print
  93. $txt = <<<EOD
  94. Encryption Example
  95. Consult the source code documentation for the SetProtection() method.
  96. EOD;
  97. // print a block of text using Write()
  98. $pdf->Write(0, $txt, '', 0, 'L', true, 0, false, false, 0);
  99. // ---------------------------------------------------------
  100. //Close and output PDF document
  101. $pdf->Output('example_016.pdf', 'D');
  102. //============================================================+
  103. // END OF FILE
  104. //============================================================+