Bebo
Bebo

Reputation: 1

Barcodes using TCPDF

I am new to Laravel and generating pdf with barcodes, i was able to generate the barcodes for the id of each item and store in the db as html, not i am trying to save the barcode to pdf file using tcpdf but the output isn't expected.

that's to save the barcode to the db which works fine:

 $products = Product::get();
    foreach ($products as $p) {
        if ($p->barcode == NULL) {
        $generator = new Picqer\Barcode\BarcodeGeneratorHTML();
        $barcode = $generator->getBarcode($p->id, $generator::TYPE_CODE_128);
        $pro = Product::find($p->id);
        $pro->barcode = $barcode;
        $pro->save();
        }
    }

and that's how i am viewing the barcode in the browser

@foreach ($products as $p)
                            <div class="barcode">
                             {!! html_entity_decode( $p->barcode ) !!} 
                             
                               <p>Id: {{ $p->id }} </p>   
                               <p> {{ $p->name}} </p>
                               </div>
                           
                               
                            @endforeach

that's how the barcode stored in the DB

    <div style="font-size:0;position:relative;width:92px;height:30px;">
 <div style="background-color:black;width:4px;height:30px;position:absolute;left:0px;top:0">&nbsp;</div>
 <div style="background-color:black;width:2px;height:30px;position:absolute;left:6px;top:0">&nbsp;</div>
 <div style="background-color:black;width:2px;height:30px;position:absolute;left:12px;top:0">&nbsp;</div>
 <div style="background-color:black;width:2px;height:30px;position:absolute;left:22px;top:0">&nbsp;</div>
 <div style="background-color:black;width:6px;height:30px;position:absolute;left:28px;top:0">&nbsp;</div>
 <div style="background-color:black;width:4px;height:30px;position:absolute;left:38px;top:0">&nbsp;</div>
 <div style="background-color:black;width:4px;height:30px;position:absolute;left:44px;top:0">&nbsp;</div>
 <div style="background-color:black;width:6px;height:30px;position:absolute;left:52px;top:0">&nbsp;</div>
 <div style="background-color:black;width:2px;height:30px;position:absolute;left:62px;top:0">&nbsp;</div>
 <div style="background-color:black;width:4px;height:30px;position:absolute;left:66px;top:0">&nbsp;</div>
 <div style="background-color:black;width:6px;height:30px;position:absolute;left:76px;top:0">&nbsp;</div>
 <div style="background-color:black;width:2px;height:30px;position:absolute;left:84px;top:0">&nbsp;</div>
 <div style="background-color:black;width:4px;height:30px;position:absolute;left:88px;top:0">&nbsp;</div>
 </div>
 

and that's how I am trying to save it as pdf

// set style for barcode
    PDF::SetMargins(3, 3, 3);

    // remove default header/footer
    PDF::setPrintHeader(false);
    PDF::setPrintFooter(false);

    PDF::setFontSubsetting(true);
    PDF::SetFont('freeserif', '', 12);
    // define barcode style
    $style = array(
        'border' => true,
        'hpadding' => 'auto',
        'vpadding' => 'auto',
        'fgcolor' => array(0,0,0),
        'bgcolor' => false, //array(255,255,255),
        'text' => true,
        'font' => 'helvetica',
        'fontsize' => 8,
        'stretchtext' => 4
    );

    PDF::SetTitle('Hello World');
    PDF::AddPage();
    
    PDF::Cell(0, 0, $products[0]->name, 0, 1);
    $code = PDF::serializeTCPDFtagParameters($products[0]->barcode);
    PDF::write1DBarcode($code, 'C39', '5', '5', '60', 14, 0.4, $style, 'N');
    PDF::Ln(5);
    PDF::Output('example_049.pdf', 'I');

and that's the output I get:

output

Thanks in advance

Upvotes: 0

Views: 759

Answers (2)

Bebo
Bebo

Reputation: 1

Solved, by passing the actual row id to write1DBarcode instead of the html/css code

Upvotes: 0

SoftwarePanel
SoftwarePanel

Reputation: 11

I think you should save the id in the db just as ID, not as html. Then you get the ID from the db and generate the barcode in HTML for a page view and or in a PDF.

Upvotes: 1

Related Questions