Call-back icon  Enterprise Sales: +1.310.775.2674 (N. America)   +44 20.3286.4137 (UK)

Magento

eCommerce Software for Online Growth

Magento Forum

   
How to print barcodes in Invoice.PDF
 
Mad Martigan
Member
 
Total Posts:  44
Joined:  2008-08-17
Medford, OR USA
 

I needed to have a barcode print at the top of every packing slip, invoice and some other documents. I had to do some digging to find the answer on how to do this so I thought I would post what I found.

Beware, I’m not a PHP programmer and I’m VERY new to Magento so this could all be way off base.

To print the order number on the invoice....

1) Download a free true type font.  (attached to this post as a zip file)

2) Put the font somewhere that your web server can read it.

3) Modify app/code/core/Mage/Sales/Model/Order/PDF/Invoice.php, and put this code in there…

/* SITE MODIFICATION */
            /* Define a font path that is readable by the web server */
            $fontPath '/var/www/barcode-fonts/FRE3OF9X.TTF';
            
            
/* Load the font */
            
$page->setFont(Zend_Pdf_Font::fontWithPath($fontPath), 36);
            
            
/* Create barcode string. */
            /* Note: Asterisks are required for code39 */
            
$barcodeImage "*".$order->getRealOrderId()."*";
            
            
/* Insert the barcode into the page */
            
$page->drawText($barcodeImage400800);

I put this on line 46 (Magento 1.1.3).  Basically, make sure its contained int he foreach loop.  I put it just above the /* Add image */

Done!

I found a lot of fonts on the web but most did not work.  I finally found a code39 font that worked just swell.  It is attached to this post as a zip file.  The font file name that I use is FRE3OF9X.TTF.  I created a directory that my apache server could read and I put the file in there, making sure it was readable by the web server.

I hope this help.

File Attachments
free3of9.zip  (File Size: 5KB - Downloads: 54)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Mad Martigan
Member
 
Total Posts:  44
Joined:  2008-08-17
Medford, OR USA
 

Adding Barcodes to a PDF file (revised a little)

After posing that first message I got to poking around and figured out how to separate my local changes from the Magento core.  Here is what I did....

We are going to override/overload a module so that we do not have to hack on the core Magento code.  This way, upgrades will not affect our changes.  To do this you basically need to create the directory structure under your local directory to mirror that of the Magento code.  Then, create a few xml files to let Magento know it’s there. Finally, copy over the Shipment.php file to your local directory for modification.

The basic steps to achieve this are…
1) Create the directory structure
2) Create xml files
3) copy the php file and edit it.

*** Make sure your cache settings are disabled in Magento so you can see your changes ***

The file we want to copy and edit is {your_magento_install_dir}/app/code/core/Mage/Sales/Model/Order/Pdf/Shipment.php

So lets do it (commands are for unix)

cd {your_magento_install_dir}/app/code
mkdir 
-p local/Local/Sales/Model/Order/Pdf
mkdir 
-p local/Local/Sales/etc
cp core
/Mage/Sales/Model/Order/Pdf/Shipment.php local/Local/Sales/Model/Order/Pdf/Shipment.php

Now edit the new local Shipment.php file and put the barcode stuff in there. (see instructions in previous post)

Important: Change the class decloration from this....

class Mage_Sales_Model_Order_Pdf_Shipment extends Mage_Sales_Model_Order_Pdf_Abstract

To this…

class Local_Sales_Model_Order_Pdf_Shipment extends Mage_Sales_Model_Order_Pdf_Abstract

Then, create config.xml in the Sales/etc directory and put this in it…

<?xml version="1.0"?>
<config>
    <global>
        <
models>
            <
shipmentpdf>
                <
rewrite>
                    <
pdf_shipment>Local_Sales_Model_Order_Pdf_Shipment</pdf_shipment>
                </
rewrite>
            </
shipmentpdf>
        </
models>
    </global>
</
config>

Then, create this file {your_magento_install_dir}app/etc/modules/Local_All.xml and put this in it…

<?xml version="1.0"?>
<config>
    <
