PHP + Barcode Generator + PDF Generator

CJones

Final Approach
Joined
Mar 14, 2005
Messages
5,797
Location
Jawjuh
Display Name

Display name:
uHaveNoIdea
I'm creating barcodes on an HTML page using a barcode generator in PHP. I would like to export those barcodes (and some other data) into a PDF.

I can export the other data with no problem using FPDF. When I try to export the barcode (as an image), it doesn't go through. I looked through the barcode generator and it is creating the barcode image using ImageJPEG. To display the barcode in HTML, I simply use an <img src='barcode.php?barcode=[text]&width=500&height=75'>. This displays the image on the webpage with no problem.

When try to put the image into the PDF using $pdf->Image('barcode.php?barcode=[text]&width=500&height=75'), I get an error saying "Invalid image type .php?......"

Digging deeper into the barcode generator, the ImageJPEG is simply outputting the image stream without ever saving the file anywhere. I tried putting a filename in the ImageJPEG call, but I can't find if/where it is saving the images to make a call to the image file itself.

Any ideas on how I can get a dynamically generated image file to export to PDF through PHP?
 
Can you execute

wget http://localhost/barcode.php?barcode=[text]&width=500&height=75 -o barcodefile.img

and use the saved image file in your pdf?
I would avoid that. You'd be relying on local system binaries and you'd be firing off an http request that you don't need.

Ideally you'd take the image resource and just import that into FPDF but it doesn't appear it supports it.. I'd be tempted to extend FPDF to support that...The next best thing would be to just write a temporary file out with the image that FPDF could then use...Like so:
Code:
//generate  temporary file and return the name of that file.
$tempFileName = tempnam("/tmp", "barcode");

//write the image to that file
imagejpeg($imageResource, $tempFileName);

//add that file to the pdf
$pdf->Image($tempFileName);

//remove the temp file.  you'll have to test to figure out where to do this exactly but you don't want to leave the file hanging around.  It's possible php might clean it up for you..
//most likely you'll do this unlink after you run the FPDF method that writes the pdf.
unlink($tempFileName);
The tempname function should generate a unique name that won't collide.

Supporting documentation:
http://php.net/manual/en/function.imagejpeg.php
http://php.net/manual/en/function.tmpfile.php
http://www.fpdf.org/en/doc/image.htm
 
Last edited:
I would avoid that. You'd be relying on local system binaries and you'd be firing off an http request that you don't need.

Ideally you'd take the image resource and just import that into FPDF but it doesn't appear it supports it.. I'd be tempted to extend FPDF to support that...The next best thing would be to just write a temporary file out with the image that FPDF could then use...Like so:
Code:
//generate  temporary file and return the name of that file.
$tempFileName = tempnam("/tmp", "barcode");

//write the image to that file
imagejpeg($imageResource, $tempFileName);

//add that file to the pdf
$pdf->Image($tempFileName);

//remove the temp file.  you'll have to test to figure out where to do this exactly but you don't want to leave the file hanging around.  It's possible php might clean it up for you..
//most likely you'll do this unlink after you run the FPDF method that writes the pdf.
unlink($tempFileName);
The tempname function should generate a unique name that won't collide.

Supporting documentation:
http://php.net/manual/en/function.imagejpeg.php
http://php.net/manual/en/function.tmpfile.php
http://www.fpdf.org/en/doc/image.htm

Looking at this post, I feel like a dog watching a television. Pretty colors, but no comprehension.
Once again, you guys amaze me.
 
I would avoid that. You'd be relying on local system binaries and you'd be firing off an http request that you don't need.

Ideally you'd take the image resource and just import that into FPDF but it doesn't appear it supports it.. I'd be tempted to extend FPDF to support that...The next best thing would be to just write a temporary file out with the image that FPDF could then use...Like so:
Code:
//generate  temporary file and return the name of that file.
$tempFileName = tempnam("/tmp", "barcode");

//write the image to that file
imagejpeg($imageResource, $tempFileName);

//add that file to the pdf
$pdf->Image($tempFileName);

//remove the temp file.  you'll have to test to figure out where to do this exactly but you don't want to leave the file hanging around.  It's possible php might clean it up for you..
//most likely you'll do this unlink after you run the FPDF method that writes the pdf.
unlink($tempFileName);
The tempname function should generate a unique name that won't collide.

Supporting documentation:
http://php.net/manual/en/function.imagejpeg.php
http://php.net/manual/en/function.tmpfile.php
http://www.fpdf.org/en/doc/image.htm

That's what I was trying to do - have ImageJPEG create an actual file, then link directly to a 'real' file for FPDF. Looks like there is some sort of weird timing issue going on - it creates the file, but doesn't find it in time to put it into the PDF. I got tired of messing with it this afternoon. I'll look into it more tomorrow.

If I can get it to put barcodes into a PDF report, it will save me a TON of time when doing some inventory management operations.
 
