Barcode and Gradient Generating With TCPDF in PHP

Introduction

In this article I will explain Barcode and Gradient generation with TCPDF in PHP. The TCPDF library is used for generating PDF documents with PHP. It needs no external libraries, is extremely common, and is actively developed. TCPDF may be found at "http://www.tcpdf.org/".  TCPDF is totally featured and supports graphics through PHP GD and image magic, barcodes, gradients, HTML, CSS, fonts, layout management, headers, and footers. Default definitions and settings are within the configuration file, found at "htdocs/tcpdf/config/tcpdf_config.php".

This example describes simple creation of a PDF file using TCPDF. 

Example

<?php

error_reporting(E_ALL);

require_once('tcpdf/config/lang/eng.php');

require_once('tcpdf/tcpdf.php');

$file = new TCPDF();

$file->AddPage();

$txt = "TCPDF Minimal Example for  Creating PDF";

$file->Write( 20, $txt );

$file->Output( 'minimal.pdf', 'I' );

?>

Output

a1.jpg

The following is an example of a barcode and gradient cell used by TCPDF.

Example

<?php

error_reporting(E_ALL);

require_once('tcpdf/config/lang/eng.php');

require_once('tcpdf/tcpdf.php');

$file = new TCPDF();

$file->SetCreator ( PDF_CREATOR );

$file->SetAuthor ( 'Vinod Kumar' );

$file->SetTitle ( 'Create Barcod Reader' );

$file->SetSubject ( 'Barcode with TCPDF' );

$file->SetKeywords ( 'Barcode with TCPDF, TCPDF, PDF, PHP' );

$file->SetFont ( 'times', '', 20 );

$file->SetMargins( PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT );

$file->AddPage();

$txt = <<<HDOC

Barcode with TCPDF Example- Barcode and Gradients

HDOC;

$file->Write( 20, $txt );

$file->Ln();

$file->write1DBarcode('101101101', 'C39+');

$file->Ln();

$txt = "Above: a generated barcode and Below a generated gradient image";

$file->WriteHTML($txt);

$file->Ln();

$blue = array( 0, 0, 200 );

$yellow = array( 255, 255, 0 );

$coords = array( 0, 0, 1, 1 );

$file->LinearGradient( PDF_MARGIN_LEFT, 90, 20, 20, $blue, $yellow, $coords );

$file->Text( PDF_MARGIN_LEFT, 111, 'Gradient cell' );

$file->Output( 'barcode_and_gradient.pdf', 'I' );

?>

Output 

a2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all