Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create pdf barcode using writeHTML() method - TCPDF

Tags:

php

tcpdf

I use TCPDF to create PDF documents in PHP. Latest TCPDF supports 1D and 2D barcodes. I successfully displayed barcode as explained in the documentation like;

require_once('tcpdf_barcodes_1d.php');
$barcodeobj = new TCPDFBarcode('some_text', 'C128');
$barcode = $barcodeobj->getBarcodeHTML(1, 20, 'black');
echo $barcode;

This works fine. The barcode generated is not image, and is a combination of absolutely positioned divs.

It also support PNG barcode generation like;

$barcodeobj->getBarcodePNG(2, 30, array(0,0,0));//displays barcode image

and SVG like;

$barcodeobj->getBarcodeSVG(2, 30, 'black');//svg file download prompt

In the case of PNG, it displays image automatically in browser. It don't need echo, or print or something else.

What I need is to print barcode into a PDF file. I use TCPDF's writeHTML() method to create PDFs, and I want to use that method here since I have some other things to print along with barcode. How can I create PDF with barcodes in it, using writeHTML() method (like inside an <img> tag)?

like image 610
Alfred Avatar asked Oct 22 '22 04:10

Alfred


1 Answers

Just use this method:

$pdf->setXY(93,272);
$pdf->write1DBarcode("074001726000003006652985", 'C39', '', '', 90, 10, 0.4, '', 'N');

Very simple, just check the documentation here: http://www.tcpdf.org/examples/example_027.phps or here http://www.tcpdf.org/doc/code/classTCPDF.html#a4816d61822a4bad6e35bb441c1699aab

like image 174
Pam Avatar answered Oct 24 '22 12:10

Pam