That's what I was trying to do - have ImageJPEG create an actual file, then link directly to a 'real' file for FPDF. Looks like there is some sort of weird timing issue going on - it creates the file, but doesn't find it in time to put it into the PDF. I got tired of messing with it this afternoon. I'll look into it more tomorrow.
Weird. Shoudln't be a timing issue. I'd double check a path wasn't being botched somewhere.
 
Finally got things working this afternoon.

Ended up changing the barcode.php from a file that had to be called from an <img> tag with querystring variables attached to a class that can be called independently. Changed some of the internals of the barcode 'class' so it would save the image as a file instead of outputting the stream. Had to use ob_start and ob_get_clean to write the file as imagejpeg wouldn't write the file for some reason. Now it creates the image file on the server where it can be linked by a regular <img> tag AND entered into the pdf via fpdf.

Works pretty slick for what I need it for now. It'll come in handy in future web reports I write.
 
Just out of curiosity, is there some reason you couldn't just use a 3 of 9 barcode font? thats by far the easiest way to make barcodes, as long as you start and stop the barcode correctly, it works perfectly and the 3 of 9 is the most widely used barcode out there (next to UPC) - if it's coming from a general puiblic access websit, just embed the font, call it good... would this make the code a little less cumberson? if so, I can supply you the font, it's shareware.
 
Just out of curiosity, is there some reason you couldn't just use a 3 of 9 barcode font? thats by far the easiest way to make barcodes, as long as you start and stop the barcode correctly, it works perfectly and the 3 of 9 is the most widely used barcode out there (next to UPC) - if it's coming from a general puiblic access websit, just embed the font, call it good... would this make the code a little less cumberson? if so, I can supply you the font, it's shareware.
Every system would need the font. It'd be a bad idea to take a shareware font from some guy on the internet and then use it inside your corporation (licensing issues).

You could try to cheat the font problem by doing some of the flash/javascript trickery out there but it's really not worth it. IMO trying to generate a PDF or an image like Chris is doing is the best way.
 
It's not my font, I'd just supply the link. it's free for general use - including inside a corporation. not shareware, freeware, I guess you could say.

by embedding the font in the page, every system that accessed the page and needed it would be prompted to install the font. It's done all the time. (I'm a programmer) especially in situations like this.
 
It's not my font, I'd just supply the link. it's free for general use - including inside a corporation. not shareware, freeware, I guess you could say.

by embedding the font in the page, every system that accessed the page and needed it would be prompted to install the font. It's done all the time. (I'm a programmer) especially in situations like this.

What are the details of the license. Much IP is "free" for personal use but to use it in a commercial application requires a licensing fee, or onerous restrictions on use.
 
It's not my font, I'd just supply the link. it's free for general use - including inside a corporation. not shareware, freeware, I guess you could say.

by embedding the font in the page, every system that accessed the page and needed it would be prompted to install the font. It's done all the time. (I'm a programmer) especially in situations like this.
Ah, well, you said shareware..which is very different from freeware :) That said the term "freeware" doesn't always mean free for any use. You always want to review the actual license.

I'm a programmer as well and it's generally bad practice to build a page that makes a user install a font on their system. You're much better off providing the content in a way that'll be compatible with the user. In a corporate environment you might be able to push the font to all the systems then it's less of an issue.
 
Ah, well, you said shareware..which is very different from freeware :) That said the term "freeware" doesn't always mean free for any use. You always want to review the actual license.

I'm a programmer as well and it's generally bad practice to build a page that makes a user install a font on their system. You're much better off providing the content in a way that'll be compatible with the user. In a corporate environment you might be able to push the font to all the systems then it's less of an issue.

Agreed, but since there's no standard barcode font supplied with any operating system I know, the might be an option to the O/P.

The freeware license that comes with this font is a "free for any use" license, meaning you can use it anywhere you like. I'm using the font in a corporate environment, but contact the make of the font to ensure it was okay. he seemed a little shocked that someone would contact him about it, but was gracious in his answer... "use it for whatever you want, I don't care..." - so, there ya go.

Embedding a font like that was a solution I used in a corporate application. It's been in use for about 7 years now and continues to be the best solution for the problem. While asking users to install a font isn't always the best solution, it makes for much simpler programming on our end. just a though. :D
 
Like it, use it, great. Don't like it, don't use it, great... I'm not the license police. Just wanted to offer another option to the OP other than having to make in image..

Work smart, not hard, right?
 
Actually, I looked into simply using a font when I first got the idea of this app.

This is used in a corporate environment and unfortunately I can't be sure that the end users will be have security access to install fonts or not (yes, I know it is crazy) not to mention the fact that the end user may not have the technical know-how to be able to do it (yes, I know it's crazy). That's why I ended up using a barcode generator instead.

Now that I have it built as a class, I can call it any time I want, pass it a filename I want it to use, and link back to that image file just like any other .jpeg.
 
Back
Top