Charles K. Clarkson wrote:
Daniel Kasak <mailto:[EMAIL PROTECTED]> wrote:I think I *did* provide all necessary code. Actually I'm 100% certain that I did, but if you want it all, here you go ... you will now see why I snipped the code to a resonable size and ommitted everything that wasn't relevant:
: Hi all.
: : Despite making some decent progress in other areas, I'm banging my
: head against a brick wall on something simple: rendering text over a
: solid rectange.
You didn't provide enough code. Show us everything we need to
reproduce your error. You do not, for example, explain what 'mm' is
or where the constants are defined. Is $self part of an object? Is
this whole code snippet part of a module? Give us more to go on.
test.pl:
---
#!/usr/bin/perl -w
use strict;
use reporter; use DBI;
use constant mm => 25.4/72; # 25.4 mm in an inch, 72 points in an inch
use constant in => 1/72; # 72 points in an inch
use constant pt => 1; # 1 point
use constant A4_x => 210 / mm; # x points in an A4 page ( 595.2755 )
use constant A4_y => 297 / mm; # y points in an A4 page ( 841.8897 )
my $dbh = DBI->connect ("dbi:mysql:dbname=sales;host=screamer;port=3306", "*****", "*****", {
PrintError => 0,
RaiseError => 0,
AutoCommit => 1, });
my @fields = ( { name => "Prospect", percent => 40, footer => "count" }, { name => "Address", percent => 20 }, { name => "Salesperson", percent => 20 }, { name => "Time", percent => 10 }, { name => "Note", percent => "10" } );
my $data = $dbh->selectall_arrayref(
"select Client, Address, concat(FirstName, ' ', LastName), NoteTime, Description"
. " from Prospects P inner join Notes N on P.LeadNo=N.LeadNo"
. " inner join NoteShortResponses NSP on N.ShortResponse=NSP.ID"
. " inner join Leads L on P.LeadNo=L.LeadNo"
. " inner join AllStaff S on L.IssuedTo=S.ID"
. " where CompleteDate is null and NoteDate=curdate()");
my $report_def = {
row_spacing => 20,
report => {
header => {
x => 100,
y => 100,
text => "Report Header Test",
font => "Times New Roman"
},
footer => {
x => 100,
y => 100,
text => "Report Footer Test",
font => "Times New Roman"
}
},
page => {
header => {
x => 40,
y => 60,
text => "I go at the top of each page",
font => "Times New Roman"
},
footer => {
x => 40,
y => 40,
text => "I go at the bottom of each page",
font => "Times New Roman"
}
},
data => {
x => 100,
y => 40,
font_size => 12,
fields => [EMAIL PROTECTED],
data_array => $data
}
};
my $thingy = reporter->new ( $report_def );
---
reporter.pm:
---
#!/usr/bin/perl
use strict;
package reporter;
use PDF::API2;
use constant mm => 25.4/72; # 25.4 mm in an inch, 72 points in an inch
use constant in => 1/72; # 72 points in an inch
use constant pt => 1; # 1 point
use constant A4_x => 210 / mm; # x points in an A4 page ( 595.2755 )
use constant A4_y => 297 / mm; # y points in an A4 page ( 841.8897 )
# Globals my ( $page, $txt, $x, $y, $fields_def );
sub new {
my ( $class, $self ) = @_;
bless $self, $class;
# Create a new PDF document
$self->{pdf} = PDF::API2->new;
$self->{fonts} = {
Helvetica => {
Bold => $self->{pdf}->corefont('Helvetica-Bold', -encoding => 'latin1'),
Roman => $self->{pdf}->corefont('Helvetica', -encoding => 'latin1'),
Italic => $self->{pdf}->corefont('Helvetica-Oblique', -encoding => 'latin1'),
},
Times => {
Bold => $self->{pdf}->corefont('Times-Bold', -encoding => 'latin1'),
Roman => $self->{pdf}->corefont('Times', -encoding => 'latin1'),
Italic => $self->{pdf}->corefont('Times-Italic', -encoding => 'latin1'),
}
};
$self->{page_count} = -1;
$self->new_page;
#
# ============================================================================================
# Report Header
# ============================================================================================
# Create a gfx object for the report header
#my $blue_box = $page->gfx;
#$blue_box->fillcolor('blue');
#$blue_box->rect(
# 20 * mm, # left
# A4_y - (100 * mm), # bottom
# A4_x - (40 * mm), # width
# 80 * mm # height
# );
#$blue_box->fill;
# Create a text object for the report header
$txt->translate( $self->{report}->{header}->{x} * mm, A4_y - ( $self->{report}->{header}->{y} * mm ) );
$txt->font( $self->{fonts}->{Times}->{Bold}, 18 );
#$txt->fillcolor("white");
$txt->fillcolor("green");
$txt->text($self->{report}->{header}->{text});
$txt->fillcolor("black");
# Set up data field sizes for later use
$x = $self->{data}->{x} * mm;
for my $field_definition ( @{$self->{data}->{fields}} ) {
$field_definition->{x} = $x;
$field_definition->{width} = ( A4_x - ( $self->{data}->{x} * mm * 2 ) ) * $field_definition->{percent} / 100;
$x += $field_definition->{width};
}
$self->page_header ( 0, $self->{row_spacing} * mm ); # Run page_header, with a y_offset for the report header
# ============================================================================================
# Main loop
# ============================================================================================
for my $row ( @{$self->{data}->{data_array}} ) {
# Create new page if necessary
if ( $y - ( $self->{page}->{y} + $self->{row_spacing} ) < 0 ) {
$self->new_page;
$self->page_header;
}
# Render row
my $field_counter = 0;
for my $field ( @{$self->{data}->{fields}} ) {
$txt->translate( $field->{x}, $y );
$txt->text($$row[$field_counter]);
$field_counter ++;
}
$y -= $self->{row_spacing};
}
# ============================================================================================
# Report Footer
# ============================================================================================
$self->{pdf}->saveas("test.pdf");
$self->{pdf}->end();
}
sub new_page {
my $self = shift;
$self->{page_count} ++;
$self->{pages}[$self->{page_count}] = $self->{pdf}->page;
# Create a reference to the above page ( ease strain on eyes )
$page = $self->{pages}[$self->{page_count}];
# Set page dimensions
$page->mediabox(A4_x, A4_y);
$txt = $page->text;
$txt->font( $self->{fonts}->{Times}->{Roman}, $self->{data}->{font_size} );
$txt->fillcolor("black");
}
sub page_header {
# Renders a new page header. Expects to already have a new page created.
my ( $self, $x_offset, $y_offset ) = @_;
$x = $self->{page}->{header}->{x} + $x_offset;
$y = ( A4_y - $self->{page}->{header}->{y} ) - $y_offset;
$txt->font( $self->{fonts}->{Times}->{Bold}, 14 );
for my $field ( @{$self->{data}->{fields}} ) {
$txt->translate( $field->{x}, $y );
$txt->text($field->{name});
}
$txt->font( $self->{fonts}->{Times}->{Roman}, $self->{data}->{font_size} );
$y -= $self->{row_spacing};
}
1;
-- Daniel Kasak IT Developer NUS Consulting Group Level 5, 77 Pacific Highway North Sydney, NSW, Australia 2060 T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989 email: [EMAIL PROTECTED] website: http://www.nusconsulting.com.au
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>