pdf_setcolor

(PHP 4 >= 4.0.5, PECL)

pdf_setcolor -- Sets fill and stroke color

Description

bool pdf_setcolor ( resource pdfdoc, string type, string colorspace, float c1, float c2, float c3, float c4 )

Set the current color space and color. 如果成功则返回 TRUE,失败则返回 FALSE。 The parameter type can be fill, stroke or both to specify that the color is set for filling, stroking or both filling and stroking. The parameter colorspace can be gray, rgb, cmyk, spot or pattern. The parameters c1, c2, c3 and c4 represent the color components for the color space specified by colorspace. Except as otherwise noted, the color components are floating-point values that range from 0 to 1. Parameters c2, c3 and c4 are optional before PHP 4.3.5 or with PDFlib less than 5.

For gray only c1 is used.

For rgb parameters c1, c2, and c3 specify the red, green and blue values respectively.

<?php
// Set fill and stroke colors to white.
pdf_setcolor($pdf, "both", "rgb", 1, 1, 1);
?>

For cmyk, parameters c1, c2, c3, and c4 are the cyan, magenta, yellow and black values, respectively.

<?php
// Set fill and stroke colors to black.
pdf_setcolor($pdf, "both", "cmyk", 0, 0, 0, 1);
?>

For spot, c1 should be a spot color handles returned by pdf_makespotcolor() and c2 is a tint value between 0 and 1.

For pattern, c1 should be a pattern handle returned by pdf_begin_pattern().


add a note add a note User Contributed Notes
_meto ALT+q web.de
09-May-2004 12:03
Damn, this took me some real long time! Maybe it's helpfull for those who even have no idea of color-Schemes like me ;)

If u want to generate PDF's for Print Offices u need to set all the colors in CMYK.

Here is a Function that will convert RGB to CMYK

<?
function hex2rgb($hex) {
 
$color = str_replace('#','',$hex);
 
$rgb = array('r' => hexdec(substr($color,0,2)),
              
'g' => hexdec(substr($color,2,2)),
              
'b' => hexdec(substr($color,4,2)));
  return
$rgb;
}

function
rgb2cmyk($var1,$g=0,$b=0) {
   if(
is_array($var1)) {
    
$r = $var1['r'];
    
$g = $var1['g'];
    
$b = $var1['b'];
   }
   else
$r=$var1;
  
$cyan    = 255 - $r;
  
$magenta = 255 - $g;
  
$yellow  = 255 - $b;
  
$black  = min($cyan, $magenta, $yellow);
  
$cyan    = @(($cyan    - $black) / (255 - $black)) * 255;
  
$magenta = @(($magenta - $black) / (255 - $black)) * 255;
  
$yellow  = @(($yellow  - $black) / (255 - $black)) * 255;
   return array(
'c' => $cyan / 255,
              
'm' => $magenta / 255,
              
'y' => $yellow / 255,
              
'k' => $black / 255);
}

$color=rgb2cmyk(hex2rgb('#FF0000'));
pdf_setcolor($pdf, "both", "cmyk", $color['c'], $color['m'], $color['y'], $color['k']);
?>

U can call it with parameters (r,g,b) or just an array(r,g,b) that contains these values.

Hope it works correct. All testing was fine.
enyo vel cora
13-Jan-2004 08:32
This seems a little less complicated:

this is the color you want to use : 'FF11AA'

<?php
pdf_setcolor
($pdf, 'both', 0xFF / 255, 0x11 / 255, 0xAA / 255);
?>
php at perfectweb dotcom
30-Apr-2003 07:22
Here's a better implementation for setting HTML hexadecimal colors:

function pdf_setcolor_hex($hexcolor, $type = '')
{
   global $pdf;    ## match this to your pdf resource handle
       $color['r'] = hexdec(substr($hexcolor, 0, 2))/255;
       $color['g'] = hexdec(substr($hexcolor, 2, 2))/255;
       $color['b'] = hexdec(substr($hexcolor, 4, 2))/255;
       if ($type != 'fill' && $type != 'stroke')  $type = 'both';
       pdf_setcolor($pdf, $type, 'rgb', $color['r'], $color['g'], $color['b']);
}

Sample usage:
pdf_setcolor_hex('FFFFFF', 'fill');

-Derek
php at perfectweb dotcom
29-Apr-2003 03:59
Here's a function for converting HTML hexadecimal colors to RGB decimals suitable for this function:

$color_hex = "FF0000";      ## whatever hex color you want
$color = convert_hexcolor_rgbdec($color_hex);
pdf_setcolor($pdf, 'fill', 'rgb', $color['r'], $color['g'], $color['b']);

function convert_hexcolor_rgbdec($color_hex)
{
   $color['r'] = hexdec(substr($color_hex, 0, 2))/255;
   $color['g'] = hexdec(substr($color_hex, 2, 2))/255;
   $color['b'] = hexdec(substr($color_hex, 4, 2))/255;
   return $color;
}

-Derek
maurice dot anglebert at free dot fr
28-Apr-2003 11:25
Bonjour,

Cette fonction est utilisable pour dfinir une couleur de police.

Hello,

I search for the way of setting colors for the font in pdf and I find this.
Hope it could help.

<?php
/**
*    set font color
*    maurice.anglebert@free.fr
*/

// path for the pdf file and string to show
$pdfFilePath = $_SERVER[DOCUMENT_ROOT]."/the_dir_of_the_file/test.pdf";
$string = "The new color of my font";

$pdf = pdf_new();
pdf_open_file($pdf, $pdfFilePath);
pdf_set_info($pdf, "Title", "Test pdf");
// A4 page
pdf_begin_page($pdf, 595, 842);

// set up of the font
$font = pdf_findfont($pdf, "Courier", "host", FALSE);
   if (isset(
$font)) {
      
pdf_setfont($pdf, $font, 10) or die ("unknown font");
   }

pdf_setColor($pdf, "fill", "rgb",0, 0, 1);
// this for blue color
// pdf_setColor($pdf, "fill", "rgb",1, 0, 0) for red

pdf_set_value($pdf, "textrendering", 0);
// show at the top of the page center
pdf_show_boxed($pdf, $string, 0, 832, 595, 10, "center", "");

// resets graphic state
pdf_initGraphics($pdf);
// end of page and pdf_object
pdf_end_page($pdf);
pdf_close($pdf);
pdf_delete($pdf);

// use header to get the file
header("Content-type: application/pdf");
header("Content-length: ".filesize($pdfFilePath));
header("Content-disposition: inline; filename=test.pdf");
readfile($pdfFilePath);   
?>
psychosos at gmx dot at
17-Apr-2003 01:51
If you want to change the font color you need to set type to  "fill", not "stroke" ("both" also works, of course).
steven dot gould at NO_SPAM dot stevengould dot org
22-Dec-2001 03:44
<p>If you encounter errors of the form<br>
function 'PDF_setcolor' must not be called in 'path' scope<br>
this generally means that you've called the PDFLib functions in the wrong order. For example, the following would be *incorrect* because PDF_rect marks the beginning of the "path scope", and set_color must not be called in path scope:
<p>// THIS CODE WILL PRODUCE AN ERROR
PDF_rect($pdf,0,0,$width,$height);
PDF_setcolor($pdf,$red,$green,$blue);
PDF_fill($pdf);
<p>The correct order of function calls is as follows:
<p>// Correct order of function calls
PDF_setcolor($pdf,$red,$green,$blue);
PDF_rect($pdf,0,0,$width,$height);
PDF_fill($pdf);