TCPDF With An Image and HTML in PHP

Introduction

This article explains TCPDF with an image and HTML in PHP. Using this article you can learn how to use TCPDF in PHP. TCPDF is very helpful for creating Graph, Chart, and Image. TCPDF is a library for generating PDF documents with PHP and TCPDF is most useful and reliable for PHP projects. Before using TCPDF you need to verify that the GD library is enabled. If the GD library is enabled then you can start using TCPDF. If the GD library is not enabled then you should enable the GD library from the "php.ini" file. Let's start using TCPDF but here I will use TCPDF to create a simple image and HTML.

Example

Simple example for PDF with TCPDF library.

<?php

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

require_once('tcpdf/tcpdf.php');

$exa = new TCPDF();

$exa->AddPage();

//Assign text in pdf file

$text = "Simple Example of TCPDF";

$exa->Write( 20, $text );

//save the pdf file

$exa->Output( 'example.pdf', 'I' );

?>

Output

cal2.jpg

Example

This example shows how to overlap an image with some text. Such as:

<?php

error_reporting ( E_ALL );

//requires file from tcpdf

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

require_once ('tcpdf/tcpdf.php');

$exa = new TCPDF ();

$exa->SetCreator ( PDF_CREATOR );

$exa->SetAuthor ( 'Vinod kumar' );

$exa->SetTitle ( 'Image with HTML' );

$exa->SetSubject ( 'Example of TCPDF' );

$exa->SetKeywords ( 'TCPDF, PDF, PHP' );

$exa->SetFont ( 'times', '', 18 );

$exa->AddPage ();

$txt = <<<HDOC

 Example of TCPDF

HDOC;

$exa->Write ( 0, $txt );

$exa->setImageScale ( PDF_IMAGE_SCALE_RATIO );

$exa->setJPEGQuality ( 90 );

$exa->Image ( "fs.jpg" );

$txt = "<h2>Embedded HTML</h2>

This text should have some <em>italic</em> and some <strong>bold</strong>

and the caption should be an &lt;h2&gt;.";

$exa->WriteHTML ( $txt );

$exa->Output ( 'image_and_html.pdf', 'I' );

?> 

Output

cal.jpg

Example

This example shows use of an Overlap Issue by inserting line breaks.

<?php

error_reporting ( E_ALL );

//requires file from tcdpf

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

require_once ('tcpdf/tcpdf.php');

$exa = new TCPDF();

$exa->setCreator ( PDF_CREATOR );

$exa->setAuthor ( 'Vinod kumar' );

$exa->setTitle ( 'Image with HTML' );

$exa->setSubject ( 'Example of TCPDF' );

$exa->setKeywords ( 'TCPDF, pdf, PHP' );

//setting font

$exa->setFont ( 'times', '', 18);

$exa->addPage ();

$txt = <<<HDOC

 Example of TCPDF

HDOC;

$exa->Write ( 0, $txt );

$exa->Ln ();

$exa->setImageScale ( PDF_IMAGE_SCALE_RATIO );

$exa->setJPEGQuality ( 90 );

$exa->Image ( "fs.jpg" );

$exa->Ln ( 30 );

$txt = "<h2>Embedded HTML</h2>

This text should have some <em>italic</em> and some <strong>bold</strong>

and the caption should be an &lt;h2&gt;.";

$exa->WriteHTML ( $txt );

$exa->Output ( 'image_and_html.pdf', 'I' );

?>

Output

cal1.jpg


Similar Articles