Barcode, Salesforce and PDF

Wednesday, February 16, 2011 by Aslam - The Alexendra
Hi All,
Recently I faced one issue where i need to generate Barcode on the fly in VF and also render that as PDF. I searched on Net and community, found some people have done Barcode generation using Javascript. But in my case i need to render it as PDF and VF does not support Javascript when renderAs PDF. So i decided to go the way to generate Barcode image. The first thing came in my mind is PHP/GD way to generate Barcode image and use that url in VF to show image. Then i found this site useful and very useful PHP script which fits my need
http://www.sid6581.net/cs/php-scripts/barcode/

I used this script, done some modification in this and hosted on my site And used in my VF page as below:-

<apex:image value="http://www.aslambari.com/barcode.php?barcode=CS1001&width=200&text=1&height=100" />


Here is some details about how you use the url and parameters to generate barcode:
URL: http://www.aslambari.com/barcode.php
Params:
barcode: [any text to render as barcode]
text: [0 or 1]
format: [jpeg/png/gif]
width: [the width you want]
height: [the height you want]



It works great if my VF render as normal HTML page, but as soon as i make it renderAs="PDF", VF again not works, it does not allow dynamic generated images. So again i got disappointed.
Then i thought again and did apex code to fetch the dynamic binary data generated my PHP site url and save it into attachment and use that url in my VF page. I was thinking this time i will win for sure, but again it did not work. HttpResponse does not allow Blob response, it always gives response as String and that mingled my image data. Disappointed again.

Then finally i have decided to create dynamic generated barcode on PHP and save as temprary images and use that url in my img tag and renderAs="PDF", this time i got success :)

Here is the working demo for my work:-
https://labsprojects-developer-edition.ap1.force.com/BarCodeSample

If you need complete code for above demo site, here is the link:
http://www.aslambari.com/SF_Barcode.zip

Enjoy barcoding :)

Thanks
Aslam Bari

30 comments:

Shaikh Mustakim said...

Great......

Anonymous said...

Again great stuff from your side...
This is really great

Anonymous said...

Thanks Aslam ji, this is really a gr8 solution, i also like to discuss it in more detail.
:)

Previously we limited our apporach and suggesting the client to print the webpage itself, due to unsupportiveness of renderas pdf by salesforce. But now i think once we can generate barcode as image for a given code this can be simply used in pdf.

Thanks again!
- Deepak

Lokesh Agarwal said...

Great post !
You r on roll!! Keeping the pace to break the limits..

Anonymous said...

Its a gr8 work around.

Anonymous said...

This is amazing really very good feature, you rock Aslam as always

Nandini

Mustafa said...

Hi Aslam,

Great post, i must say. There is a little problem here hope you can help. I have done just as you have suggested but on my own server. Here images get generated on my server and render well on vf page.

But when i try renderas="pdf" or getContentAsPDF() the images don't render. Funny party is images from your server render well on vf and pdf page. Its just my server that's causing some problem. Take a look at this image and please suggest a workaround:

http://www.beyondvision.biz/karim/aslam.jpg

Thanks and happy coding
Mustafa

Aslam - The Alexendra said...

@Mustafa: I think you must add your website url into Remote Site Setting. I can think only that problem.

Mustafa Turab Ali said...

Aslam,

I have done that but still it's not working. Any other idea?

Mustafa

kanupriya said...

thanks aslam... for this... bt i have an another query ... can you pls solve dat... i want to integrate barcode scanner with salesforce... i hope you have an idea for this...


Thankssss

Anonymous said...

Thank you for the idea. It's great!

Anonymous said...

I'm running into the same issue where dynamic images are not rendering when using renderAs="pdf". I've tried controller extentions and custom components to no avail. I urgently need to resolve this issue.

I've reviewed your demo and your PDF version doesn't seem to be rendering correctly? I've also noticed that the link to your demo code is no longer valid.

Thanks.

Aslam - The Alexendra said...

The url
http://www.aslambari.com/barcode.php
is working fine?
Have you added this in remote site setting as well (just to make sure that does not affect issue)

Anonymous said...

Hi Aslam,
I am not able to open http://www.aslambari.com/SF_Barcode.zip

Can you please check?
I need this code. It's urgent.
Can you please provide me on my mail npmane007@gmail.com ?

Anonymous said...

It seems you demo didn't work ok...could you please check it?

Anonymous said...

Hi Aslam,

Can you send me the original code to me david.lee@meginfo.com?

Aslam - The Alexendra said...

Hi David,
It seems i have removed the original code while maintanance of my external FTP site. I will send you once i found the original code. But if you try the steps given in post, you can do it yourself.

Vasu said...

i want genarate barcode dynamically

Anonymous said...

can i autimatically save the barcode i generated using this script(http://www.sid6581.net/cs/php-scripts/barcode/barcode.phps)?

teach me master

Anonymous said...

Good work! Can you please send me code for the same on sumi.shingavi@gmail.com

Anonymous said...

hi Aslam, I am looking for barcode reader which can populate data in salesforce. Is there any such APU / java script available?

shruti said...

Really a wonder post and helpfull as well..
But i am having issues in showing barcode on Pdf. will you please send me the full code, as the Zipped code base is not available at the link.
My email address is vasishta.shruti@gmail.com

Thnx again ..

Anonymous said...

Thanks for putting this together -- any chance you can re-post the ZIP or email it to me?

jeff.may@misstheiceberg.com

Thanks!

Unknown said...

Really a wonder post and helpfull as well..
But i am having issues in showing barcode on Pdf. will you please send me the full code, as the Zipped code base is not available at the link.
My email address is mahesh060708@gmail.com

Anonymous said...

A great post.......hi Aslam,can you please send me the code @ sanjeevsh15@gmail.com

Unknown said...

great stuff..!!!

Can you please send the code to avirajten@gmail.com

Thanx

Anonymous said...

Hi Aslam,

I am also stuck with the renderAs and inserting it as an attachment. The PDF is showing blank. Can you please send me this code @ gopal026@gmail.com?

Thanks in advance.

Anonymous said...

Can i also have the code please? @ f.f2017@yahoo.com

Venkat Pattapu said...

Hi there is no code,i'm also having similar type of reqiurement ,please send code to email
venkatp187@gmail.com

Text said...

can you please share the source to spprathap2003@gmail.com

Post a Comment