2

I use FPDF to create a PDF from database data. I need to put an image at the end but it is unsupported.
I get image from a php file that generate the image from a code in this way:

http://server/ean13.php?code=1234567890123

Usually I put this URL in <img> tag but it doesn’t work with FPDF.

This is the code:

$pdf = new PDF(); //PDF Class extends FPDF
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->Cell(0,10,'Test line',0,1);
$pdf->Image('ean13.php?code=1234567890123',10,6,30); //not work
$pdf->Output();

In ean13.php the image is generated by imagepng() function to display the barcode.

I've also tried with file_get_contents but is the same.

Just to add an other example, if in a page I put <img src="ean13.php?code=1234567890123" /> it works

Mate Mate
  • 31
  • 1
  • 5
  • Hi @MateMate, you should post your code where you try to insert the image, otherwise we won't be able to help you. (see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask)) – AymDev Aug 24 '18 at 10:00
  • You could see if your server allows to remotely load the file with file_get_contents or cURL first and then work with it. – Chris S. Aug 24 '18 at 10:26
  • Here's the same problem for TCPDF which is basically another FPDF fork https://stackoverflow.com/questions/2687352/tcpdf-remote-image-loading-problem – Chris S. Aug 24 '18 at 10:26
  • From your post edit (code added) I see that you entirely wrongly access the php file. It's not executed if you call it locally that way. You either need to include it and store the image for later use or use the external path to said php file. – Chris S. Aug 24 '18 at 10:30

3 Answers3

1

You are using a relative url which doesn't mean anything in the context of the server where your script is running.

Relative urls work in the browser, because they have actually loaded the current page using some url, like http://example.com/some/path/index.php. Because, of that, when you give the image a relative url, like ean13.php, it actually knows it should invoke http://example.com/some/path/ean13.php on the server.

But the script on the server doesn't have that context. It will just try to load that string as a file, without actually executing the code in ean13.php.

So, the solution: use a full url, so FPDF can do a request to get it. Or generate the image and store it in a local file, then pass that filename to FPDF.

As far as I know (or can tell by the documentation), there is unfortunately no way to load an image from a stream, string or other in-memory representation of the image. It's either a file or a (full) URL.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • I’ve tried different solution like using imagepng(data, filename) or salve the image, but all of these don’t work – Mate Mate Aug 30 '18 at 12:56
  • I'm sorry to hear that, but that's not enough context for me to help you further. Maybe you can update your question and describe (+code) the new things you tried so I and others can have a look at them. – GolezTrol Aug 30 '18 at 14:32
  • I’ve found an extention for FPDF for generating EAN13 barcode so at the moment the problem is solved – Mate Mate Aug 30 '18 at 19:40
1

I had the same problem juste add ?ext=.png at the end of url ! like :

$pdf->Image($logo_path.'?ext=.png', 8, $this->current_y);
Arpit Jain
  • 1,599
  • 9
  • 23
0

I had the same problem and even with a full url it was not working. I solved it by generating the image in the same file and saving the output in a variable.

Then I used the extension from this link: http://www.fpdf.org/en/script/script45.php to show the image from the variable.

soliver
  • 319
  • 4
  • 10