Solved

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

Posted on 2014-09-11
4
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 110

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 110

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 35

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

724 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