Informationen aus PDFs mit PHP lesen
Das nachfolgende Script war nur ein Test, ob es überhaupt möglich ist mit PHP diverse Informationen aus PDF-Files auszulesen. Nun, es ist in manchen Fällen möglich, womit es aber zusammenhängt konnte ich leider nicht herausfinden.
Die Metainformationen, wie Autor oder Titel kann man recht oft extrahieren, bei dem eigentlichen Text wird es schwierig. Aber vielleicht hilft das hier ja jemandem, das Problem weiter zu bearbeiten.
Code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
<?
// get file over URL?file=
$file=pdf2text($_GET['file']);
foreach(
$file as $k => $v)
{
 echo 
"$k: $v <br>";
}


function 
pdf2text($source)
{
 
// read pdf-file
 
if(function_exists('file_get_contents'))
 {
  
$content=file_get_contents($source);
 }else{
  
$content='';
  
$handle=fopen($source,'r');
  while(!
feof($handle))
  {
   
$line=fgets($handle4096);
   
$content.=$line;
  }
  
fclose($handle);
 }
 
 
$content2=array();
 
// title of file
 
if(preg_match_all('|/Title(.?)((.*))|Ui',$content,$title))
 {
  
$content2['Title']=$title[2][0];
 }

 
// subject of file
 
if(preg_match_all('|/Subject(.?)((.*))|Ui',$content,$subject))
 {
  
$content2['Subject']=$subject[2][0];
 }
 
 
// creator of file
 
if(preg_match_all('|/Creator(.?)((.*))|Ui',$content,$creator))
 {
  
$content2['Creator']=$creator[2][0];
 }
 
 
// producer of file
 
if(preg_match_all('|/Producer(.?)((.*))|Ui',$content,$producer))
 {
  
$content2['Producer']=$producer[2][0];
 }
 
 
// author of file
 
if(preg_match_all('|/Author(.?)((.*))|Ui',$content,$author))
 {
  
$content2['Author']=$author[2][0];
 }
 
 
// find stream-sections
 // uses part of the function pdf2string from <a href="http://de.php.net/manual/de/ref.pdf.php#56492<br">http://de.php.net/manual/de/ref.pdf.php#56492<br</a> />
 
$searchstart 'stream';
 
$searchend 'endstream';
 
$text '';
 
$pos 0;
 
$pos2 0;
 
$startpos 0;
 while (
$pos !== false && $pos2 !== false)
 {
  
$pos strpos($content$searchstart$startpos);
  
$pos2 strpos($content$searchend$startpos 1);

  if(
$pos !== false && $pos2 !== false)
  {
   if(
$content[$pos] == 0x0d && $content[$pos 1] == 0x0a)
   {
    
$pos += 2;
   }else if (
$content[$pos] == 0x0a){
    
$pos++;
   }

   if(
$content[$pos2 2] == 0x0d && $content[$pos2 1] == 0x0a)
   {
    
$pos2 -= 2;
   }else if (
$content[$pos2 1] == 0x0a){
    
$pos2--;
   }

   
$textsection substr($content$pos strlen($searchstart) + 2$pos2 $pos strlen($searchstart) - 1);
   
/*if(function_exists('convert_uudecode'))
   {
    $data= convert_uudecode($textsection);
   }else*/
{
    
$data = @gzuncompress($textsection);
   }

   
$text.= pdfExtractText($data);
   
$startpos $pos2 strlen($searchend) - 1;
  }
 }
 
$content2['Text']=$text;
 
 return 
$content2;
}



function 
pdfExtractText($data)
{
 
$data=preg_replace('|T[cjdJm](.*)TD|Us','<br>',$data);
 
$data=str_replace('(''##BEGINBRACKET##'$data);
 
$data=str_replace('[''##BEGINSBRACKET##'$data);
 
$data=str_replace(')''##ENDBRACKET##'$data);
 
$data=str_replace(']''##ENDSBRACKET##'$data);
 
$data=str_replace('(','',$data);
 
$data=str_replace(')','',$data);

 
// Translate special characters and put back brackets.
 
$trans = array(
  
'...' => '…',
  
'205' => '…',
  
'221' => chr(145),
  
'222' => chr(146),
  
'223' => chr(147),
  
'224' => chr(148),
  
'226' => '-',
  
'200' => '�',
  
'374' => 'ü',
  
'334' => 'Ü',
  
'366' => 'ö',
  
'337' => 'ß',
  
'344' => 'ä',
  
'267' => '•',
  
'##BEGINBRACKET##' => '(',
  
'##BEGINSBRACKET##' => '[',
  
'##ENDBRACKET##' => ')',
  
'##ENDSBRACKET##' => ']',
  
chr(133) => '-',
  
chr(141) => chr(147),
  
chr(142) => chr(148),
  
chr(143) => chr(145),
  
chr(144) => chr(146),
 );
 
$data strtr($data$trans);
 
 return 
$data;
}
?>