Solved

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

Posted on 2014-09-11
4
484 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 109

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 109

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php construct 5 29
Why do people dis php? 5 54
php extract($_REQUEST) 5 56
PHP PDO   How do I correctly remove record using DELETE FROM? 1 21
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

825 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