Jump to content

FPDF Problem


thara
Go to solution Solved by Barand,

Recommended Posts

Using FPDF, I try to create barcode labels as the image attached below.

Untitled-1.png.1c340b64ccc46238cbd62d1671ec41ea.png

For barcode printing in the label, here I am using Code 128 barcodes script at FPDF.

This is how I tried it:

//require('./fpdf/barcode-codabar.php');
$data = ['item_name'     => 'Fuel Vapour Hose'
        ,'code_purchase' => 'ABC-2342'
        ,'code_sale'     => 'DFS-4312'
        ,'item_code'     => '47900001' 
        ];

require('./pdf_code128.php');
$pdf=new PDF_Code128();
$barcode = $pdf->Code128( 4,3,$data['item_code'],30,10);

class Barcode_Label extends FPDF { 
    protected $data;
    protected $barcode;
    
    //constructor
    public function __construct(\PDF_Code128 $barcode)
    {
        parent::__construct('P','mm',[31,110]);
        $this->setFillColor(0xDD);
        $this->barcode = $barcode;
    }

    public function printLabel($labelData)
    {
        $this->data = $labelData;
        $this->AddPage();
        $this->addLabel();

    }

    private function addLabel()
    {
        $k = count($catdata['data']);
        $this->setFont('Times', 'B', 10);

        $this->Cell(30, 6, 'Label 1', 'B', 0, 'C');
        $this->Cell(30, 6, 'Label 2', 'B', 0, 'C');
        $this->Cell(30, 6, 'Label 3', 'B', 0, 'C');
        $this->ln(2);

    }
}

$label= new Barcode_Label();
$label->printLabel($data);
$label->Output();

 

But this code is not working for me and it gives me

Quote

Fatal error: Uncaught Exception: FPDF error: No page has been added yet in E:\test\barcode-label\www\fpdf\fpdf.php:271 Stack trace: #0 E:\test\barcode-label\www\fpdf\fpdf.php(1451): FPDF->Error('No page has bee...') #1 E:\test\barcode-label\www\fpdf\fpdf.php(444): FPDF->_out('11.34 586.78 2....') #2 E:\test\barcode-label\www\pdf_code128.php(279): FPDF->Rect(4, 3, 0.75, 10, 'F') #3 E:\test\barcode-label\www\pdf.php(11): PDF_Code128->Code128(4, 3, '', 30, 10) #4 {main} thrown in E:\test\barcode-label\www\fpdf\fpdf.php on line 271

 

 

Edited by thara
Link to comment
Share on other sites

Yes that code is working. But just I want to create custom label as attached image with dynamic data. Thats why I try to to create custom code as above.

Sir, my I know, what would be the issue on my code?

Link to comment
Share on other sites

What you are doing is very confusing. 

Why are you not extending from the \PDF_Code128 class?  

Do you need the features of that class or not?

Having code that both defines a class and also creates objects of that class is terrible.  Have your class definition in a script with the name of the class as the name of the script.  Then require that class in your script.

As for the problem, it's shown to you clearly in the stack trace.  You need to actually learn to look at it and follow it. 

 FPDF is throwing the "No page has been added yet".  

You need to determine where in your code, that exception is occurring.  The stack trace shows you that:

Quote

E:\test\barcode-label\www\pdf.php(11): PDF_Code128->Code128(4, 3, '', 30, 10)

$barcode = $pdf->Code128( 4,3,$data['item_code'],30,10);

So this is the code that triggered your error -- and it's an object you try and create, purely so you can pass it into your class, yet don't even use in your code.

  • Like 1
Link to comment
Share on other sites

try

<?php
require 'code128.php';

$data = ['item_name'     => 'Fuel Vapour Hose'
        ,'code_purchase' => 'ABC-2342'
        ,'code_sale'     => 'DFS-4312'
        ,'item_code'     => '47900001' 
        ];


class Barcode_Label extends PDF_Code128 { 
    protected $data;
    
    //constructor
    public function __construct()
    {
        parent::__construct('L','mm',[60, 35]);
    }

    public function printLabel($data)
    {
        $this->setMargins(5,5,5);
        $this->SetAutoPageBreak(0);
        $this->AddPage();
        $this->setFont('Times', 'B', 10);
        $this->Cell(50, 5, $data['item_name'], 0, 1, 'C');
        $this->Cell(25, 5, $data['code_purchase'], 0, 0, 'C');
        $this->Cell(25, 5, $data['code_sale'], 0, 1, 'C');
        $barcode = $this->Code128( 5,15,$data['item_code'],50,10);
        $this->setY(25);
        $this->Cell(50, 5, $data['item_code'], 0, 1, 'C');
        $this->ln(2);

    }

} #Barcode_Label

$label= new Barcode_Label();
for ($i=0; $i<3; $i++) {
    $label->printLabel($data);
}
$label->Output();
?>

