Why does text string retain line spacing when echoed to browser but, not when used to create PDF?

Experts,

I am using TCPDF to create dynamic PDFs with no problems.

The text that I am exporting from the DB to create the PDFs is user input that I've captured using a simple PHP form.

When I export the text strings from the DB to the browser, I see the line breaks as typed by my users.

How text looks when echoed to browser
When I export the same text string from the DB to TCPDF, I don't see the line breaks as typed by my users. The text string is one long run-on sentence.

How text looks when exported to PDF
How can I export the text from my DB to TCPDF so that it retains the line spacing as typed by my users?
evibesmusicAsked:
Who is Participating?
 
Marco GasiConnect With a Mentor FreelancerCommented:
Using php you can use following code supposing $data_str hold your text grabbed from the db:

Unix-like:
$date_str = "soem text from database";
$needles = array("<br>", "<br />", "<br/>");
$replacement = "\n";
$text = str_replace($needles, $replacement, $text);

Open in new window

0
 
Julian HansenCommented:
Do you have the code you are using to export to TCPDF?
And do you have some sample data from your database?
0
 
Marco GasiFreelancerCommented:
What are the line breaks symbols used by your users. If exporting db data to html the line breaks works correctly, this means they are a series of <br /> tags but to work in pdf they must be "\n" so you need to convert "<br>" or "<br />" to "\n".
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Ray PaseurCommented:
This might be useful: http://php.net/manual/en/function.nl2br.php

The bigger question for me would be, "What formatting do you want on the printed document?"  HTML is a semantic markup language intended to attach meaning to elements of a taxonomy.  PDF is a document layout language intended to prescribe location of visual objects on the printed page.  These are not quite as different as fish and bicycles, but almost.  In my experience creating PDFs with PHP, I found that you want two different output "views" of the data.  You can use CSS to style the appearance of HTML documents, but you need a different output engine to talk to the PDF creating software like FPDF and TCPDF.
0
 
gr8gonzoConnect With a Mentor ConsultantCommented:
PDF and HTML are different languages, just like English and Spanish. So HTML handles line breaks in a certain way, and PDF handles them in another way.

In TCPDF (or FPDF, for that matter), you use the Ln() function to add a line break. You can use explode() to separate the user's content by newlines and then display them with line breaks:

$content = "Hello world,
My name is John Doe!
Sincerely,
Me";

$lines = explode("\n",$content);

foreach($lines as $line)
{
    $yourpdf->Cell( .... ); // Display the $line of text
    $yourpdf->Ln();  // Add the line break
}

Open in new window

0
 
Ray PaseurCommented:
You can also tell TCPDF how to position the cursor after the Text() method.  I haven't done as much with TCPDF as FPDF, but I've got a small teaching example that shows how to put text over an image.  It positions the text absolutely (X:Y) and I think the next line of text will fall below the first line.  You might want to give it a try in this small, self-contained example.

<?php // RAY_tcpdf_image_position.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// SYNTHESIZE THE PDF FILE NAME
$pdf_file_link    = '/RAY_tcpdf_example.pdf';
$pdf_file_name    = getcwd() . $pdf_file_link;

// THE TCPDF CLASS AND CONFIGURATION
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

// EXTEND THE TCPDF OBJECT
class PDF extends TCPDF
{
    // NULLIFY AUTOMATIC HEADER AND FOOTER
    public function Header() {}
    public function Footer() {}
}

echo "<br />Starting PDF creation" . PHP_EOL;

