in reply to Re: Formatting Input
in thread Formatting Input

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=\"http://www.w3.org/1999/xhtml\"> <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;

Replies are listed 'Best First'.
Re^3: Formatting Input
by daxim (Curate) on Jun 14, 2012 at 17:05 UTC
    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.

      As far as i can see it is when written back to the browser and into the email. It isn't when printed to the PDF Document though

      <p>$Contents</p>
        Unclear, ambiguous, lacking a clear antecedent to both instances of "it." Downvoted for that very serious shortcoming.

        You're wrapping the whole string (several paragraphs) into a single HTML paragraph tag. The grandparent was suggesting that you parse the string for paragraphs and manually insert the necessary paragraph tags.

        Do look at the HTML source your program generates and check that everything is there. You may also want to try white-space: pre; if you want a boring CSS solution (but nothing compares to actually understanding the problem!)

Re^3: Formatting Input
by ww (Archbishop) on Jun 14, 2012 at 17:00 UTC
    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/ /;
      or
    • 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 reply, not added anything to remove whitespace, wouldn't know how to

      If i have then i have done so as a typo somewhere by accident

      Someone is suggesting the problem is on line 17 but i can't find a line 17 that makes any sense no matter how i count down the number of lines, so i don't know which line of code the person is referring to!

        The reference to "line 17" is an ironic (or maybe something harsher) comment about your expectation that we could answer a question about your code without seeing it. Don't sweat it; just take heed of the fact that we can offer better help, in most cases, if we have the problem code.

        And, not just BTW, welcome to the Monastery.

A reply falls below the community's threshold of quality. You may see it by logging in.