0

I have problems with connecting to database within fpdf.php code. The code works without sql connection include("db_connection.php"). My database connection (works):

session_start();
ini_set('display_errors', true);

$hostname = "localhost";
$database = "db";
$username = "username";
$password = "password";

$db = mysqli_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 
mysqli_select_db($db, $database);
?>

My fpdf.php code:

<?php
define('FPDF_FONTPATH','.');
use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;

require('fpdf.php');
require('src/autoload.php');


include_once('db_connection.php');
    if ($_POST['id_user_output'] ) { //button
        $idUser=$_POST['id_user_output'];
        $sql="SELECT * FROM user where id_user='$idUser'";
        $result1 = mysqli_query($db,$sql);
        $row = mysqli_fetch_assoc($result1);
        $field1name = $row["id_user"];
        $field2name = $row["name"];
        $field3name = $row["surname"];
        }   


$pdf = new FPDI();
$pdf->SetAutoPageBreak(false);
$pageCount = $pdf->setSourceFile('EMPTY-UPN.pdf');
$pageId = $pdf->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);

$pdf->addPage();
$pdf->useImportedPage($pageId, 0, 0, 210);
$pdf->AddFont('CourierNewPS-BoldMT','','courbd.php');

$pdf->SetFont('CourierNewPS-BoldMT','',8);//,'B',10);
//$pdf->setFontSize(10);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(5, 207);
$pdf->Write(0, '$field1name');

$pdf->SetFont('CourierNewPS-BoldMT','',8);//,'B',10);
//$pdf->setFontSize(10);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(5, 211);
$pdf->Write(0, '$field2name');

// namen plačnika talon
$pdf->SetFont('CourierNewPS-BoldMT','',8);//,'B',10);
//$pdf->setFontSize(10);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(5, 223);
$pdf->Write(0, '$field3name');

$pdf->Output();
?>

It works if I don't include db_connection.php otherwise it gives me error "Failed to load PDF document". If I hardcode fieldnames with strings the pdf is loaded correctly. Any help would be helpful.

vidooo
  • 135
  • 1
  • 1
  • 9
  • _Side note:_ The call: `$pdf->Write(0, '$field1name');` will literally write `$filed1name`. If you want to write the _value_, remove the quotes: `$pdf->Write(0, $field1name);` – M. Eriksson Jan 18 '20 at 12:46
  • My guess is that your code throws some error/warning/notice that messes up the PDF output. Try commenting out `$pdf->Output()` and access the page. Is it empty or do you see any messages? – M. Eriksson Jan 18 '20 at 12:48
  • You are also _wide open_ for SQL injection attacks. You should use parameterized [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of using completely unescaped user data directly in your queries. – M. Eriksson Jan 18 '20 at 12:50
  • It’s not the output that messes up, if I just delete the include of database connection there is no errors. – vidooo Jan 18 '20 at 16:24
  • Did that mean that you tried what I suggested, or that you "just know" that it isn't the issue? – M. Eriksson Jan 18 '20 at 23:07
  • If I comment out the output there is no errors and messages, just a blank page. – vidooo Jan 20 '20 at 08:50

1 Answers1

0

So I solved problem with adding ob_start(); at the begginging of php file, which turns on output buffering. And added ob_end_flush(); at the end of php file. Might be helpful to someone.

vidooo
  • 135
  • 1
  • 1
  • 9