?
Solved

I've got to create Avery labels from a PHP platform. How?

Posted on 2014-09-11
4
Medium Priority
?
800 Views
Last Modified: 2014-09-11
I know there's more than one way to do this, but I figured before I just start throwing things against the wall and seeing what sticks, I would ask.

I've got to create a dynamic where a user can print a round of Avery 5160 labels from a mysql database - just loop through all the records in a particular table - and I also want to provide a situation where they can manually dictate what would show up on a single page.

What would you recommend?
0
Comment
Question by:brucegust
  • 2
4 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1600 total points
ID: 40316841
FPDF or TCPDF.  Both are free and open-source.  Here's my "hello world" example with FPDF.  These are somewaht time-consuming at first, but once you have the document layout correct, they are dependable and lightning fast.
<?php // demo/fpdf_hello_world.php
error_reporting(E_ALL);


// DEMONSTRATE SOME OF THE BASICS OF FPDF


// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$font = 'Arial';
$text = 'Hello World!';

// BRING IN THE PDF THING
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(  0,   0, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Blue PDF</a>';



// START OVER FOR A NEW PDF
unset($pdf);
echo PHP_EOL . "<br/>";

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'storage'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN BLACK ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 24);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(  2,   2,   2);
$pdf->Cell(52, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Red PDF</a>';

Open in new window

0
 

Author Comment

by:brucegust
ID: 40316897
Thank you, Ray!

I had seen this (fpdf) and downloaded the file, but I'm glad to be getting some "validation" from someone like yourself.

BTW: Ordered the PHP book you recommended (http://www.amazon.com/Objects-Patterns-Practice-Matt-Zandstra/dp/1430260319/ref=sr_1_7?ie=UTF8&qid=1410441068&sr=8-7&keywords=PHP). Poised on the threshold of greatness, I am...
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40316912
Great!

Sidebar note about FPDF.  I believe it allows positioning to within about 1/100 inch.  I've used it for postcards, business cards, labels and all kinds of stuff.  Really useful library!
0
 
LVL 36

Assisted Solution

by:gr8gonzo
gr8gonzo earned 400 total points
ID: 40316919
I've had to do this exact thing before (use PHP to generate Avery labels). I actually ended up using HTML <table> instead of PDF. If you're going to do the actual printing on different machines or on different browsers than the ones where the generation takes place, PDF is a better bet. Otherwise, HTML works just fine.

Basically, I used the Word template from Avery to print out the label borders onto a blank sheet of paper. Then I set up an HTML page with a table (border=0), and set the background of the cells to a light-grey and use CSS to define a specific width and height of the cells (each cell being a label), and had PHP print out some sample data in each one.

Use print preview to get a "guesstimate" of what it should look like (Chrome's preview worked really well for this), and when you get close to the desired result, just print out a test sheet on regular paper and hold the two printouts together against the light to see if the borders match up. It may take a little bit of tweaking and some changing of the browser's print settings (print margins, mostly), but you should be able to get it to a point where the browser is printing out exactly the layout that you want.

At that point, you just fill in the data and print.

Last point - double-check your data to see if you have anything that will be extra long (data length) in case you need to lower the font size when the data gets to a certain length.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question