Creating Shipping Labels for Magento Orders

This is a small modification to magento that creates shipping labels in pdf. This code was designed to generate labels for the Dymo LabelWriter 400 using the 99014 (101mmx54mm) labels, however the size of the paper can be changed easily.

  • Open the file:

    /app/design/adminhtml/default/default/template/sales/order/view/info.phtml

  • Around line 171, after<h4>

    <?php echo Mage::helper(‘sales’)->__(‘Shipping Address’) ?></h4>

  • put the following code:
    <span style="float:right;margin-left: 0.5em;"><a href='#' style="color:#FFCC22"  onclick="labelPrintLandscape()">Landscape</a></span>
    <span style="float:right;margin-left: 0.5em;"><a href='#' style="color:#FFCC22"  onclick="labelPrintPortrait()">Portrait</a></span>
    <span style="float:right;margin-left: 0.5em;"><b>Print Label:</b></span>
  • Place at the bottom of the file, the following code:

    <script type="text/javascript">
    <?php
    $shipaddr= trim($_order->getShippingAddress()->getFormated(true));
    $splitx=explode("\n",$shipaddr);
    $end = "";
    
    $end = Mage::getBaseUrl();
        echo "\n".'var end="'.$end.'"; '."\n" ;
        echo"\n".'var shipx="'.urlencode($shipaddr).'"; '."\n" ;
    ?>
    
    var popUpWin;
    
    function labelPrintLandscape() {
       var url=end.replace("index.php/admin/","").replace("index.php/","");
       url=url +"custom/print_label_landscape.php?addr="+shipx;
       popUpWin = open(url,'popUpWin','toolbar=yes,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,copyhistory=yes, width=500,height=300,left=100,top=100,screenX=100,screenY=100');
    }
    
    function labelPrintPortrait() {
        var url=end.replace("index.php/admin/","").replace("index.php/","");
        url=url +"custom/print_label_portrait.php?addr="+shipx;
        popUpWin = open(url,'popUpWin','toolbar=yes,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,copyhistory=yes, width=500,height=300,left=100,top=100,screenX=100,screenY=100');
    }
    </script>
  • Create the directories /custom and /custom/dompf on the site root folder.
  • Download the dompdf library here and extract its contents to /custom/dompf.
  • Create the file /custom/print_label_portrait.php and with the following content:
    <?php
    $html ='<html<head></head><body>';
    
    $url = str_replace("addr=","",$_SERVER["QUERY_STRING"]);
    $decoded_url = urldecode($url);
    
    //REMOVE PHONE NUMBER
    //$decoded_url = explode("T:",$decoded_url); $decoded_url = $decoded_url[0];
    
    $html = utf8_decode(str_replace("T:","",urldecode($decoded_url)));
    
    $html.="</body></html>";
    
    require_once("dompdf/dompdf_config.inc.php");
    
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $paper = array(0,0,152.82,285.83);
    $dompdf->set_paper($paper , 'portrait');
    $dompdf->render();
    $dompdf->stream("label_landscape.pdf");
    ?>
  • Create the file /custom/print_label_landscape.php and with the following content:
    <?php
    $html ='<html<head></head><body>';
    
    $url = str_replace("addr=","",$_SERVER["QUERY_STRING"]);
    $decoded_url = urldecode($url);
    
    //REMOVE PHONE NUMBER
    //$decoded_url = explode("T:",$decoded_url); $decoded_url = $decoded_url[0];
    
    $html = utf8_decode(str_replace("T:","
    ",urldecode($decoded_url)));
    
    $html.="</body></html>";
    
    require_once("dompdf/dompdf_config.inc.php");
    
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $paper = array(0,0,152.82,285.83);
    $dompdf->set_paper($paper , 'landscape');
    $dompdf->render();
    $dompdf->stream("label_landscape.pdf");
    ?>
  • In the administration page, go to a order page and you should see two links in the address box header, clicking in one of the should generate the label.

Removing the phone number

On the files:

  • /custom/print_label_landscape.php
  • /custom/print_label_portrait.php

replace:

//$decoded_url = explode("T:",$decoded_url); $decoded_url = $decoded_url[0];

with:

$decoded_url = explode("T:",$decoded_url); $decoded_url = $decoded_url[0];

Setting the paper size

On the files

  • /custom/print_label_landscape.php
  • /custom/print_label_portrait.php

replace the last two numbers of the following line:

$paper = array(0,0,150.00,280.00);

with:

(measure in mm) X 2.83

Example: for a label of 150mmx180mm it should be:

  • 150×2.83=424.50
  • 180×2.83=509.40
$paper = array(0,0,509.40,424.50);

Tips

  • You can check some standard measures in here.
  • Since this generates a pdf you don’t need to print the label to see if the size is correct or not, you can check the paper size in the file proprieties of your pdf viewer that usually is in File→Proprieties

Sources

Share
  1. Laurent Briere says:

    Hi, good idea but a little question how to define the css for text, for that style in the file “print_label_landscape.php” does not work. At you a solution thank you

    • Marco Constâncio says:

      I notice that I had a few errors and corrected them. As for the css, the library that generates the pdf file (dompdf) seems to have a few bugs and ignore certain css, but since the size of the font is ideal for labels that I use, I never really noticed. But if you want to change the font size you might want to check other pdf libraries like fpdf, tcpdf or ufpdf.

      • Laurent Briere says:

        Ok I found I did the update to 0.6 beta DOMPDF and it works well as the css file. killed an idea to add the sender address (the shop). Or custom html page that generates the PDF.
        Thank you to you

  2. Peter says:

    Hi, the phone numbers is removed in both my files but in the pdf it still shows.

    • Marco Constâncio says:

      Sorry, I made a mistake in the tip to remove the phone number. You have to uncomment that line and remove the slashes.

  3. Peter says:

    Hi Marco,

    Thanks for this, it works great.

  4. Nauman says:

    Hi

    Very nice and informative tutorial, i have a question though. On my local machine, this code works fine, whereas on my live server, when i click on top menu to generate pdf, it gives a 403 error. If i remove special characters from query string, then it works fine.

    it seems that special characters are causing issues. can u please guide, how to overcome this issue.

    cheers

    Nauman

  5. Jagesh says:

    Hi,
    I am working in magento.
    do u know from which file the shipment mail sending,I want to modify the sender name of shipment mail instead of system->configuration settings.

    if possible can you tell me the file path to modify the sender name of shipment mail.

  6. mrdaniels says:

    Hi.

    This is working perfect! Thanks for sharing. Easy to implement as well.

    Is it posible to change the font-size of the label? And design the label in generel?

    I would also like to know if it is posible to add the ordernumber in the label?

    Thank you!

    Regards,
    Daniel

    • Marco Constâncio says:

      For changing the font size you problably going to add the css style tag in the $html var in the 2 php files, but I never tested so I’m not sure. I suggest that check the documentation and forums of the dompdf library to check of how to change specific details of the label. As for the order number try replacing $splitx=explode(“\n”,$shipaddr); with $splitx=explode(“\n”,$shipaddr.”\n”.$_order->getIncrementId()); and the order should be placed in last part of the label.

  7. Ozan says:

    Hi,

    I couldn’t get it to work. Then I realized that you said to create the folder /custom/dompf but shouldn’t it be? /custom/dompdf? So I renamed it but it still doesn’t work. I get the error 500. Thanks.

Leave a Reply