And the comments and solutions are:

  1. In lines 2 and 3 there is no need to send the header. FPDF will take care of all.
  2. Before line 11 you should use a SetFont() FPDF method to set the font you wish to use
  3. On lines 24 and 25 you used mysql_query function twice, with errors in syntax
  4. On line 28 you used a nonexisting index 1 (only index 0 exists)
  5. Before line 30 you should again use SetFont() FPDF method (I think)
  6. I do not know where the values $row['id'] and $row['name'] come from. I ignored them. Make sure they exist. (credits also to pzuurveen)
  7. Make sure you can write to the current directory or add a parameter to the Output() method (like 'D' for Download)
  8. I do not know what is the point of line 30. Get rid of it. (credits also to pzuurveen)

The code that works for me is (a bit rearranged to be more clear):

<?php
$link=mysql_connect('localhost:3306','root','');
mysql_select_db('uploadimage');-
$query = "SELECT name FROM image1 WHERE id=1";
$result = mysql_query($query);
$row=mysql_fetch_row($result);
$name=$row[0];

require("fpdf.php");
class PDF1 extends FPDF
{
    // Page header
    function Header()
    {
        // Logo
        $this->SetFont('Arial','B',12);
        $this->Cell(0,10,'APPLICATION FORM FOR ADMISSION INTO MEDICAL PG DEGREE/DIPLOMA COURSES',0,0,'C');
        $this->Ln(6);
        $this->Cell(0,10,'UNDER MANAGEMENT QUOTA FOR THE ACADEMIC YEAR 2014-2015',0,0,'C');
        $this->Ln(6);
        $this->Cell(0,10,'-----',0,'C');
        // Line break
        $this->Ln(15);
    }
}

$pdf = new PDF1();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$pdf->Image($name);
// $pdf->Cell(0,10,'id'.$row['id'],0,1);
// $pdf->Cell(0,10,'name'.$row['name'],0,1);
$pdf->Cell(0,10,'id',0,1);
$pdf->Cell(0,10,'name',0,1);
$pdf->Output("image.pdf", 'D');
?>

The code works provided that the image is present and the path to it is correct.

wow this is also my usual qusetion

Thank you very much broj1 sir..code is working.

Thank you very much zuurveen sir..for your response and the code is working

You are welcome. Please mark the thread as solved. Happy coding.

one question sir.when the image are display in pdf.How to resize that image.

Use the resize parameter of the image function to scale the image . See the documentation.

Something like:

// set the required parameters
$posX = 100;      // position x of the upper left corner 
$posY = 100;      // position y of the upper left corner 
$newWidth = 400;  // new width that you wish to scale to 
$newHeight = 300; // new height that you wish to scale to
$type = '';       // will be kept default
$link = '';       // will be kept default
$align = '';      // will be kept default
$resize = true;   // this will make the image resize

// add the scaled image
$pdf->image($name, $posX, $posY, $newWidth, $newHeight, $type, $link, $align, $resize);

Thank you very much broj1 sir.

I have this issue a year later apologies for that,
This is my problem a getting an error
Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Image file has no extension and no type was specified: 2' in C:\wamp64\www\testlogin\fpdf\fpdf.php on line 271
( ! ) Exception: FPDF error: Image file has no extension and no type was specified: 2 in C:\wamp64\www\testlogin\fpdf\fpdf.php on line 271

Below is my code kindly assist

$query = ("SELECT * FROM tbl_uploads t where id =2");
$result = mysqli_query($db, $query) ;

$row = mysqli_fetch_row($result);

// Get the image from each row
$url = $row[0];
  // Place the image in the pdf document

$pdf->Image($url);

$pdf->Output('Profile.pdf','i');

Member Avatar for diafol

Please start your own thread.

From a cursory look, the $url needs to be a url to an image file with an extension like png/gif/jpg/jpeg. However it could also be to a php file that generates an image file.
ALso,

$pdf->Output('Profile.pdf','i');

May be in the wrong order: http://www.fpdf.org/en/doc/output.htm

$pdf->Output('I','profile.pdf');

Maybe?

Hi i'm anis, how can i attach images from database to fpdf?

Can any one help me. I want to upload a image file using html & same uploaded image need to be displayed and print in pdf i'm using fpdf. Is bit urgent please help me.

my piece of code in HTML: (the code will upload the file and show preview "Note : Uploaded image will not store in any path")

            <div class="row">
              <div class="col-md-12 form-group container">
                    <div> Upload Image</div>
                    <input type="file" class="form-control" id="fileToUpload" placeholder="Spare Image" name="fileToUpload1" onchange="Oneloadfile(event)">
                    <img  id="preimage1" name="preimage1" width="330px" height="200px"> 
              </div>
            </div>

            <!--Image Previewer start -->
            <script type="text/javascript">
            function Oneloadfile(event){
   var output=document.getElementById('preimage1');
   output.src=URL.createObjectURL(event.target.files[0]);
   };

</script>

Here is my pdf.php code

// Header1
$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,10,"Upload Image",1,1);

// Data1
$pdf->SetFont('Arial','',12);
$pdf->Cell(0,96,$fileToUpload1,1,1);
$pdf->Ln(10);
commented: Be sure to not bury your new question under what may be 8 years of another discussion. -4

Thank you very much broj1 sir..code is working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.