image.png.778e1702494991b1c3ceaeca5ee408bb.png

  • Great Answer 1
Link to comment
Share on other sites

21 hours ago, Barand said:

try

<?php
require 'code128.php';

$data = ['item_name'     => 'Fuel Vapour Hose'
        ,'code_purchase' => 'ABC-2342'
        ,'code_sale'     => 'DFS-4312'
        ,'item_code'     => '47900001' 
        ];


class Barcode_Label extends PDF_Code128 { 
    protected $data;
    
    //constructor
    public function __construct()
    {
        parent::__construct('L','mm',[60, 35]);
    }

    public function printLabel($data)
    {
        $this->setMargins(5,5,5);
        $this->SetAutoPageBreak(0);
        $this->AddPage();
        $this->setFont('Times', 'B', 10);
        $this->Cell(50, 5, $data['item_name'], 0, 1, 'C');
        $this->Cell(25, 5, $data['code_purchase'], 0, 0, 'C');
        $this->Cell(25, 5, $data['code_sale'], 0, 1, 'C');
        $barcode = $this->Code128( 5,15,$data['item_code'],50,10);
        $this->setY(25);
        $this->Cell(50, 5, $data['item_code'], 0, 1, 'C');
        $this->ln(2);

    }

} #Barcode_Label

$label= new Barcode_Label();
for ($i=0; $i<3; $i++) {
    $label->printLabel($data);
}
$label->Output();
?>

First, Thank you very much sir, for the big help. I tried with your code and its working fine for me.

Sir, one thing, here on my printer i am using a sticker paper roll as shown in the image below:

2-5000-34mm-x-22mm-3a-avery-original-imafzu7hgw2q26fp.png.d22be14ad186501d727b6d5972f1bf24.png

Using this code, labels can only be printed on the right side of the paper. As shown in the image below,

IMG_20220406_231029.png.b1de37da2efcac7a011fd65b9b5603d8.png

If I print labels for other two columns, I can do it by adjusting printer setting on my printer. But its not easy for all who use the printer.

Sir, can we modify above script to match this paper roll?

Link to comment
Share on other sites

  • Solution

Simple. Triple the page width and offset each label.

require 'code128.php';

$data = ['item_name'     => 'Fuel Vapour Hose'
        ,'code_purchase' => 'ABC-2342'
        ,'code_sale'     => 'DFS-4312'
        ,'item_code'     => '47900001' 
        ];


class Barcode_Label extends PDF_Code128 { 
    protected $data;
    
    //constructor
    public function __construct()
    {
        parent::__construct('L','mm',[190, 35]);
    }

    public function printLabel($data)
    {
        $this->setMargins(5,5,5);
        $this->SetAutoPageBreak(0);
        $this->AddPage();
        $this->setFont('Times', 'B', 10);
        
        for ($lab=0; $lab<3; $lab++) {
            $offset = $lab * 65;
            $this->setXY($offset, 5);
            $this->Cell(50, 5, $data['item_name'], 0, 2, 'C');
            $this->Cell(25, 5, $data['code_purchase'], 0, 0, 'C');
            $this->Cell(25, 5, $data['code_sale'], 0, 2, 'C');
            $barcode = $this->Code128($offset + 5,15,$data['item_code'],50,10);
            $this->setXY($offset, 25);
            $this->Cell(50, 5, $data['item_code'], 0, 1, 'C');
        }

    }

} #Barcode_Label

$label= new Barcode_Label();
for ($i=0; $i<3; $i++) {
    $label->printLabel($data);
}
$label->Output();

image.png.245915ba5ecbb4658d6d72f0ea119c14.png

[edit] PS I don't know your label dimensions so you may have to adjust offset, page size and margins

Edited by Barand
  • Like 1
  • Great Answer 1
Link to comment
Share on other sites

On 4/7/2022 at 12:25 AM, Barand said:

[edit] PS I don't know your label dimensions so you may have to adjust offset, page size and margins

Yes sir, I have customized your code to suit my needs.

IMG_20220406_231029.png.61d480c57d93a9f6204c7f9dfaca6ecc.png

Sir one thing, I have tested these codes with few actual barcode scanners. Sometimes, barcode scanners can't recognize a barcode or sometimes its take few seconds to recognize. And aslo, in some cases, I have to manage some distance between label and scaner to read the code.

I have tested this for code 128 barcode and code 39 barcode scripts at FPDF. Both scripts work the same way.

 

 

Link to comment
Share on other sites

PS I did some experimenting with the QR reader app  on my smartphone (10 years old with a camera resolution you wouldn't write home about).

It had no problem scanning those barcodes from the screen examples in my above post (with the code) or from a print of the PDF file on my 300dpi inkjet printer.

Although it couldn't scan the (smaller) example output you posted???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.