|
 275.1 Кб |
|
| Здравствуйте.
Есть у меня задача вытянуть структуру pdf файла. Он представляет из себя набор объектов со ссылками на другие объекты. Я пытаюсь сделать так же, только через массивы.
Делаю так:
<?
class PDF {
var $pdf_struct = array();
function __construct($name) {
if (!$name) return false;
$file = file_get_contents($name);
if (preg_match_all('|(\d+)\s(\d+)\sobj(.*)endobj|ismU', $file, $pdf)) {
$elements_count = count($pdf[0]);
for ($i = 0; $i < $elements_count; ++$i) {
$obj = $pdf[3][$i];
$this->pdf_struct[$pdf[1][$i]] = array();
$this->getDictionaryInto($obj, $this->pdf_struct[$pdf[1][$i]], $pdf[2][$i]);
}
}
}
function getDictionaryInto($obj, &$target, $name) {
$obj = trim($obj);
if (preg_match('|<<(.+)>>(.*)|ism', $obj, $dictionary)) {
preg_match_all('#/([\w;\*\?\$\@\+]+)\s?(<<.*>>|\d+\s\d+\sR|\d+|/[\w;\*\?\$\@]+)+?#i', $dictionary[1], $out);
$target[$name] = array();
for ($i = 0; $i < count($out[1]); ++$i) {
$this->getDictionaryInto($out[2][$i], $target[$name], $out[1][$i]);
}
} elseif (preg_match('|(\d+)\s(\d+)\sR|i', $obj, $dictionary)) {
$dictionary[1] = intval($dictionary[1]);
$dictionary[2] = intval($dictionary[2]);
$target[$name] = &$this->pdf_struct[$dictionary[1]][$dictionary[2]];
} elseif ($obj[0] == '/' || is_numeric($obj)) {
$target[$name] = $obj;
}
}
}
$pdf = new PDF('gost.pdf');
print_r($pdf->pdf_struct);
?>
|
В итоге получаю примерно такое (из pdf в аттаче):
Array
(
[1] => Array
(
[0] => Array
(
[Parent] => Array
(
[Parent] =>
[Count] => 10
[Type] => /Pages
)
[Contents] =>
[SeparationInfo] => Array
(
[Pages] => Array
(
[Parent] =>
[Contents] =>
[SeparationInfo] =>
[Resources] =>
[Type] => /Page
)
[DeviceColorant] => /Black
)
...
|
Все элементы "Parent", "Contents" и еще некоторые должны быть не пустые (они ссылаются на другие элементы массива). В тут некоторые заполнены, а некоторые пустые. Причем, если потом сделать
$pdf->pdf_struct[1][0]['Contents'] = &$pdf->pdf_struct[2][0];
| , то массив правильно отобразится в этом месте.
Почему
$target[$name] = &$this->pdf_struct[$dictionary[1]][$dictionary[2]];
| не всегда срабатывает? | |
|
|