sub _apply_defaults {
...
# Create a new page inside our .pdf unless a page was provided.
unless (defined $self->page) {
$self->page($self->pdf->page);
}
...
}
That is, if you supply an existing page to the constructor like this
my $pdf = PDF::API2->new( -file => "mytest.pdf" );
my $page = $pdf->page();
my $tb = PDF::TextBlock->new({
pdf => $pdf,
page => $page, # <---
...
then PDF::TextBlock won't create a new one itself within the apply() method.
(you have to pass the same $page to the other constructor as well, of course, if you
want both blocks to appear on the same page...)
2. Would it be possible to alter the TextBlock.pm to change the
colour of the text especially inside the 'markup' tags (so hyperlinks
could appear blue or bold text appear red etc)? If this is not possible
could one specify the colour of an entire text block
For tags other than href you just have to define a
fillcolor for the custom font that you're setting for the
specific tag. E.g. for <b>:
b => PDF::TextBlock::Font->new({
pdf => $pdf,
font => $pdf->corefont( 'Helvetica-Bold', -encoding => '
+latin1' ),
fillcolor => '#ff0000', # red
}),
An analogous approach for hyperlinks, such as
href => PDF::TextBlock::Font->new({
pdf => $pdf,
font => $pdf->corefont( 'Helvetica-Oblique', -encoding =
+> 'latin1' ),
fillcolor => '#0077ff', # blue
}),
unfortunately doesn't work out of the box... For one, because
the href tag undergoes special built-in handling (underlining, link
annotation, ...). Secondly, because the regex that's being used to
filter out the tags is extracting the entire part in between <...> -
i.e. href="http://www.omnihotels.com" in your case - so you'd
have to define a separate custom font object for each and every link:
'href="http://www.omnihotels.com"' => PDF::TextBlock::Font->ne
+w({
...
}),
'href="http://www.foo.bar"' => ...,
Probably not what you want...
Anyway, here's a quick hack that seems to do the job (i.e. you can
then use the above mentioned href => ... font definition
similiarly to b => ..., etc.
In case you need more functionality, it's probably time to get in contact with the author of the module... :)
The complete diff is (your patch included (the last section)):
$ diff -u TextBlock.pm.orig TextBlock.pm
--- TextBlock.pm.orig 2009-07-13 18:45:27.000000000 +0200
+++ TextBlock.pm 2009-09-03 15:52:45.128961624 +0200
@@ -151,7 +151,7 @@
# Build %content_texts. A hash of all PDF::API2::Content::Text obj
+ects,
# one for each tag (<b> or <i> or whatever) in $text.
my %content_texts;
- foreach my $tag (($text =~ /<([^\/].*?)>/g), "default") {
+ foreach my $tag (($text =~ /<(\w*)[^\/].*?>/g), "default") {
+
next if ($content_texts{$tag});
my $content_text = $page->text; # PDF::API2::Content::Text
+ obj
my $font;
@@ -307,6 +307,7 @@
if ($tag =~ /^href/) {
($href) = ($tag =~ /href="(.*?)"/);
# warn "href is now $href";
+ $current_content_text = $content_texts{href} if ref
+ $content_texts{href};
} elsif ($tag !~ /\//) {
$current_content_text = $content_texts{$tag};
}
@@ -350,7 +351,8 @@
if ($word =~ /\//) {
if ($word =~ /\/href/) {
undef $href;
- } else {
+ }
+ unless ($href) {
$current_content_text = $content_texts{default};
}
}
@@ -374,7 +376,10 @@
# Don't yet know why we'd want to return @paragraphs...
# unshift( @paragraphs, join( ' ', @paragraph ) ) if scalar(@parag
+raph);
- return ( $endw, $ypos ); # , join( "\n", @paragraphs ) )
+ #return ( $endw, $ypos ); # , join( "\n", @paragraphs ) )
+ unshift( @paragraphs, join( ' ', @paragraph ) ) if scalar(@paragra
+ph);
+ my $overflow = join("\n",@paragraphs);
+ return ( $endw, $ypos, $overflow); #$overflow text returned to
+script
}
|