public function __construct($str = null, $sz = null, $w = null, $h = null, $i = null)
{
// Use default settings for a new PDF page.
if (null === $str) {
// If no arguments are passed, default to the Letter size.
if (null === $sz && null === $w && null === $h) {
$this->width = $this->sizes['Letter']['width'];
$this->height = $this->sizes['Letter']['height'];
} else {
// Check for a default size setting.
if (null !== $sz) {
if (array_key_exists($sz, $this->sizes)) {
$this->width = $this->sizes[$sz]['width'];
$this->height = $this->sizes[$sz]['height'];
} else {
// Else, assign a custom width and height.
if (null === $w && null !== $h || null !== $w && null === $h) {
throw new Exception('Error: A width and height must be passed.');
}
$this->width = $w;
$this->height = $h;
}
} else {
// Else, assign a custom width and height.
if (null === $w && null !== $h || null !== $w && null === $h) {
throw new Exception('Error: A width and height must be passed.');
}
$this->width = $w;
$this->height = $h;
}
}
if (null === $i) {
throw new Exception('Error: A page index must be passed.');
}
$this->index = $i;
$this->data = "\n[{page_index}] 0 obj\n<</Type/Page/Parent [{parent}] 0 R[{annotations}]/MediaBox[0 0 {$this->width} {$this->height}]/Contents[[{content_objects}]]/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI][{xobjects}][{fonts}]>>>>\nendobj\n";
} else {
// Else, determine the page object index.
$this->index = substr($str, 0, strpos($str, ' '));
// If present, record and record any thumb object index, as the contents of the page may change.
if (strpos($str, '/Thumb') !== false) {
$t = substr($str, strpos($str, 'Thumb'));
$t = substr($t, 0, strpos($t, '/'));
$t = str_replace('Thumb', '', $t);
$t = str_replace('0 R', '', $t);
$t = str_replace(' ', '', $t);
$this->thumb = $t;
}
// Determine the page parent object index.
$par = substr($str, strpos($str, 'Parent'));
$par = substr($par, 0, strpos($par, '/'));
$par = str_replace('Parent', '', $par);
$par = str_replace('0 R', '', $par);
$par = str_replace(' ', '', $par);
$this->parent = $par;
// Determine the page width and height.
$wh = substr($str, strpos($str, 'MediaBox'));
$wh = substr($wh, 0, strpos($wh, ']') + 1);
$wh = strpos($wh, 'MediaBox [') !== false ? str_replace('MediaBox [', '', $wh) : str_replace('MediaBox[', '', $wh);
$wh = str_replace(']', '', $wh);
$whAry = explode(' ', $wh);
$this->width = $whAry[2];
$this->height = $whAry[3];
// Determine the page content objects.
$cn = substr($str, strpos($str, 'Contents'));
if (strpos($cn, '/') !== false) {
$cn = substr($cn, 0, strpos($cn, '/'));
} else {
if (strpos($cn, '>') !== false) {
$cn = substr($cn, 0, strpos($cn, '>'));
}
}
$cn = str_replace('Contents', '', $cn);
if (strpos($cn, '[') !== false) {
$cn = substr($cn, 0, strpos($cn, ']') + 1);
$str = str_replace('/Contents' . $cn, '/Contents[{content_objects}]', $str);
$cn = str_replace('[', '', $cn);
$cn = str_replace(']', '', $cn);
$cn = str_replace('0 R', '|', $cn);
$cn = str_replace(' ', '', $cn);
$cn = explode('|', $cn);
foreach ($cn as $value) {
if ($value != '') {
$this->content[] = $value;
}
}
} else {
$str = str_replace('/Contents' . $cn, '/Contents[{content_objects}]', $str);
$cn = str_replace('0 R', '', $cn);
$cn = str_replace(' ', '', $cn);
$this->content[] = $cn;
}
// If they exist, determine the page annotation objects.
if (strpos($str, '/Annots') !== false) {
$an = substr($str, strpos($str, 'Annots'));
$an = substr($an, 0, strpos($an, '/'));
$an = str_replace('Annots', '', $an);
if (strpos($an, '[') !== false) {
$an = substr($an, 0, strpos($an, ']') + 1);
$str = str_replace('/Annots' . $an, '[{annotations}]', $str);
$an = str_replace('[', '', $an);
$an = str_replace(']', '', $an);
$an = str_replace('0 R', '|', $an);
$an = str_replace(' ', '', $an);
$an = explode('|', $an);
foreach ($an as $value) {
if ($value != '') {
$this->annots[] = $value;
}
}
} else {
$an = substr($an, 0, strpos($an, '/'));
$str = str_replace('/Annots' . $an, '[{annotations}]', $str);
$an = str_replace('0 R', '', $an);
$an = str_replace(' ', '', $an);
$this->annots[] = $an;
}
}
// If they exist, determine the page fonts.
if (strpos($str, '/Font') !== false) {
$ft = substr($str, strpos($str, 'Font'));
$ft = substr($ft, 0, strpos($ft, '>>') + 2);
$str = str_replace('/' . $ft, '[{fonts}]', $str);
$ft = str_replace('Font<<', '', $ft);
$ft = str_replace('>>', '', $ft);
$ft = explode('/', $ft);
foreach ($ft as $value) {
if ($value != '') {
$this->fonts[] = '/' . $value;
}
}
}
// If they exist, determine the page xobjects.
if (strpos($str, '/XObject') !== false) {
$xo = substr($str, strpos($str, 'XObject'));
$xo = substr($xo, 0, strpos($xo, '>>') + 2);
$str = str_replace('/' . $xo, '[{xobjects}]', $str);
$xo = str_replace('XObject<<', '', $xo);
$xo = str_replace('>>', '', $xo);
$xo = explode('/', $xo);
foreach ($xo as $value) {
if ($value != '') {
$this->xobjs[] = '/' . $value;
}
}
}
// If they exist, determine the page graphic states.
if (strpos($str, '/ExtGState') !== false) {
$gs = substr($str, strpos($str, 'ExtGState'));
$gs = substr($gs, 0, strpos($gs, '>>') + 2);
//$str = str_replace('/' . $gs, '', $str);
$gs = '/' . $gs;
} else {
$gs = '';
}
// If any groups exist
if (strpos($str, '/Group') !== false) {
$grp = substr($str, strpos($str, 'Group'));
$grp = substr($grp, 0, strpos($grp, '>>') + 2);
$grp = '/' . $grp;
} else {
$grp = '';
}
// If resources exists
if (strpos($str, '/Resources') !== false) {
$res = substr($str, strpos($str, 'Resources'));
if (strpos($res, '0 R') !== false) {
$res = substr($res, 0, strpos($res, '0 R') + 3);
$res = '/' . $res;
} else {
if (strpos($res, '>>') !== false) {
$res = substr($res, 0, strpos($res, '>>') + 2);
$res = '/' . $res;
} else {
$res = "/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI][{xobjects}][{fonts}]{$gs}>>";
}
}
} else {
$res = "/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI][{xobjects}][{fonts}]{$gs}>>";
}
if (substr_count($res, '<<') > substr_count($res, '>>')) {
$res .= str_repeat('>>', substr_count($res, '<<') - substr_count($res, '>>'));
}
$this->data = "\n[{page_index}] 0 obj\n<</Type/Page/Parent [{parent}] 0 R[{annotations}]/MediaBox[0 0 {$this->width} {$this->height}]{$grp}/Contents[[{content_objects}]]{$res}>>\nendobj\n";
}
}