// INSTANTIATE THE OBJECT
$pdf = new PDF('P', 'mm', 'LETTER', true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(0,0,0,TRUE);
$pdf->SetAutoPageBreak(FALSE);
$pdf->setLanguageArray($l);
$pdf->SetFont('times', '', 13);
$pdf->setCellPaddings(0,0,0,0);
$pdf->setCellMargins(0,0,0,0);

// DO NOT DO THIS
// $pdf->setFillColor(255,255,224);


// CELL BORDERS, IF NEEDED
$bdr = '1TRBL';

// IF NO CELL BORDER IS NEEDED
$bdr = 0;

// ADD A PAGE
$pdf->AddPage('P', 'LETTER', TRUE);

// ADD AN IMAGE
// http://www.tcpdf.org/doc/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352

$img = 'RAY_EE_images/ray_padded.png';

// I HAVE NO IDEA WHAT THIS IS DOING TO IMAGE SCALE - EXPERIMENT WITH IT
$pdf->setImageScale(1.53);

// GET THE IMAGE, I HOPE
$pdf->Image
( $img
, 0             // $x
, 0             // $y
, 0             // WIDTH
, 0             // HEIGHT
, 'PNG'         // TYPE
, '#'           // LINK URL
, 'T'           // SET POINTER TOP LEFT
, FALSE         // NO RESIZING
, 300           // DPI
, 'L'           // PALIGN
, FALSE         // ISMASK
, FALSE         // IMGMASK
, 0             // BORDER
, FALSE         // FIT TO BOX
, FALSE         // HIDDEN
, FALSE         // FIT ON PAGE
)
;

// ADD SOME TEXT ON TOP OF THE IMAGE
$pdf->Text
( 24            // $x,
, 24            // $y,
, 'HELLO WORLD'
, FALSE         // $fstroke = false,
, FALSE         // $fclip = false,
, TRUE          // $ffill = true,
, $bdr          // $border = 0,
, 2             // $ln = 0, 2=PUT CURSOR BELOW
, ''            // $align = '', DEFAULT LEFT
, FALSE         // $fill = false, CELL BACKGROUND
, ''            // $link = '', NOT A LINK
, 0             // $stretch = 0, NO TEXT STRETCH
, FALSE         // $ignore_min_height = false,
, 'A'           // $calign = 'T', A=FONT TOP INSIDE CELL
, 'T'           // $valign = 'M', T=VERTICAL ALIGN INSIDE CELL
, TRUE          // $rtloff = false TRUE = USE PAGE TOP LEFT CORNER TO ALIGN
)
;

// WRITE THE PDF FILE TO THE SERVER
echo "<br />Writing PDF\n";
$pdf->Output($pdf_file_name,'F');

// PRESENT A CLICKABLE LINK SO WE CAN D/L AND PRINT THE PDF
echo "<br /><a target=\"my_PDF\" href=\"$pdf_file_link\"><strong>Print the PDF</strong></a>";

Open in new window

HTH, ~Ray
0
 
evibesmusicAuthor Commented:
@All:

This is what is stored in the database...
snapshot of dbI am exporting data from my MySQL db in order to create dynamic HTML. This HTML is what is being placed into the PDFs.

 @gr8gonzo:

Does your methodology apply to the way my information is captured in the database? There are no HTML characters to designate line spaces. This makes me unsure that the line spaces seen in the image above will be recognized by your script?
0
 
Ray PaseurConnect With a Mentor Commented:
Herein lies the problem:
This HTML is what is being placed into the PDFs.
Please go back and read the comment linked here, carefully, for understanding.  Or if you prefer, take gr8gonzo's analogy of English and Spanish.

We don't put HTML into PDFs. We put text and images into PDFs.  PDF is a document layout language.  It's not about semantics.  We don't attach meaning to the layout, we just put the data where we want it.  PDF has no CSS (at least not at this writing) and no concept of responsive design.

What I think I see in the notes column of the image would be generated by this string:

TESTING

testing

testing

Open in new window

That is the equivalent of two end-of-line characters between the lines of text.  When you look at that on a browser screen, the browser will collapse the whitespace down to one blank for rendering purposes.  But the blank lines are still there.  Compare what you see on the screen with the content that shows when you use "view source."  Then take the same web page and add two <br> tags to the end of each line and look at it again in the browser.  HTML affects the page rendering in the browser.
http://www.laprbass.com/RAY_temp_evibesmusic.php

We use HTML to create browser documents for the desktop, laptop, tablet and mobile devices.  We strip out the HTML markup and use PDF to create documents for print.
0
 
gr8gonzoConsultantCommented:
My code was banking on using the newline character as a delimiter (a line break between your lines), not on HTML.

If you're anticipating that your data will contain more complex HTML and you really need to be able to convert full HTML into a PDF, then I would suggest trying html2pdf:

http://html2pdf.fr/en/example

The downside to using a class like that is that it's not perfect and it tends to use up a lot of memory during conversion, depending on the complexity of the HTML. The more nested HTML (e.g. nested tables), the higher the memory usage will be. So if you do go down this route, make sure you save / cache the final result so you don't have to waste CPU and memory regenerating it again later on.
0
 
Julian HansenCommented:
@gr8gonzo - I suspect your code will work though based on the screen shot of the data which is showing text on different lines but no markup.
0
 
gr8gonzoConsultantCommented:
@julianH - Yeah, possibly, but you can never tell when the content is test content. The production content might be more complex. -shrug-
0
 
evibesmusicAuthor Commented:
Very bad form on my part Experts. I am sorry for not tending to this question.

The fact of the matter is that the solution was two fold based on several answers.

1) Replacing HTML line breaks with '\n' made the spaces in my HTML output appear properly in my PDF.
2) Per Ray's answer: HTML was not the appropriate output for use in a PDF. Although it accomplished most of what I wanted to display, it was inconsistent and flaky in doing so. User input which contained "<" or ">" symbols would throw the scripts for a loop.

EvibesMusic
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.