Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for readword_php

(view source code of readword_php as plain text)

  1. <?php
  2. $progver = "1.00";
  3.  
  4. $commandline = ( !isset( $_SERVER ) || !isset( $_SERVER['REMOTE_ADDR'] ) );
  5.  
  6.  
  7. function read_word( $wordfile ) {
  8. 	global $commandline;
  9.  
  10. 	$errormessage = "Unable to instantiate Word";
  11. 	if ( !$commandline ) {
  12. 		$errormessage = "<p>{$errormessage}</p>\n\n";
  13. 	}
  14. 	$word = new com( "Word.Application" ) or die( $errormessage );
  15.  
  16. 	$wordversion = "[Microsoft Word,  Version {$word->Version}]";
  17. 	if ( !$commandline ) {
  18. 		$wordversion = "<p style=\"color: silver;\">{$wordversion}</p>\n\n";
  19. 	}
  20. 	print( $wordversion );
  21.  
  22. 	try {
  23. 		$word->Visible = 1;
  24. 		$document = $word->Documents->open( $wordfile );
  25. 		if ( $commandline ) {
  26. 			print( "[{$_POST['filename']}]\n\n" );
  27. 		} else {
  28. 			print( "<p style=\"color: silver;\">[{$_POST['filename']}]</p>\n\n" );
  29. 		}
  30. 		if ( $word->Documents->count( ) == 1 ) {
  31. 			foreach ( $document->Sentences as $sentence ) {
  32. 				if ( $commandline ) {
  33. 					print( "{$sentence}\n\n" );
  34. 				} else {
  35. 					print( "<p>{$sentence}</p>\n\n" );
  36. 				}
  37. 			}
  38. 		}
  39. 	} catch ( Exception $e ) {
  40. 		if ( $commandline ) {
  41. 			print( "{$e->getMessage( )}\n\n" );
  42. 		} else {
  43. 			print( "<pre>{$e->getMessage( )}</pre>\n" );
  44. 		}
  45. 	} finally {
  46. 		if ( $word->Documents->count( ) > 0 ) {
  47. 			$word->Documents->close( );
  48. 		}
  49. 		$word->Quit( );
  50. 		$word = null;
  51. 	}
  52. }
  53.  
  54.  
  55. function show_help( ) {
  56. 	global $progver;
  57. 	print( "\nreadword.php, Version {$progver} for Windows\n" );
  58. 	print( "Read a Word document and display the text in a console or web page\n\n" );
  59. 	print( "Usage:  PHP  \"" . $_SERVER['SCRIPT_NAME'] . "\"  word_doc\n\n" );
  60. 	print( "   or:  [ START ]  http://example.com/" . basename( __FILE__ ) . "\n\n" );
  61. 	print( "Where:  word_doc is the full path to an MS Word document\n" );
  62. 	print( "        http://example.com/" . basename( __FILE__ ) . " is the url to this script.\n\n" );
  63. 	print( "Notes:  When run on the command line, this script requires a Word document\n" );
  64. 	print( "        as its only command line argument; when run in a browser, the file\n" );
  65. 	print( "        name has to be entered interactively.\n" );
  66. 	print( "        In command line mode only the first argument is used, any excess\n" );
  67. 	print( "        arguments will be ignored.\n" );
  68. 	print( "        This Windows-only script requires MS Word to read the Word document.\n\n" );
  69. 	print( "Written by Rob van der Woude\n" );
  70. 	print( "https://www.robvanderwoude.com\n" );
  71. }
  72.  
  73.  
  74. if ( $commandline ) {
  75. 	array_shift( $argv );
  76. 	if ( count( $argv ) == 0 || $argv[0] == "/?" ) {
  77. 		show_help( );
  78. 	} else {
  79. 		read_word( $argv[0] );
  80. 	}
  81. } else {
  82. 	print( "<html>\n" );
  83. 	print( "<head>\n" );
  84. 	print( "<title>Read Word document in web page with PHP by Rob van der Woude</title>\n" );
  85. 	print( "</head>\n" );
  86. 	print( "<body>\n\n" );
  87.  
  88. 	print( "<form action=\"{$_SERVER['SCRIPT_NAME']}\" method=\"POST\">\n" );
  89. 	print( "<input name=\"loadfile\" type=\"hidden\" value=\"1\" />\n" );
  90.  
  91. 	if ( isset( $_POST ) && isset( $_POST['loadfile'] ) && $_POST['loadfile'] == '1' && isset( $_POST['filename'] ) && trim( $_POST['filename'] ) != '' ) {
  92. 		read_word( $_POST['filename'] );
  93. 	} else {
  94. 		print( '<p title="Enter the full path for the Word document and click \'OK\'">Word file name: ' );
  95. 		print( '<input type="text" name="filename" />' );
  96. 		print( ' ' );
  97. 		print( '<input type="submit" value="OK" /></p>' );
  98. 	}
  99.  
  100. 	print( "</form>\n\n" );
  101. 	print( "</body>\n" );
  102. 	print( "</html>" );
  103. }
  104. ?>

page last modified: 2024-02-26; loaded in 0.0270 seconds