Jump to content

[SOLVED] Class error


forumnz

Recommended Posts

I'm following a tutorial on the internet for creating PDF files using PHP.

It says I need a class file.

Here is my file contents:

 

<?php
var $_buffer = '';          // Buffer holding in-memory PDF.
var $_state = 0;            // Current document state.
var $_page = 0;             // Current page number.
var $_n = 2;                // Current object number.
var $_offsets = array();    // Array of object offsets.
var $_pages = array();      // Array containing the pages.
var $_w;                    // Page width in points.
var $_h;                    // Page height in points
var $_fonts = array();      // An array of used fonts.
var $_font_family = '';     // Current font family.
var $_font_style = '';      // Current font style.
var $_current_font;         // Array with current font info.
var $_font_size = 12;       // Current font size in points.
var $_compress;             // Flag to compress or not.
var $_core_fonts = array('courier'      => 'Courier',
                         'courierB'     => 'Courier-Bold',
                         'courierI'     => 'Courier-Oblique',
                         'courierBI'    => 'Courier-BoldOblique',
                         'helvetica'    => 'Helvetica',
                         'helveticaB'   => 'Helvetica-Bold',
                         'helveticaI'   => 'Helvetica-Oblique',
                         'helveticaBI'  => 'Helvetica-BoldOblique',
                         'times'        => 'Times-Roman',
                         'timesB'       => 'Times-Bold',
                         'timesI'       => 'Times-Italic',
                         'timesBI'      => 'Times-BoldItalic',
                         'symbol'       => 'Symbol',
                         'zapfdingbats' => 'ZapfDingbats'); 
var $_fill_color = '0 g';   // Color used on text and fills.
var $_draw_color = '0 G';   // Line draw color.
var $_line_width = 1;       // Drawing line width.
var $_images = array();     // An array of used images. 
?>

 

Error:

Parse error: syntax error, unexpected T_VAR in /domains/html/pdfclass.php on line 3

 

What could the problem be?

 

Thanks

Sam

 

Link to comment
https://forums.phpfreaks.com/topic/156475-solved-class-error/
Share on other sites

If it's saying you need a class then use one. The code above is the contents of a PHP4 class, just without the class declaration. Just add it:

 

PHP4

class CreatePDF
{

var $_buffer = '';          // Buffer holding in-memory PDF.
var $_state = 0;            // Current document state.
var $_page = 0;             // Current page number.
var $_n = 2;                // Current object number.
var $_offsets = array();    // Array of object offsets.
var $_pages = array();      // Array containing the pages.
var $_w;                    // Page width in points.
var $_h;                    // Page height in points
var $_fonts = array();      // An array of used fonts.
var $_font_family = '';     // Current font family.
var $_font_style = '';      // Current font style.
var $_current_font;         // Array with current font info.
var $_font_size = 12;       // Current font size in points.
var $_compress;             // Flag to compress or not.
var $_core_fonts = array('courier'      => 'Courier',
                         'courierB'     => 'Courier-Bold',
                         'courierI'     => 'Courier-Oblique',
                         'courierBI'    => 'Courier-BoldOblique',
                         'helvetica'    => 'Helvetica',
                         'helveticaB'   => 'Helvetica-Bold',
                         'helveticaI'   => 'Helvetica-Oblique',
                         'helveticaBI'  => 'Helvetica-BoldOblique',
                         'times'        => 'Times-Roman',
                         'timesB'       => 'Times-Bold',
                         'timesI'       => 'Times-Italic',
                         'timesBI'      => 'Times-BoldItalic',
                         'symbol'       => 'Symbol',
                         'zapfdingbats' => 'ZapfDingbats'); 
var $_fill_color = '0 g';   // Color used on text and fills.
var $_draw_color = '0 G';   // Line draw color.
var $_line_width = 1;       // Drawing line width.
var $_images = array();     // An array of used images. 


function CreatePDF()
{
   //constructor
}

}

PHP5

