Juliette Posted March 6, 2013 Share Posted March 6, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/ Share on other sites More sharing options...
Juliette Posted March 7, 2013 Author Share Posted March 7, 2013 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417166 Share on other sites More sharing options...
Barand Posted March 7, 2013 Share Posted March 7, 2013 $pic = $_POST["PDF"]; "PDF" is the name of your submit button! Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417210 Share on other sites More sharing options...
cyberRobot Posted March 7, 2013 Share Posted March 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417213 Share on other sites More sharing options...
Barand Posted March 7, 2013 Share Posted March 7, 2013 Also, your image names have "\r\n" at the end - if you var_dump() you'll see that "pic1.jpg" is 10 characters. Use $pic_array[] = trim(fgets($handle, 4096)); Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417219 Share on other sites More sharing options...
Juliette Posted March 7, 2013 Author Share Posted March 7, 2013 (edited) 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 March 7, 2013 by Juliette Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417263 Share on other sites More sharing options...
cyberRobot Posted March 7, 2013 Share Posted March 7, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417289 Share on other sites More sharing options...
Solution Barand Posted March 7, 2013 Solution Share Posted March 7, 2013 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 .... Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417291 Share on other sites More sharing options...
cyberRobot Posted March 7, 2013 Share Posted March 7, 2013 (edited) EDIT: Ah, Barand beat me to it 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 March 7, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417293 Share on other sites More sharing options...
Juliette Posted March 7, 2013 Author Share Posted March 7, 2013 (edited) Its working again. Tnx guys Edited March 7, 2013 by Juliette Quote Link to comment https://forums.phpfreaks.com/topic/275337-problem-with-fpdf-and-variables/#findComment-1417310 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.