Axial and radial shadings with multiple stops and per-stop opacity, plus Coons patch meshes.
- Rendered PDF: E034_color_gradient_mesh.pdf
- PHP source: E034_color_gradient_mesh.phps
- All examples: /examples/
Source Code
<?php
declare(strict_types=1);
/**
* E034_color_gradient_mesh.php
*
* @since 2017-05-08
* @category Library
* @package Pdf
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2002-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf
*
* This file is part of tc-lib-pdf software library.
*/
// NOTE: local file reads (images, fonts, attachments) are restricted to an allowlist of
// trusted paths that covers this package tree, so run the examples in place. To read assets
// from other locations, list them in the 'allowedPaths' entry of the fileOptions constructor
// parameter (see E047_remote_resources_security.php).
// NOTE: run make fonts in the project root to generate the dependencies and example fonts.
// autoloader when using Composer
require __DIR__ . '/../vendor/autoload.php';
\define('OUTPUT_FILE', \realpath(__DIR__ . '/../target') . '/example.pdf');
// define fonts directory
\define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts'));
// autoloader when using RPM or DEB package installation
//require ('/usr/share/php/Com/Tecnick/Pdf/autoload.php');
// main TCPDF object
$pdf = new \Com\Tecnick\Pdf\Tcpdf(
unit: \Com\Tecnick\Pdf\Page\Unit::Millimeter,
isunicode: true,
subsetfont: false,
compress: true,
mode: \Com\Tecnick\Pdf\PdfConformance::None,
objEncrypt: null,
);
// ----------
$pdf->setCreator('tc-lib-pdf');
$pdf->setAuthor('Nicola Asuni');
$pdf->setSubject('tc-lib-pdf example: 034');
$pdf->setTitle('Color Gradient Mesh');
$pdf->setKeywords('TCPDF tc-lib-pdf color gradient mesh shading patch');
$pdf->setPDFFilename('034_color_gradient_mesh.pdf');
$pdf->setViewerPreferences(['DisplayDocTitle' => true]);
$pdf->enableDefaultPageContent();
// ----------
// Insert fonts
$bfont1 = $pdf->font->insert($pdf->pon, 'helvetica', '', 12);
// ----------
$page01 = $pdf->addPage();
$pdf->setBookmark(name: 'Color Gradients', link: '', level: 1);
$html =
'<h1>Color Gradients and Meshes</h1>'
. '<p>This page demonstrates linear and radial gradients together with Coons patch meshes rendered by tc-lib-pdf.</p>';
$pdf->addHTMLCell(html: $html, posx: 15, posy: 20, width: 180);
// Linear gradient
$lingrad = $pdf->graph->getLinearGradient(20, 80, 80, 80, 'red', 'blue', [0, 0, 1, 0]);
$pdf->page->addContent($lingrad);
// Radial Gradient
$radgrad = $pdf->graph->getRadialGradient(110, 80, 80, 80, 'white', 'black', [0.5, 0.5, 1, 1, 1.2]);
$pdf->page->addContent($radgrad);
// CoonsPatchMesh
$coonspatchmesh1 = $pdf->graph->getCoonsPatchMeshWithCoords(20, 180, 80, 80, 'yellow', 'blue', 'green', 'red');
$pdf->page->addContent($coonspatchmesh1);
// set the coordinates for the cubic Bézier points x1,y1 ... x12, y12 of the patch
$coords = [
0.00,
0.00,
0.33,
0.20,
//lower left
0.67,
0.00,
1.00,
0.00,
0.80,
0.33,
//lower right
0.80,
0.67,
1.00,
1.00,
0.67,
0.80,
//upper right
0.33,
1.00,
0.00,
1.00,
0.20,
0.67,
//upper left
0.00,
0.33,
]; //lower left
// paint a coons patch gradient with the above coordinates
$coonspatchmesh2 = $pdf->graph->getCoonsPatchMeshWithCoords(
110,
180,
80,
80,
'yellow',
'blue',
'green',
'red',
$coords,
0,
1,
);
$pdf->page->addContent($coonspatchmesh2);
// ----------
$page02 = $pdf->addPage();
$pdf->setBookmark(name: 'Color gradient mesh', link: '', level: 1);
// first patch: f = 0
$patch_array[0]['f'] = 0;
$patch_array[0]['points'] = [
0.00,
0.00,
0.33,
0.00,
0.67,
0.00,
1.00,
0.00,
1.00,
0.33,
0.8,
0.67,
1.00,
1.00,
0.67,
0.8,
0.33,
1.80,
0.00,
1.00,
0.00,
0.67,
0.00,
0.33,
];
$patch_array[0]['colors'][0] = [
'red' => 1,
'green' => 1,
'blue' => 0,
'alpha' => 1,
];
$patch_array[0]['colors'][1] = [
'red' => 1,
'green' => 1,
'blue' => 1,
'alpha' => 1,
];
$patch_array[0]['colors'][2] = [
'red' => 0,
'green' => 1,
'blue' => 0,
'alpha' => 1,
];
$patch_array[0]['colors'][3] = [
'red' => 1,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
// second patch - above the other: f = 2
$patch_array[1]['f'] = 2;
$patch_array[1]['points'] = [
0.00,
1.33,
0.00,
1.67,
0.00,
2.00,
0.33,
2.00,
0.67,
2.00,
1.00,
2.00,
1.00,
1.67,
1.5,
1.33,
];
$patch_array[1]['colors'][0] = [
'red' => 0,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
$patch_array[1]['colors'][1] = [
'red' => 1,
'green' => 0,
'blue' => 1,
'alpha' => 1,
];
// third patch - right of the above: f = 3
$patch_array[2]['f'] = 3;
$patch_array[2]['points'] = [
1.33,
0.80,
1.67,
1.50,
2.00,
1.00,
2.00,
1.33,
2.00,
1.67,
2.00,
2.00,
1.67,
2.00,
1.33,
2.00,
];
$patch_array[2]['colors'][0] = [
'red' => 0,
'green' => 1,
'blue' => 1,
'alpha' => 1,
];
$patch_array[2]['colors'][1] = [
'red' => 0,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
// fourth patch - below the above, which means left(?) of the above: f = 1
$patch_array[3]['f'] = 1;
$patch_array[3]['points'] = [
2.00,
0.67,
2.00,
0.33,
2.00,
0.00,
1.67,
0.00,
1.33,
0.00,
1.00,
0.00,
1.00,
0.33,
0.8,
0.67,
];
$patch_array[3]['colors'][0] = [
'red' => 0,
'green' => 0,
'blue' => 0,
'alpha' => 1,
];
$patch_array[3]['colors'][1] = [
'red' => 0,
'green' => 0,
'blue' => 1,
'alpha' => 1,
];
$coonspatchmesh3 = $pdf->graph->getCoonsPatchMesh(0, 0, 210, 297, $patch_array, 0, 2);
$pdf->page->addContent($coonspatchmesh3);
// =============================================================
// ----------
// get PDF document as raw string
$rawpdf = $pdf->getOutPDFString();
// ----------
// Various output modes:
//$pdf->savePDF(\dirname(__DIR__).'/target', $rawpdf);
$pdf->renderPDF(rawpdf: $rawpdf);
//$pdf->downloadPDF($rawpdf);
//echo $pdf->getMIMEAttachmentPDF($rawpdf);
