I'm having trouble with the stroke function in PDF. I'm getting the following error:
Fatal error: PDFlib error: function 'PDF_stroke' must not be called in 'page' scope in /home/elderhop/public_html/pdf/pdflib.php on line 168
The script is from PHP and MySQL Development.
Here's line 168:
pdf_stroke($pdf);
and, I hope this is okay, but here's the script:
<?
if(!$name||!$score)
{
echo "<h1>Error:</h1>This page was called incorrectly";
}
else
{
//generate the headers to help a browser choose the correct application
header( "Content-type: application/pdf" );
header( "Content-Disposition: filename=cert.pdf");
$date = date( "F d, Y" );
// create a pdf document in memory
$pdf = pdf_open();
// set up the page size in points
// US letter is 11" x 8.5"
// there are approximately 72 points per inch
$width = 11*72;
$height = 8.5*72;
pdf_begin_page($pdf, $width, $height);
// draw a borders
$inset = 20; // space between border and page edge
$border = 10; // width of main border line
$inner = 2; // gap within the border
//draw outer border
pdf_rect($pdf, $inset-$inner,
$inset-$inner,
$width-2*($inset-$inner),
$height-2*($inset-$inner));
pdf_stroke($pdf);
//draw main border $border points wide
pdf_setlinewidth($pdf, $border);
pdf_rect($pdf, $inset+$border/2,
$inset+$border/2,
$width-2*($inset+$border/2),
$height-2*($inset+$border/2));
pdf_stroke($pdf);
pdf_setlinewidth($pdf, 1.0);
//draw inner border
pdf_rect($pdf, $inset+$border+$inner,
$inset+$border+$inner,
$width-2*($inset+$border+$inner),
$height-2*($inset+$border+$inner));
pdf_stroke($pdf);
//add text
pdf_set_font($pdf, "Times-Roman", 48, "host");
$startx = ($width - pdf_stringwidth($pdf, "PHP Certification"))/2;
pdf_show_xy($pdf, "PHP Certification", $startx, 490);
pdf_set_font($pdf, "Times-Roman", 26, "host");
$startx = 70;
pdf_show_xy($pdf, "This is to certify that:", $startx, 430);
pdf_show_xy($pdf, strtoupper($name), $startx+90, 391);
pdf_set_font($pdf, "Times-Roman", 20, "host");
pdf_show_xy($pdf, "has demonstrated that they are certifiable ".
"by passing a rigorous exam", $startx, 340);
pdf_show_xy($pdf, "consisting of three multiple choice questions.",
$startx, 310);
pdf_show_xy($pdf, "$name obtained a score of $score"."%.", $startx, 260);
pdf_show_xy($pdf, "The test was set and overseen by the ", $startx, 210);
pdf_show_xy($pdf, "Fictional Institute of PHP Certification",
$startx, 180);
pdf_show_xy($pdf, "on $date.", $startx, 150);
pdf_show_xy($pdf, "Authorised by:", $startx, 100);
// add bitmap signature image
$signature = pdf_open_image_file($pdf, "tiff",
"/htdocs/book/chapter30/signature.tif");
pdf_place_image($pdf, $signature, 200, 75, 1);
pdf_close_image($pdf, $signature);
pdf_setrgbcolor_fill($pdf, 0, 0, .4); //dark blue
pdf_setrgbcolor_stroke($pdf, 0, 0, 0); // black
// draw ribbon 1
pdf_moveto($pdf, 630, 150);
pdf_lineto($pdf, 610, 55);
pdf_lineto($pdf, 632, 69);
pdf_lineto($pdf, 646, 49);
pdf_lineto($pdf, 666, 150);
pdf_closepath($pdf);
pdf_fill($pdf);
// outline ribbon 1
pdf_moveto($pdf, 630, 150);
pdf_lineto($pdf, 610, 55);
pdf_lineto($pdf, 632, 69);
pdf_lineto($pdf, 646, 49);
pdf_lineto($pdf, 666, 150);
pdf_closepath($pdf);
pdf_stroke($pdf);
// draw ribbon 2
pdf_moveto($pdf, 660, 150);
pdf_lineto($pdf, 680, 49);
pdf_lineto($pdf, 695, 69);
pdf_lineto($pdf, 716, 55);
pdf_lineto($pdf, 696, 150);
pdf_closepath($pdf);
pdf_fill($pdf);
// outline ribbon 2
pdf_moveto($pdf, 660, 150);
pdf_lineto($pdf, 680, 49);
pdf_lineto($pdf, 695, 69);
pdf_lineto($pdf, 716, 55);
pdf_lineto($pdf, 696, 150);
pdf_closepath($pdf);
pdf_stroke($pdf);
pdf_setrgbcolor_fill($pdf, .8, 0, 0); //red
//draw rosette
draw_star(665, 175, 32, 57, 10, $pdf, true);
//outline rosette
draw_star(665, 175, 32, 57, 10, $pdf, false);
pdf_end_page($pdf);
pdf_close($pdf);
}
function draw_star($centerx, $centery, $points, $radius, $point_size, $pdf, $filled)
{
$inner_radius = $radius-$point_size;
for ($i = 0; $i<=$points*2; $i++ )
{
$angle= ($i*2*pi())/($points*2);
if($i%2)
{
$x = $radius*cos($angle) + $centerx;
$y = $radius*sin($angle) + $centery;
}
else
{
$x = $inner_radius*cos($angle) + $centerx;
$y = $inner_radius*sin($angle) + $centery;
}
if($i==0)
pdf_moveto($pdf, $x, $y);
else if($i==$points*2)
pdf_closepath($pdf);
else
pdf_lineto($pdf, $x, $y);
}
if($filled)
pdf_fill($pdf);
pdf_stroke($pdf);
}
?>