Hi all.
Apologies. This is hopefully a stupid and easily answered Q. However, I'm lost, and need a bit of guidance...
I have pecl-ps installed. The following code woks fine, and produces a very simple PS page:
<?php
$doc = PS_new();
if (!PS_open_file($doc, '-')) { die; }
PS_set_parameter($doc, 'warning', 'true');
PS_set_info($doc, 'boundingbox', '0 0 596 842');
PS_set_info($doc,'Title', 'A Hello World Example');
PS_set_parameter($doc, 'SearchPath', '/usr/share/fonts/default/ghostscript');
PS_begin_page($doc, 596, 842);
$font = PS_findfont($doc,'n021023l', '', 0);
PS_setfont($doc, $font, 58.0);
PS_set_value($doc,'charspacing', 2);
PS_set_text_pos($doc, 10,10);
PS_show_xy($doc, 'Hello World', 50,50);
PS_setlinewidth($doc, 2);
PS_moveto($doc, 50, 50);
PS_lineto($doc, 100,100);
PS_stroke($doc);
ps_end_page($doc);
?>
Now, what I want to do is get most of this into a class. Thus, my code would become:
<?php
class PS {
var $ps;
public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false){
$this->ps = PS_new();
if (!PS_open_file($this->ps, '-')) { die; }
// Construct PS file as above...
}
public function Output($name='doc.ps', $dest='I') {
echo $this->getBuffer();
}
protected function getBuffer() {
return ps_get_buffer($this->ps);
}
}
$doc = new PS('P', 'mm', 'A4', true, 'UTF-8', false);
// do stuff
$doc->Output('example.ps', 'I');
?>
Which again, works OK. However, obviously, what I want is the actual document writing code in a mass of other functions, and this is where I am failing - miserably! Any attempt to shift the 'construct PS' code elsewhere results in an error:
<?php
require_once('above-class.inc');
$ps = new PS('P', 'mm', 'A5', true, 'UTF-8', false);
PS_begin_page($ps->ps, 596, 842);
// or, perhaps
$ps->WriteText($ps->ps, 'Hello World', 100, 100);
... more of the same ...
$ps->Output('example_010.ps', 'I');
?>
Warning: ps_get_buffer(): 3 is not a valid ps document resource ...
As you can probably tell, this is my first attempt at building a class, so a pointer on where I am going wrong, and how I can fix it, would be very much appreciated.
Many thanks.
Phil.