You are not logged in.

  • Login

1

Monday, September 26th 2011, 6:10pm

PDF in PHP ausgeben

Guten Tag,
Ich suche eine Funktion das ich eine PDF in PHP anzeigen kann! Kennt sich da jemand aus! Es soll eine eigene Funktion sein!

3

Monday, September 26th 2011, 7:29pm

PHP Quellcode

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
// Author: http://www.webcheatsheet.com/php/reading_clean_text_from_pdf.php
function pdf2text($filename) { 
 
    // Read the data from pdf file
    $infile = @file_get_contents($filename, FILE_BINARY); 
    if (empty($infile)) 
        return ""; 
 
    // Get all text data.
    $transformations = array(); 
    $texts = array(); 
 
    // Get the list of all objects.
    preg_match_all("#obj(.*)endobj#ismU", $infile, $objects); 
    $objects = @$objects[1]; 
 
    // Select objects with streams.
    for ($i = 0; $i < count($objects); $i++) { 
        $currentObject = $objects[$i]; 
 
        // Check if an object includes data stream.
        if (preg_match("#stream(.*)endstream#ismU", $currentObject, $stream)) { 
            $stream = ltrim($stream[1]); 
 
            // Check object parameters and look for text data. 
            $options = getObjectOptions($currentObject); 
            if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"]))) 
                continue; 
 
            // So, we have text data. Decode it.
            $data = getDecodedStream($stream, $options);  
            if (strlen($data)) { 
                if (preg_match_all("#BT(.*)ET#ismU", $data, $textContainers)) { 
                    $textContainers = @$textContainers[1]; 
                    getDirtyTexts($texts, $textContainers); 
                } else 
                    getCharTransformations($transformations, $data); 
            } 
        } 
 
    } 
 
    // Analyze text blocks taking into account character transformations and return results. 
    return getTextUsingTransformations($texts, $transformations); 
}


Für das nächste Mal: Selber suchen macht schlau.

4

Tuesday, September 27th 2011, 7:36am

Hallo,
Ich wollte Ihnen mitteilen das es nicht funktioniert!
Queltext:

PHP Quellcode

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
<?php
 
 
function pdf2text($filename) { 
 
    // Read the data from pdf file
    $infile = @file_get_contents($filename, FILE_BINARY); 
    if (empty($infile)) 
        return ""; 
 
    // Get all text data.
    $transformations = array(); 
    $texts = array(); 
 
    // Get the list of all objects.
    preg_match_all("#obj(.*)endobj#ismU", $infile, $objects); 
    $objects = @$objects[1]; 
 
    // Select objects with streams.
    for ($i = 0; $i < count($objects); $i++) { 
        $currentObject = $objects[$i]; 
 
        // Check if an object includes data stream.
        if (preg_match("#stream(.*)endstream#ismU", $currentObject, $stream)) { 
            $stream = ltrim($stream[1]); 
 
            // Check object parameters and look for text data. 
            $options = getObjectOptions($currentObject); 
            if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"]))) 
                continue; 
 
            // So, we have text data. Decode it.
            $data = getDecodedStream($stream, $options);  
            if (strlen($data)) { 
                if (preg_match_all("#BT(.*)ET#ismU", $data, $textContainers)) { 
                    $textContainers = @$textContainers[1]; 
                    getDirtyTexts($texts, $textContainers); 
                } else 
                    getCharTransformations($transformations, $data); 
            } 
        } 
 
    } 
 
    // Analyze text blocks taking into account character transformations and return results. 
    return getTextUsingTransformations($texts, $transformations); 
}
 
 echo pdf2text("test.pdf");
?>

5

Tuesday, September 27th 2011, 8:42am

Hallo,
Ich wollte Ihnen mitteilen das es nicht funktioniert!

:thumbsup:


Hat sich das Problem gelöst ? Wenn nicht, dann stell bitte eine Frage und wenn etwas nicht funktioniert, dann beschreib bitte was genau nicht funktioniert.


Es könnte sein, dass die Funktion nicht funktioniert, da noch Abhängigkeiten bestehen. Auf der verlinkten Seite von bastey gibt es aber die vollständige Source.

PHP Quellcode

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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
 
