formsubmit.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. echo '<'.'!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  7. </head>
  8. <body>
  9. <h2>Submitted data from PDF Form</h2>
  10. <p style="font-size:0.9em;">(See formsubmit.php in the Examples folder for details)</p>
  11. ';
  12. if (count($_POST)) {
  13. // To display HTML output from PDF form
  14. echo '<h4>HTML format data sent as POST</h4>';
  15. foreach($_POST AS $name=>$val) {
  16. $t = mb_convert_encoding(PDFDocEncodingToWin1252($val), 'UTF-8', 'Windows-1252' ); // If from core fonts doc
  17. echo '<p>PDFDocEnc: '.$name.' => '.$t.'</p>';
  18. }
  19. }
  20. else if (count($_GET)) {
  21. // To display HTML output from PDF form
  22. echo '<h4>HTML format data sent as GET</h4>';
  23. foreach($_GET AS $name=>$val) {
  24. $t = mb_convert_encoding(PDFDocEncodingToWin1252($val), 'UTF-8', 'Windows-1252' ); // If from core fonts doc
  25. echo '<p>PDFDocEnc: '.$name.' => '.$t.'</p>';
  26. }
  27. }
  28. else {
  29. $postdata = file_get_contents("php://input");
  30. if ($postdata) {
  31. echo '<h4>XFDF format data detected</h4>';
  32. // To parse XFDF
  33. if (preg_match_all('/<field name="([^>]*)"\s*>\s*(<value\s*>(.*?)<\/value\s*>)\s*<\/field\s*>/s', $postdata, $m)) {
  34. for($i=0; $i<count($m[0]); $i++) {
  35. // if multiple values in response e.g. from multiple selected options
  36. preg_match_all('/<value\s*>(.*?)<\/value\s*>/s', $m[2][$i], $v);
  37. if (count($v[0])>1) {
  38. $values = array();
  39. foreach($v[1] AS $val) { $values[] = $val; }
  40. //foreach($v[1] AS $val) { $values[] = htmlspecialchars_decode($val); }
  41. echo '<p>Field: '.$m[1][$i].' => [array of values] ('.implode(', ',$values).')</p>';
  42. }
  43. else {
  44. //echo '<p>Field: '.$m[1][$i].' => '.htmlspecialchars_decode($m[3][$i]).'</p>';
  45. echo '<p>Field: '.$m[1][$i].' => '.$m[3][$i].'</p>';
  46. }
  47. }
  48. }
  49. if (preg_match_all('/<field name="([^>]*)"\s*>\s*<value\s*\/\s*>\s*<\/field\s*>/s', $postdata, $m)) {
  50. for($i=0; $i<count($m[0]); $i++) {
  51. echo '<p>Field: '.$m[1][$i].' => [blank]</p>';
  52. }
  53. }
  54. if (preg_match_all('/<field name="([^>]*)"\s*\/\s*>/s', $postdata, $m)) {
  55. for($i=0; $i<count($m[0]); $i++) {
  56. echo '<p>Field: '.$m[1][$i].' => [no value]</p>';
  57. }
  58. }
  59. // To display whole XFDF
  60. //$postdata = preg_replace("/[\n\r]/", "", $postdata);
  61. //$postdata = preg_replace('/>\s*</', ">\n<", $postdata);
  62. //echo nl2br(htmlspecialchars($postdata));
  63. }
  64. else { echo "No form data detected"; }
  65. }
  66. echo '</body></html>';
  67. exit;
  68. function PDFDocEncodingToWin1252($txt) {
  69. $Win1252ToPDFDocEncoding = array(
  70. chr(0200) => chr(0240), chr(0214) => chr(0226), chr(0212) => chr(0227), chr(0237) => chr(0230),
  71. chr(0225) => chr(0200), chr(0210) => chr(0032), chr(0206) => chr(0201), chr(0207) => chr(0202),
  72. chr(0205) => chr(0203), chr(0227) => chr(0204), chr(0226) => chr(0205), chr(0203) => chr(0206),
  73. chr(0213) => chr(0210), chr(0233) => chr(0211), chr(0211) => chr(0213), chr(0204) => chr(0214),
  74. chr(0223) => chr(0215), chr(0224) => chr(0216), chr(0221) => chr(0217), chr(0222) => chr(0220),
  75. chr(0202) => chr(0221), chr(0232) => chr(0235), chr(0230) => chr(0037), chr(0231) => chr(0222),
  76. chr(0216) => chr(0231)
  77. );
  78. return strtr($txt, array_flip($Win1252ToPDFDocEncoding) );
  79. }
  80. ?>