Solved

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

Posted on 2014-09-11
4
417 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 108

Accepted Solution

by:
Ray Paseur earned 400 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 108

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 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 100 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now