function decodeAsciiHex($input) {
    $output = "";
 
    $isOdd = true;
    $isComment = false;
 
    for($i = 0, $codeHigh = -1; $i < strlen($input) && $input[$i] != '>'; $i++) {
        $c = $input[$i];
 
        if($isComment) {
            if ($c == '\r' || $c == '\n')
                $isComment = false;
            continue;
        }
 
        switch($c) {
            case '\0': case '\t': case '\r': case '\f': case '\n': case ' ': break;
            case '%': 
                $isComment = true;
            break;
 
            default:
                $code = hexdec($c);
                if($code === 0 && $c != '0')
                    return "";
 
                if($isOdd)
                    $codeHigh = $code;
                else
                    $output .= chr($codeHigh * 16 + $code);
 
                $isOdd = !$isOdd;
            break;
        }
    }
 
    if($input[$i] != '>')
        return "";
 
    if($isOdd)
        $output .= chr($codeHigh * 16);
 
    return $output;
}
function decodeAscii85($input) {
    $output = "";
 
    $isComment = false;
    $ords = array();
 
    for($i = 0, $state = 0; $i < strlen($input) && $input[$i] != '~'; $i++) {
        $c = $input[$i];
 
        if($isComment) {
            if ($c == '\r' || $c == '\n')
                $isComment = false;
            continue;
        }
 
        if ($c == '\0' || $c == '\t' || $c == '\r' || $c == '\f' || $c == '\n' || $c == ' ')
            continue;
        if ($c == '%') {
            $isComment = true;
            continue;
        }
        if ($c == 'z' && $state === 0) {
            $output .= str_repeat(chr(0), 4);
            continue;
        }
        if ($c < '!' || $c > 'u')
            return "";
 
        $code = ord($input[$i]) & 0xff;
        $ords[$state++] = $code - ord('!');
 
        if ($state == 5) {
            $state = 0;
            for ($sum = 0, $j = 0; $j < 5; $j++)
                $sum = $sum * 85 + $ords[$j];
            for ($j = 3; $j >= 0; $j--)
                $output .= chr($sum >> ($j * 8));
        }
    }
    if ($state === 1)
        return "";
    elseif ($state > 1) {
        for ($i = 0, $sum = 0; $i < $state; $i++)
            $sum += ($ords[$i] + ($i == $state - 1)) * pow(85, 4 - $i);
        for ($i = 0; $i < $state - 1; $i++)
            $ouput .= chr($sum >> ((3 - $i) * 8));
    }
 
    return $output;
}
function decodeFlate($input) {
    return @gzuncompress($input);
}
 
