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

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?
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brucegustPHP DeveloperAuthor Commented:
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
Ray PaseurCommented:
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
gr8gonzoConsultantCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.