PDF-Create

 view release on metacpan or  search on metacpan

lib/PDF/Create/Page.pm  view on Meta::CPAN

    croak 'No font found !' if !defined $self->{'current_font'};

    # set up current_x/y used in stringml
    $self->{'current_y'} = $y if defined $y;
    carp 'No starting position given, using 800' if !defined $self->{'current_y'};
    $self->{'current_y'}    = 800   if !defined $self->{'current_y'};
    $self->{'current_x'}    = $x    if defined $x;
    $self->{'current_x'}    = 20    if !defined $self->{'current_x'};
    $self->{'current_size'} = $size if defined $size;
    $self->{'current_size'} = 12    if !defined $self->{'current_size'};

    # print the line(s)
    my $n = 0;
    for my $line ( split '\n', $s ) {
        $n++;
        $self->string($self->{'current_font'}, $self->{'current_size'}, $self->{'current_x'}, $self->{'current_y'}, $line);
        $self->{'current_y'} = $self->{'current_y'} - $self->{'current_size'};
    }

    return $n;
}

=head2 block_text(\%params)

Add block of text to the page. Parameters are explained as below:

    +------------+--------------------------------------------------------------+
    | Key        | Description                                                  |
    +------------+--------------------------------------------------------------+
    | page       | Object of type PDF::Create::Page                             |
    | font       | Font index to be used.                                       |
    | text       | Text block to be used.                                       |
    | font_size  | Font size for the text.                                      |
    | text_color | Text color as arrayref i.e. [r, g, b]                        |
    | line_width | Line width (in points)                                       |
    | start_y    | First row number (in points) when adding new page.           |
    | end_y      | Last row number (in points) when to add new page.            |
    | x          | x co-ordinate to start the text.                             |
    | y          | y co-ordinate to start the text.                             |
    +------------+--------------------------------------------------------------+

    use strict; use warnings;
    use PDF::Create;

    my $pdf  = PDF::Create->new('filename'=>"$0.pdf", 'Author'=>'MANWAR', 'Title'=>'Create::PDF');
    my $root = $pdf->new_page('MediaBox' => $pdf->get_page_size('A4'));
    my $page = $root->new_page;
    my $font = $pdf->font('BaseFont' => 'Helvetica');

    $page->rectangle(30, 780, 535, 40);
    $page->setrgbcolor(0,1,0);
    $page->fill;

    $page->setrgbcolorstroke(1,0,0);
    $page->line(30, 778, 565, 778);

    $page->setrgbcolor(0,0,1);
    $page->string($font, 15, 102, 792, 'MANWAR - PDF::Create');

    my $text = qq{
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It h...
    };

    $page->block_text({
        page       => $page,
        font       => $font,
        text       => $text,
        font_size  => 6,
        text_color => [0,0,1],
        line_width => 535,
        start_y    => 780,
        end_y      => 60,
        'x'        => 30,
        'y'        => 770,
    });

    $pdf->close;

=cut

sub block_text {
    my ($self, $params) = @_;

    croak "ERROR: parameters to method block_text() should be hashref.\n"
        unless (defined $params && (ref($params) eq 'HASH'));

    my $page       = $params->{page};
    my $font       = $params->{font};
    my $text       = $params->{text};
    my $font_size  = $params->{font_size};
    my $text_color = $params->{text_color};
    my $line_width = $params->{line_width};
    my $start_y    = $params->{start_y} || 0;
    my $end_y      = $params->{end_y} || 0;
    my $x          = $params->{x};
    my $y          = $params->{y};
    my $one_space  = $page->string_width($font, ' ') * $font_size;

    my $para_space_factor = 1.5;
    $para_space_factor = $params->{para_space_factor}
        if (exists $params->{para_space_factor}
            && defined $params->{para_space_factor});

    my @lines = ();
    foreach my $block (split /\n/, $text) {
        my @words = split(/ /, $block);
        my $para_last_line = 0;

        while (@words) {
            my $num_words    = 1;
            my $string_width = 0;
            my $space_width  = undef;

            while (1) {
                $string_width = $font_size * $page->string_width(
                $font, _get_text(\@words, $num_words));

                # Shorter, try one more word
                if ($string_width + $one_space < $line_width) {
                    if (scalar(@words) > $num_words) {
                        $num_words++;



( run in 0.744 second using v1.01-cache-2.11-cpan-e1769b4cff6 )