Jump to content

problem with FPDF and variables


Juliette
Go to solution Solved by Barand,

Recommended Posts

Hello, i have a problem with displaying .jpg images in pdf, when using php & FPDF.

 

If I write path to image in '2php.php' manually, it's working properly (image is displayed in created doc.pdf file) but when I try to use it with variables received from 'main.php' an error occures. Error: FPDF error: Unsupported image type: jpg

Is there some solution to this? tnx :)

 

MAIN.PHP:

<html>
<body>

<?php
  $PATH = "path to image dir..";	
  $file = "pic.txt"; 
  $lines = count(file($file)); 
  $handle = @fopen($file, "r"); 

  if ($handle) { 
    while (!feof($handle)) { 
      $pic_array[] = fgets($handle, 4096); 
     } 
    fclose($handle); 
  } 
?>

<table  BORDER="1">
<?php for($counter = 0;$counter<=$lines; $counter++){ ?>
<tr>
<td><?php echo $pic_array[$counter];?></td>

<td> <form method="POST" action="2php.php" name="form" id="form">
<input type="hidden" name="path" value="<?php echo $PATH; ?>">
<input type="hidden" name="pic" value="<?php echo $pic_array[$counter]; ?>">
<input type="submit" value="<?php echo $pic_array[$counter];?>" name="PDF"></td></tr>
<?php }?>
</form>
</td></table>

</body>
</html>

 

2php.php 

<?php
require('fpdf.php');

$pic = $_POST["PDF"];
$path=$_POST['path'];
$path1 ="".$path."".$pic.""; 
//$pic1 = 'pic1.jpg';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->Image($path1, 1, 10, 250, 250);
//$pdf->Image($pic, 1, 10, 250, 250);
//$pdf->Image($pic1, 1, 10, 250, 250);

$pdf->Cell(40,10,$path1);
//$pdf->Cell(40,10,$pic1);
$pdf->Output();

?>

 

example of pic.txt:

pic1.jpg
pic2.jpg
picure3.jpg
image.jpg

 

 

Link to comment
Share on other sites

For security reasons, you may want to consider an alternate approach. As it stands, someone could easily create a form and pass undesired content to the PDF creator.

 

Instead of passing all the image information via the form, you could pass some sort of image ID. The script for generating the PDF would then pull the proper image data based on the ID.

Link to comment
Share on other sites

its working with trim! Great.

 

And about this

"PDF" is the name of your submit button!

Don't know why but that's the only way to successfully pass value $pic_array[$counter]; to another file. I tried with

<input type="hidden" name="pic" value="<?php echo $pic_array[$counter]; ?>">, as you can see, but its not working..

 

Edit: $counter from first file is sending number of last line (the empty one) , and not the number of chosen picture. Any suggestions?

Edited by Juliette
Link to comment
Share on other sites

Have you looked at the source code which was sent to the browser? Do the fields contain what you expect? If so, have you tried echoing the variables in the PHP script which generates the PDF? Do they display values that you expect?

 

If you still need help, it may be helpful to see the newest iteration of the code.

Link to comment
Share on other sites

  • Solution

 

 

Don't know why but that's the only way to successfully pass value $pic_array[$counter]; to another file. I tried with

<input type="hidden" name="pic" value="<?php echo $pic_array[$counter]; ?>">, as you can see, but its not working..

 

Edit: $counter from first file is sending number of last line (the empty one) , and not the number of chosen picture. Any suggestions?

 

You need to nest your tags correctly. The form starts inside the td cell and should finish inside.

 

<table  BORDER="1">
<?php for($counter = 0;$counter<$lines; $counter++){ ?>
<tr>
<td><?php echo $pic_array[$counter];?></td>

<td> <form method="POST" action="noname2.php" name="form" id="form">
<input type="hidden" name="path" value="<?php echo $PATH; ?>">
<input type="hidden" name="pic" value="<?php echo $pic_array[$counter]; ?>">
<input type="submit" value="<?php echo $pic_array[$counter];?>" name="PDF"></form></td></tr>

<?php }?>
</table>

 

If you have $lines items you need to loop while < $lines if starting at zero eg

 

for ($counter = 0; $counter < $lines ....

Link to comment
Share on other sites

EDIT: Ah, Barand beat me to it  :happy-04: 

 

 

 

For what it's worth, indenting might help avoid issues like this in the future.

 



<?php
for($counter = 0;$counter<=$lines; $counter++) {
     ?>
     <tr>
     <td><?php echo $pic_array[$counter];?></td>
     <td> <form method="POST" action="2php.php" name="form" id="form">
     <input type="hidden" name="path" value="<?php echo $PATH; ?>">
     <input type="hidden" name="pic" value="<?php echo $pic_array[$counter]; ?>">
     <input type="submit" value="<?php echo $pic_array[$counter];?>" name="PDF"></td></tr>
     <?php
}
?>
</form>

Edited by cyberRobot
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.