Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Formatting Input

by akwe-xavante (Novice)
on Jun 14, 2012 at 15:20 UTC ( #976230=perlquestion: print w/replies, xml ) Need Help??
akwe-xavante has asked for the wisdom of the Perl Monks concerning the following question:

A Formatting Input Question - I Think?

I have a web form, when several sentances of text are entered and or paragraphs of text with spaces i have a problem

When the data is processed by a perl script and either printed back to the browsers screen or written to a PDF Document the blank lines between sentances or and paragraphs are removed and the text is printed out as a single block of text

How can i prevent these blank lines between sentances or paragraphs from being removed?

If user inputs lines or paragraphs with spaces i want these blank lines to be present when the data is printed to the browsers screen and in the PDF Document created

Hope someone can help me

Replies are listed 'Best First'.
Re: Formatting Input
by zentara (Archbishop) on Jun 14, 2012 at 18:11 UTC
    How can i prevent these blank lines between sentances or paragraphs from being removed?

    Just a wild guess, but maybe put your $Content text into double quotes, so that it gets interpolated for newlines. "$Content" .

    Or force the newlines to be converted to html returns

    $Content =~ s/\n/<br>/g; #untested

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thank you for your reply, infact thanks to everybody for your replies. Having now had my problem pointed out to to me i now feel a complete fool because i should of known what the problem was myself

      Now i have had the problem pointed out to me i now understand my problem and with this i can now find an answer. I shall explore using the <pre> tags and adding this to my my CSS to format the texts font style, color and size and then enclose the text into <div> tags to restrict width of the text area

      Not realy thought it through yet though, a job for monday next week

      This, or whatever i finally use will resolve my HTML printed back to the browser and printed in the email but won't resolve the problem when printed into the PDF Document so i'll explore this problem later next week

      Thank you all for your help.

        <moment>DUH! It just occurred to me that you're probably not taint-checking the user input.</DUH-moment>

        That's dangerous; DANGEROUS, I say.

        Use taint checking on user input, always! (and using <pre> is not nearly as good a cure for your problem as a variant of advice offered above: s/\n/<br>/m;)

Re: Formatting Input
by Anonymous Monk on Jun 14, 2012 at 15:33 UTC

    How can i prevent these blank lines between sentances or paragraphs from being removed?

    The only way they get removed is if some code you wrote (or call) removes them -- the solution is to not do that

    Without specific code we can't help you, see How do I post a question effectively?

      Nah, the problem's on line 17. Bleedin' obvious.

      (Yeah, what he said. See also How (Not) To Ask A Question)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      My Perl / cgi script gets data from the webform in this way

      The script gets data from a webform then prints back to the browser, creates a PDF Document and sends an email with the PDF document as an attachment

      The script is long so i've only printed what i think you'l need here - i hope

      #!/usr/bin/perl use strict; use CGI ':standard'; use PDF::API2; use MIME::Lite; use utf8; $Name = param('name'); $Email = param('email'); $Subject = param('subject'); $Contents = param('contents');

      If the users data entered into the variable $Contents contains spaces between lines or paragraphs, these blank lines are removed somewhere during the process of printing it back to the browsers screen and into a PDF Document and the email

      Simplistically its printed back to the browser:

      print "Content-Type:text/html\n\n"; <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.or +g/TR/xhtml11/DTD/xhtml11.dtd\"> <html xmlns=\"\"> <head></head> <body> <p>Thank you $Name for your message entitled $Subject</p> <br> <p>Your Message was:</p> <br /> <p>$Contents</p> <br /> <p>I will contact you as soon as possible if a response is required.</ +p> <br /> <h2>$MyName</h2> <br /> <br /> <br /> </body </html>

      And Printed into the PDF Document:

      $txt->font($fnt, 8); $txt->translate(25,740); $txt->fillcolor('black'); $txt->text("$Contents");

      And added to an email:

      my $Message = " <p>Thank you $Name for your email.</p> <p>Subject: $Subject</p> <p>Your Message was:</p> <p>$Contents</p> "; my $msg = MIME::Lite->new ( From => $From, To => $To, Subject => "Subject: re: $Subject", Type =>'multipart/mixed' ) or die "Error creating multipart container: $!\n"; ###### Add the text message part # $msg->attach ( Type => 'HTML', Data => "$Message", ) or die "Error adding the text message part: $!\n"; ##### Send the Message $msg->send;
        The problem is that you don't know how HTML works. Consecutive whitespace, such as your line breaks, is collapsed into a single space for display purposes.

        That means before you print your variable contents in HTML, you need to prepare it appropriately. For example, you could wrap whole paragraphs of text into a <p></p> element.

        Nice snippets... but they show nothing that removes common whitespace chars (\s\t\r\n).

        So, in a case of the blind (perhaps mis-) leading the blind, you might want to look at your cgi again, for something like one of these WAGs:

        •   chomp...
        •   s/\./\s$/\./; or s/\n/ /;
        • a regex ending in /s or /m

        This may not help, but it might rule out a few more possibilities.

        Update: Should have said earlier that the initial reply encapsulates the approach to a solution. + +. But also, I read, only after posting the initial version of this node, daxim's node re collapsing of white space and OP's reply, which, though ambiguous, may cast the issue in a different light.

        Thank you for your answer! doesn't really help though

        Line 17 is a blank line!

        Sometimes you just do not know what the question is to get the answer your looking for, i've obviously asked the wrong question or assumed my problem is a formatting issue, i don't know

        I'm still no wiser, smart arse!

        But thank you though, i'm assuming you think that i know what i'm doing - I don't!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://976230]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-06-22 06:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.