Adding string from database into PDF

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts:282
Location:Holland Rotterdam

Adding string from database into PDF

Hello Coursesweb I have a problem with my php document to convert it to PFD
I cant find out how to get results from $users into the HTML to the PDF document

Searching trying for hours to findout but I gave-up & comming by you.

Code: Select all

<?php 
  
include('connection.php');

$id = (int) $_GET['id'];

$sql=mysqli_query($conn,"SELECT * FROM `user` WHERE id='" . $id . "' ");
$users=mysqli_fetch_assoc($sql);
?>


I cant find out why $users[name] doesnt give any results

Code: Select all

$html = '  $users[name] You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
text, such as <a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';
Under is the rest from the code

Code: Select all

$pdf = new PDF();
// First page
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Write(5,"To find out what's new in this tutorial, click ");
$pdf->SetFont('','U');
$link = $pdf->AddLink();
$pdf->Write(5,'here',$link);
$pdf->SetFont('');
// Second page
$pdf->AddPage();
$pdf->SetLink($link);
$pdf->Image('logo.png',10,12,30,0,'','http://www.fpdf.org');
$pdf->SetLeftMargin(45);
$pdf->SetFontSize(14);
$pdf->WriteHTML($html);


$pdf->Output();
?>

MarPlo Posts:186
You have error into the $html variable.
Separate the variable with data from database, and add the name between quotes (see the differences):

Code: Select all

$html = $users['name'] .'You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
text, such as <a href="//fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';
- In the rest, I don't know if there is other problem.

JanMolendijk Posts:282
MarPlo cheey I where looking for hours did not knewn it would be this simple.

The PDF document is working,, meight I gonna ask some more after progressing + testing.

So mutch thanks.

your sincerely.

JanMolendijk Posts:282
MarPlo my next problem I don`t known how to add
white-space: pre-line into the html it is not working ????

Needs help again

Code: Select all

$html = $users['post_user_name'] .'<br><br> '. $users['title'] .' <br><br> <span style="white-space: pre-line">'. $users['post_text'] .'</span> <br><br>  You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
text, such as


<br><br>
<a href="http://www.fpdf.org">Brought by @Molendijk (visit website)</a>

';

MarPlo Posts:186
For new line try add the "\n" character.
For example:

Code: Select all

$html = $users['post_user_name'] ."\n\n". $users['title'] ."\n\n Text on new line. \n Text on another new line";

JanMolendijk Posts:282
MarPlo super the fast support `just great`

Only in '. $users['post_text'] .' are topics & I realy wanna
have into post_text automatic an new line.

Hope you have some other solution for me ????

MarPlo Posts:186
If the text in $users['post_text'] contains <br>, try replace it with '\n'.

Code: Select all

$users['post_text'] = str_ireplace('<br>', '\n', $users['post_text']);
- If it is not working, try ask on stackoverflow.com.

JanMolendijk Posts:282
Thanks MarPlo I prefer not to ask anything on stackoverflow.com
because I once typed a small i instead of I.
and received many comments about it.

But I'll stick to the statement that the small i
is much more creative and beautiful than
the capital letter I.

Not even that was understood.

I let it sink in for a few days and
will ask questions there out of necessity.

Thanks again