public function __construct($pdf, $pgs = null)
{
// Read the file data from the imported PDF.
$this->data = file_get_contents($pdf);
// Strip any and all XREF tables, as the structure of the PDF will change.
while (strpos($this->data, 'xref') !== false) {
$xref = substr($this->data, 0, strpos($this->data, '%%EOF') + 5);
$xref = substr($xref, strpos($xref, 'xref'));
$this->data = str_replace($xref, '', $this->data);
}
// Get the PDF objects.
$this->getObjects($this->data);
$this->pages = $this->kids;
// If the page argument was passed, parse out the desired page(s), removing any unwanted pages and their content.
if (null !== $pgs) {
if (is_array($pgs)) {
foreach ($pgs as $value) {
$pAry[] = $this->pages[$value - 1];
}
} else {
$pAry[] = $this->pages[$pgs - 1];
}
$rm = array();
foreach ($this->pages as $value) {
if (!in_array($value, $pAry)) {
$rm[] = $value;
}
}
// Remove unwanted pages and their content from the imported data.
if (count($rm) != 0) {
foreach ($rm as $value) {
$content = substr($this->objects[$value]['data'], strpos($this->objects[$value]['data'], 'Contents'));
$content = substr($content, 0, strpos($content, '/'));
$content = str_replace('Contents', '', $content);
$content = str_replace('[', '', $content);
$content = str_replace(']', '', $content);
$content = str_replace(' 0 R', '|', $content);
$content = str_replace(' ', '', $content);
$content = substr($content, 0, -1);
$content_objs = explode('|', $content);
unset($this->objects[$value]);
if (in_array($value, $this->kids)) {
$k = array_search($value, $this->kids);
unset($this->kids[$k]);
}
foreach ($content_objs as $val) {
unset($this->objects[$val]);
}
}
$this->pages = $this->kids;
}
}
}