function getObjectOptions($object) {
    $options = array();
    if (preg_match("#<<(.*)>>#ismU", $object, $options)) {
        $options = explode("/", $options[1]);
        @array_shift($options);
 
        $o = array();
        for ($j = 0; $j < @count($options); $j++) {
            $options[$j] = preg_replace("#\s+#", " ", trim($options[$j]));
            if (strpos($options[$j], " ") !== false) {
                $parts = explode(" ", $options[$j]);
                $o[$parts[0]] = $parts[1];
            } else
                $o[$options[$j]] = true;
        }
        $options = $o;
        unset($o);
    }
 
    return $options;
}
function getDecodedStream($stream, $options) {
    $data = "";
    if (empty($options["Filter"]))
        $data = $stream;
    else {
        $length = !empty($options["Length"]) ? $options["Length"] : strlen($stream);
        $_stream = substr($stream, 0, $length);
 
        foreach ($options as $key => $value) {
            if ($key == "ASCIIHexDecode")
                $_stream = decodeAsciiHex($_stream);
            if ($key == "ASCII85Decode")
                $_stream = decodeAscii85($_stream);
            if ($key == "FlateDecode")
                $_stream = decodeFlate($_stream);
        }
        $data = $_stream;
    }
    return $data;
}
function getDirtyTexts(&$texts, $textContainers) {
    for ($j = 0; $j < count($textContainers); $j++) {
        if (preg_match_all("#\[(.*)\]\s*TJ#ismU", $textContainers[$j], $parts))
            $texts = array_merge($texts, @$parts[1]);
        elseif(preg_match_all("#Td\s*(\(.*\))\s*Tj#ismU", $textContainers[$j], $parts))
            $texts = array_merge($texts, @$parts[1]);
    }
}
function getCharTransformations(&$transformations, $stream) {
    preg_match_all("#([0-9]+)\s+beginbfchar(.*)endbfchar#ismU", $stream, $chars, PREG_SET_ORDER);
    preg_match_all("#([0-9]+)\s+beginbfrange(.*)endbfrange#ismU", $stream, $ranges, PREG_SET_ORDER);
 
    for ($j = 0; $j < count($chars); $j++) {
        $count = $chars[$j][1];
        $current = explode("\n", trim($chars[$j][2]));
        for ($k = 0; $k < $count && $k < count($current); $k++) {
            if (preg_match("#<([0-9a-f]{2,4})>\s+<([0-9a-f]{4,512})>#is", trim($current[$k]), $map))
                $transformations[str_pad($map[1], 4, "0")] = $map[2];
        }
    }
    for ($j = 0; $j < count($ranges); $j++) {
        $count = $ranges[$j][1];
        $current = explode("\n", trim($ranges[$j][2]));
        for ($k = 0; $k < $count && $k < count($current); $k++) {
            if (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+<([0-9a-f]{4})>#is", trim($current[$k]), $map)) {
                $from = hexdec($map[1]);
                $to = hexdec($map[2]);
                $_from = hexdec($map[3]);
 
                for ($m = $from, $n = 0; $m <= $to; $m++, $n++)
                    $transformations[sprintf("%04X", $m)] = sprintf("%04X", $_from + $n);
            } elseif (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+\[(.*)\]#ismU", trim($current[$k]), $map)) {
                $from = hexdec($map[1]);
                $to = hexdec($map[2]);
                $parts = preg_split("#\s+#", trim($map[3]));
 
                for ($m = $from, $n = 0; $m <= $to && $n < count($parts); $m++, $n++)
                    $transformations[sprintf("%04X", $m)] = sprintf("%04X", hexdec($parts[$n]));
            }
        }
    }
}
function getTextUsingTransformations($texts, $transformations) {
    $document = "";
    for ($i = 0; $i < count($texts); $i++) {
        $isHex = false;
        $isPlain = false;
 
        $hex = "";
        $plain = "";
        for ($j = 0; $j < strlen($texts[$i]); $j++) {
            $c = $texts[$i][$j];
            switch($c) {
                case "<":
                    $hex = "";
                    $isHex = true;
                break;
                case ">":
                    $hexs = str_split($hex, 4);
                    for ($k = 0; $k < count($hexs); $k++) {
                        $chex = str_pad($hexs[$k], 4, "0");
                        if (isset($transformations[$chex]))
                            $chex = $transformations[$chex];
                        $document .= html_entity_decode("&#x".$chex.";");
                    }
                    $isHex = false;
                break;
                case "(":
                    $plain = "";
                    $isPlain = true;
                break;
                case ")":
                    $document .= $plain;
                    $isPlain = false;
                break;
                case "\\":
                    $c2 = $texts[$i][$j + 1];
                    if (in_array($c2, array("\\", "(", ")"))) $plain .= $c2;
                    elseif ($c2 == "n") $plain .= '\n';
                    elseif ($c2 == "r") $plain .= '\r';
                    elseif ($c2 == "t") $plain .= '\t';
                    elseif ($c2 == "b") $plain .= '\b';
                    elseif ($c2 == "f") $plain .= '\f';
                    elseif ($c2 >= '0' && $c2 <= '9') {
                        $oct = preg_replace("#[^0-9]#", "", substr($texts[$i], $j + 1, 3));
                        $j += strlen($oct) - 1;
                        $plain .= html_entity_decode("&#".octdec($oct).";");
                    }
                    $j++;
                break;
 
                default:
                    if ($isHex)
                        $hex .= $c;
                    if ($isPlain)
                        $plain .= $c;
                break;
            }
        }
        $document .= "\n";
    }
 
    return $document;
}
 
function pdf2text($filename) {
    $infile = @file_get_contents($filename, FILE_BINARY);
    if (empty($infile))
        return "";
 
    $transformations = array();
    $texts = array();
 
    preg_match_all("#obj(.*)endobj#ismU", $infile, $objects);
    $objects = @$objects[1];
 
    for ($i = 0; $i < count($objects); $i++) {
        $currentObject = $objects[$i];
 
        if (preg_match("#stream(.*)endstream#ismU", $currentObject, $stream)) {
            $stream = ltrim($stream[1]);
 
            $options = getObjectOptions($currentObject);
            if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])))
                continue;
 
            $data = getDecodedStream($stream, $options); 
            if (strlen($data)) {
                if (preg_match_all("#BT(.*)ET#ismU", $data, $textContainers)) {
                    $textContainers = @$textContainers[1];
                    getDirtyTexts($texts, $textContainers);
                } else
                    getCharTransformations($transformations, $data);
            }
        }
    }
 
    return getTextUsingTransformations($texts, $transformations);
}
?>

6

Wednesday, September 28th 2011, 3:23pm

"Stolpern fördert" ;)

Similar threads

Social bookmarks