class CreatePDF
{

public $_buffer = '';          // Buffer holding in-memory PDF.
public $_state = 0;            // Current document state.
public $_page = 0;             // Current page number.
public $_n = 2;                // Current object number.
public $_offsets = array();    // Array of object offsets.
public $_pages = array();      // Array containing the pages.
public $_w;                    // Page width in points.
public $_h;                    // Page height in points
public $_fonts = array();      // An array of used fonts.
public $_font_family = '';     // Current font family.
public $_font_style = '';      // Current font style.
public $_current_font;         // Array with current font info.
public $_font_size = 12;       // Current font size in points.
public $_compress;             // Flag to compress or not.
public $_core_fonts = array('courier'      => 'Courier',
                         'courierB'     => 'Courier-Bold',
                         'courierI'     => 'Courier-Oblique',
                         'courierBI'    => 'Courier-BoldOblique',
                         'helvetica'    => 'Helvetica',
                         'helveticaB'   => 'Helvetica-Bold',
                         'helveticaI'   => 'Helvetica-Oblique',
                         'helveticaBI'  => 'Helvetica-BoldOblique',
                         'times'        => 'Times-Roman',
                         'timesB'       => 'Times-Bold',
                         'timesI'       => 'Times-Italic',
                         'timesBI'      => 'Times-BoldItalic',
                         'symbol'       => 'Symbol',
                         'zapfdingbats' => 'ZapfDingbats'); 
public $_fill_color = '0 g';   // Color used on text and fills.
public $_draw_color = '0 G';   // Line draw color.
public $_line_width = 1;       // Drawing line width.
public $_images = array();     // An array of used images. 

public function __construct()
{
  //constructor
}

}

Link to comment
https://forums.phpfreaks.com/topic/156475-solved-class-error/#findComment-823957
Share on other sites

Thanks again the182guy. I'm so confused though.

 

I used the PHP5 example you wrote.

I get this error:

Fatal error: Call to undefined method PDF::factory() in /html/pdf.php on line 3

 

Here is the pdf.php code:

<?php
require 'pdfclass.php';               // Require the class.
$pdf = &PDF::factory('p', 'a4');      // Set up the pdf object.
$pdf->open();                         // Start the document.
$pdf->setCompression(true);           // Activate compression.
$pdf->addPage();                      // Start a page.
$pdf->setFont('Courier', '', ;      // Set font to courier 8 pt.
$pdf->text(100, 100, 'First page');   // Text at x=100 and y=100.
$pdf->setFontSize(20);                // Set font size to 20 pt.
$pdf->setFillColor('rgb', 1, 0, 0);   // Set text color to red.
$pdf->text(100, 200, 'HELLO WORLD!'); // Text at x=100 and y=200.

$pdf->setDrawColor('rgb', 0, 0, 1);   // Set draw color to blue.
$pdf->line(100, 202, 240, 202);       // Draw a line.
$pdf->setFillColor('rgb', 1, 1, 0);   // Set fill/text to yellow.
$pdf->rect(200, 300, 100, 100, 'fd'); // Draw a filled rectangle.
$pdf->addPage();                      // Add a new page.

$pdf->setFont('Arial', 'BI', 12);     // Set font to arial bold
                                      // italic 12 pt.
$pdf->text(100, 100, 'Second page');  // Text at x=100 and y=100.
$pdf->image('sample.jpg', 50, 200);   // Image at x=50 and y=200.
$pdf->setLineWidth(4);                // Set line width to 4 pt.
$pdf->circle(200, 300, 150, 'd');     // Draw a non-filled
                                      // circle.
$pdf->output('foo.pdf');              // Output the file named foo.pdf
?> 

 

What am I doing wrong? Sorry, this is just new to me :)

Thanks

Sam

Link to comment
https://forums.phpfreaks.com/topic/156475-solved-class-error/#findComment-823958
Share on other sites

Sam, the code you provided in post one is only part of the class. It only contains the class properties (variables). It doesn't contain any of the class methods (functions) such as the method you're trying to use called 'factory'.

 

Also change the class name to PDF instead of CreatePDF.

 

You need to copy the rest of the code from the tutorial, the part which contains all the methods.

Link to comment
https://forums.phpfreaks.com/topic/156475-solved-class-error/#findComment-823960
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.