could you post your additions to add color? Sounds great :)
Sure. Eventually, I'd like to get it on CPAN, but I'm not sure how long it can still take, so in the meantime, here's my add-on module for PDF::Create, which I gave the name PDF::Create::Extensions. All you need is this extra file, which adds a few methods to PDF::Create::Page, and they're available as soon as you do
use PDF::Create; use PDF::Create::Extensions;
You can create two kinds of colour, "linecolor" (for lines) and "fillcolor" (for area fills). For each, you can set the color in the same way, in RGB ("RGB" or "RG": red, green, blue, each value ranging from 0 to 1, less is darker), grayscale ("GRAY", "GREY" or "G", 1 value from 0 (white) to 1 (black); and CMYK ("CMYK" or "K", 4 values from 0 to 1, more is darker), like this:
$page->linecolor(RGB => 0.6, 0.2, 0.2);
The color type is case insensitive.

The second thing you can set, is line width, line connection style ("join": styles "miter", "round", "bevel"), and line end style ("cap": styles "butt", "round", "square" (="projecting square")), like this:

$page->linewidth(0.8, 'join' => 'miter');
See the PDF documentation on what the possibilities represent, or just experiment with these options. Again, the names are case insensitive.

Save the next code as PDF/Create/Extensions.pm somewhere in your @INC

# file PDF/Create/Extensions.pm package PDF::Create::Extensions; package PDF::Create::Page::Extensions; require PDF::Create::Page; push @PDF::Create::Page::ISA, __PACKAGE__; use strict; use Carp; my %color_mode = ( GRAY => [ G => 1 ], RGB => [ RG => 3 ], CMYK => [ K => 4 ], ); foreach(keys %color_mode) { $color_mode{uc $color_mode{$_}[0]} ||= $color_mode{$_}; } $color_mode{GREY} = $color_mode{GRAY}; sub _modecolor { my $mode = uc shift; if(my $info = $color_mode{$mode}) { $#_ = $info->[1] - 1; return @_, $info->[0]; } croak "'$mode' is not a valid color mode"; } sub fillcolor { my $self = shift; my @cmd = _modecolor(@_); $_ = lc for $cmd[-1]; $self->{'pdf'}->page_stream($self); $self->{'pdf'}->add(join ' ', @cmd); } sub linecolor { my $self = shift; my @cmd = _modecolor(@_); $_ = uc for $cmd[-1]; $self->{'pdf'}->page_stream($self); $self->{'pdf'}->add(join ' ', @cmd); } my %line_styles = ( CAP => [ J => { BUTT => 0, ROUND => 1, 'PROJECTING SQUARE' => 2, + SQUARE => 2 }], JOIN => [ j => { MITER => 0, ROUND => 1, BEVEL => 2 }], ); sub linewidth { my $self = shift; $self->{'pdf'}->page_stream($self); my @add = (shift, 'w'); while(@_) { my $key = uc shift; my $value = uc shift; if(my $dispatch = $line_styles{$key}) { if($value =~ /^\d+$/) { } elsif(exists $dispatch->[1]{$value}) { $value = $dispatch->[1]{$value}; } else { croak "'$value' is not a valid parameter for line styl +e mode '$key'"; } push @add, $value, $dispatch->[0]; } else { croak "'$key' is not a valid line style key"; } } $self->{'pdf'}->add(join ' ', @add); } 1;


In reply to PDF::Create::Extensions (was: Re: How do I replace certain character if condition exists) by bart
in thread How do I replace certain character if condition exists by peppiv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.