thara Posted April 5, 2022 Share Posted April 5, 2022 (edited) Using FPDF, I try to create barcode labels as the image attached below. 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 April 5, 2022 by thara Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/ Share on other sites More sharing options...
Barand Posted April 5, 2022 Share Posted April 5, 2022 Try this code Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595014 Share on other sites More sharing options...
thara Posted April 5, 2022 Author Share Posted April 5, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595021 Share on other sites More sharing options...
gizmola Posted April 5, 2022 Share Posted April 5, 2022 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595035 Share on other sites More sharing options...
Barand Posted April 5, 2022 Share Posted April 5, 2022 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(); ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595039 Share on other sites More sharing options...
gizmola Posted April 5, 2022 Share Posted April 5, 2022 Seems like you ought to buy Barand a cup of coffee at least 1 Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595040 Share on other sites More sharing options...
thara Posted April 6, 2022 Author Share Posted April 6, 2022 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: Using this code, labels can only be printed on the right side of the paper. As shown in the image below, 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? Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595073 Share on other sites More sharing options...
Solution Barand Posted April 6, 2022 Solution Share Posted April 6, 2022 (edited) 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(); [edit] PS I don't know your label dimensions so you may have to adjust offset, page size and margins Edited April 6, 2022 by Barand 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595076 Share on other sites More sharing options...
thara Posted April 10, 2022 Author Share Posted April 10, 2022 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595208 Share on other sites More sharing options...
Barand Posted April 10, 2022 Share Posted April 10, 2022 Unless you rewrite the classes, the only thing you can change is the output. Does adjusting the width of the output barcode affect readability? Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595209 Share on other sites More sharing options...
thara Posted April 10, 2022 Author Share Posted April 10, 2022 The maximum width of the sticker is used for the barcode. Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595211 Share on other sites More sharing options...
Barand Posted April 10, 2022 Share Posted April 10, 2022 (edited) And why does that stop you experimenting? Perhaps you may need different sized stickers. Or maybe a higher resolution printer? Edited April 10, 2022 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595213 Share on other sites More sharing options...
Barand Posted April 10, 2022 Share Posted April 10, 2022 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??? Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595217 Share on other sites More sharing options...
thara Posted April 10, 2022 Author Share Posted April 10, 2022 Sir I have been doing some experiments for a considerable time. Now it is at a satisfactory level. The printer settings also have the ability to adjust to a certain level. Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595218 Share on other sites More sharing options...
thara Posted April 10, 2022 Author Share Posted April 10, 2022 Sir, My label size is 34x25mm Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595219 Share on other sites More sharing options...
Barand Posted April 10, 2022 Share Posted April 10, 2022 How wide are the spacings beween the labels? Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595220 Share on other sites More sharing options...
thara Posted April 10, 2022 Author Share Posted April 10, 2022 (edited) Sir, this is my actual size of the paper Total width: 110mm Total Height: 31mm Edited April 10, 2022 by thara Quote Link to comment https://forums.phpfreaks.com/topic/314668-fpdf-problem/#findComment-1595221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.