0

I am using a barcode generator php code to generate barcodes. But when I want to write barcode to an existing pdf file using fpdi+fpdf the written string is not the generated barcode! It is written obscure words. The code that I used is below:

<?php
session_start();
#----- gets serialnumber from page form ------
$serialnumber = $_SESSION['serialnumber'];
#---------------------------------------------

    include 'lib/barcode/barcode128.php';
    $bar_code= bar128($serialnumber);

#echo $serialnumber;
include('lib/FPDI-2.3.6/src/autoload.php');
require('lib/fpdf184/fpdf.php');
require('lib/FPDI-2.3.6/src/fpdi.php');
$pdf = new Fpdi();
#$pdf->AddPage();


$pagecount= $pdf->setSourceFile("lib/test3.pdf");


$tpl = $pdf->importPage(1);
$pdf->AddPage();


$pdf->useTemplate($tpl);

$pdf->SetFont('arial');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(0, 5);

$pdf->Write(0, $bar_code);
$pdf->Cell(20,10,$bar_code,1,1,'C');

$pdf->Output();
?>

also barcode128.php works fine no problem with that. I downloaded it from https://jinujawad.com/create-simple-barcode-using-php/

I used both write and cell but the result is the same.up=write & down=cell

  • Thank you @KJ . I want to solve this by font approach. I don't want to export an image or any other file and embed it in my PDF. Prefer to insert barcode like a text. What font family will probably help me? – Saeed Eisakhani Oct 25 '21 at 19:53
  • You can use [this script](http://www.fpdf.org/?go=script&id=88) to add the barcode. – Olivier Oct 26 '21 at 07:17
  • Thanks @Olivier but code128.php can not pair with fpdi !!! fpdi explicitly wants fpdf – Saeed Eisakhani Oct 26 '21 at 11:23
  • I think I have problem with my barcode variable as an string!!! I tried to convert it to image by `imagestring($im, $font, 0, 0, $text, $textcolor);` but the image does not contain barcode! it contains obscure words againe !! can any body help me please ! – Saeed Eisakhani Oct 26 '21 at 18:49
  • Just make the `PDF_Code128` class extend `Fpdi` instead of `FPDF`. – Olivier Oct 27 '21 at 07:06
  • @Olivier I did that and changed the line `class PDF_Code128 extends FPDF {` to `class PDF_Code128 extends Fpdi {` in code128.php file. But I got the eror `Fatal error: Uncaught Error: Class "Fpdi" not found in C:\xampp\htdocs\code128.php:44 Stack trace: #0 C:\xampp\htdocs\pdfgenerate.php(11): require() #1 {main} thrown in C:\xampp\htdocs\code128.php on line 44` . After this I required Fpdi in line 42 instead of FPDI but I got error `Fatal error: Uncaught Error: Class "FPDF" not found in C:\xampp\htdocs\FPDI-2.3.6\src\FpdfTpl.php:18 ` . Did I do wrog? – Saeed Eisakhani Oct 27 '21 at 12:09

0 Answers0