Hello all,

I am trying to format a string properly in PDF but I just can't get it right after many tries and research. I only have so much space in the PDF report so I am trying to squeeze each character in the given space. So far I have the following code. (Note: I am using PDF::Reuse)

printAdjustedField(238, $LINE_POS, uc($RECS->[$i]{'EVENT_WHERE'}), 45) +; #45 is the length of one line sub printAdjustedField { my ($left_pos, $line_height, $case_field, $allowed, $align) = @_; my ($field_str, $line_pos) = ('', 0); if($case_field =~ /(.*?)\\+$/) { $case_field = $1; } my @field = split /\s/, $case_field; if($allowed == 0 || ((2.5*length($case_field)) - 1 <= $allowed)) { prText($left_pos, $line_height, $case_field, $align); } else { for $cnt (0 .. $#field) { $field_str .= $field[$cnt]; if($cnt != (scalar @field - 1)) { $line_pos = (rindex($field_str, "\n") > 0) ? rindex($field_str +, "\n") : 0; $field_str .= (2.5*(length(substr($field_str, $line_pos)) + le +ngth($field[$cnt+1])) < $allowed) ? ' ' : "\n"; } } @field = split("\n", $field_str); for $pos (0 .. $#field) { prText($left_pos, $line_height + (6*(scalar @field - ($pos+1))), + $field[$pos], $align); } } }

My PDF report consists of several columns. I am trying to format the strings, retrieved from an oracle table, on the 5th column named Event Where. Currently my perl code is searching for a space to go to a new line but if there are no spaces, it spills over to the next column. 45 is the length of each line as you can see from the subroutine definition.

In my test case I used a string with 40 W's:

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

But the column in the PDF is not wide enough to store this string in one line and it is writing over the 6th column.

If a word is going over the 5th column, it should hyphen to the next line like so:

WWWWWWWWWWWWWW-

WWWWWWWWWWWWWW-

WWWWWWWWWWWW

I am currently using prFont(Courier New) prFontSize(5). Another thing to take into consideration is with this font, different characters will take up different amount of space. For example "I" will take up less space then "W".

I hope someone can help me out with this. I know we may realistically rarely ever have such long words as used in my example, but if there are two long words and the second word goes over the space provided, it needs to be hyphened instead of going to a new line.


In reply to Terminate new line with hyphen in PDF report by jdm2490

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.