modules>
        <
Local_Sales>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Local_Sales>
    </
modules>
</
config>

After you make some changes (and turn off caching) you should see your changes when you generate a packing slip.

Enjoy!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Priest
Jr. Member
 
Total Posts:  20
Joined:  2008-04-21
 

I am having a problem getting this to work correctly.  It does work, if I follow directions in the first post - but I’d prefer not to hack into the core magento files.  However, when I follow the second revised directions, it does not work for me.  I have triple-checked that everything is setup and edited as you suggest.  I am using Magento v1.1.3 and verified that my cache is disabled.

I’ve been able to track down the problem to this:  Magento is still trying to use the original Shipment.php file and not the one that’s been edited and relocated.  (I have made sure that both xml files are created and in the correct folders.) Is there something else I should be doing besides what you have posted?

Thanks!
Priest

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mad Martigan
Member
 
Total Posts:  44
Joined:  2008-08-17
Medford, OR USA
 

Hmm, not sure what is going on.  I checked my setup and it matches the instructions.  It did take me hours to get it working because I am still new to the way Magento handles layouts and name-spaces.  So my guess is that it’s a typo or uppercase-lowercase thing.  If not that then I’d look at directory permissions.

While testing I did tail the Magento error log and sometimes that helped. I would also put in typos into the xml files just to see if Magento would error out and that told me whether or not the file was even being accessed by Magento.  I started with the Local_All.xml and then the config.xml and finally the php file.

You might try that and see if it helps. 

Marty

 
Magento Community Magento Community
Magento Community
Magento Community
 
mjohnsonperl
Member
 
Avatar
Total Posts:  34
Joined:  2008-02-28
Carthage, MO
 

Here is a wiki article I created that lists a download link for the files I used to add barcodes to invoices, shipments, and credit memos:
http://www.magentocommerce.com/wiki/groups/207/invoice-shipment_barcodes

I’m using a TTF font that I’ve setup Zend_PDF to reference in the PDFs and wrote a little bit of code to convert alphanumeric values into the correct CODE 128 values for use with the barcode font. The use of CODE 128 means that there are start and stop characters to the barcode and a check digit so your barcode scanner will not accidentally get a partial read of the barcode.

The TTF font that I used came from:
http://freebarcodefonts.dobsonsw.com/

I referenced the documentation on that site in order to build the PHP code necessary to calculate the checksums and convert the string correctly for the font.
http://freebarcodefonts.dobsonsw.com/Code128Transformation.htm

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mad Martigan
Member
 
Total Posts:  44
Joined:  2008-08-17
Medford, OR USA
 

Hi mjohnsonperl,

Thanks for posting those links.  Excellent information!  I would rather use code128 anyway but I could not find a good free TTF for code 128.  So you’re post is perfect for my needs. 

Thanks again for sharing the info.

Marty

 
Magento Community Magento Community
Magento Community
Magento Community
 
Luke Berg
Jr. Member
 
Total Posts:  6
Joined:  2008-09-30
 

I was trying to do a similar thing, but I wanted to modify the Invoice pdf, so I needed to use my own Invoice.php.

I followed the steps above, but it didn’t seem to work.. after some research, I discovered the problem.  Steps 1 and 3 are correct,
but the second step seemed to be wrong (for magento 1.2.01)

Rather than,

<?xml version="1.0"?>
<config>
    <global>
        <
models>
            <
shipmentpdf>
                <
rewrite>
                    <
pdf_shipment>Local_Sales_Model_Order_Pdf_Shipment</pdf_shipment>
                </
rewrite>
            </
shipmentpdf>
        </
models>
    </global>
</
config>

I had to use this

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <global>
        <
models>
            <
sales>
                <
rewrite>
                    <
order_pdf_shipment>Local_Sales_Model_Order_Pdf_Shipment</order_pdf_shipment>
                </
rewrite>
            </
sales>
        </
models>                    
    </global>    
</
config>

That should make it work. 

*For me the code above had shipment replaced with invoice

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2009 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
108780 users|330 users currently online|199897 forum posts