PHP Classes

PHP Print Labels to PDF: Generate PDF documents with labels from HTML tags

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog (2)    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 78%Total: 336 This week: 2All time: 7,099 This week: 96Up
Version License PHP version Categories
printable_labels_pdf 1.0.1Custom (specified...5HTML, PHP 5, Files and Folders, Printing, B...
Description 

Author

This package can generate PDF documents with labels from HTML tags.

It can take configuration parameters to define pages and labels that appear in the pages to be generated as a PDF file with personalized labels.

The package takes HTML tags that define the content and formatting for each label that will appear in the generated PDF document pages.

Innovation Award
PHP Programming Innovation award winner
August 2021
Winner


Prize: One official elePHPant Plush Mascott
Sometimes it is necessary to generate labels to be printed and stamped in other objects like boxes, badges, or folders.

You can generate a label document in a word processing program and then send the label to a printer.

However, when you need to generate many labels with different text, for instance, to show the names of different people, it is a lot of work to create those label documents manually.

This package provides a more automated solution that can generate personalized labels in great quantity in less time.

It generates a PDF document with the spaces for each label. Then an application defines the content of each label using HTML.

Manuel Lemos
Picture of Rafael Martin Soto
  Performance   Level  
Name: Rafael Martin Soto <contact>
Classes: 14 packages by
Country: Spain Spain
Age: 49
All time rank: 221753 in Spain Spain
Week rank: 6 Up2 in Spain Spain Up
Innovation award
Innovation award
Nominee: 8x

Winner: 4x

Example

<?php
/** example of use of printable_labels_pdf_class
 *
 * @author Rafael Martin Soto
 * @author {@link http://www.inatica.com/ Inatica}
 * @blog {@link https://rafamartin10.blogspot.com/ Rafa Martin's blog}
 * @since August 2021
 * @version 1.0.0
 * @license GNU General Public License v3.0
*/

require_once __DIR__ . '/printable_labels_pdf_class.php';



$labels_config = [];

$labels_config['page_format'] = 'A4'; // Page format
$labels_config['page_orientation'] = 'P'; // Orientation 'L'andscape 'P'ortrait

$labels_config['width_label'] = 8.89;
$labels_config['height_label'] = 2.33;

$labels_config['num_cols'] = 2;
$labels_config['num_rows'] = 12;

$labels_config['margin_left_page'] = 1.3;
$labels_config['margin_top_page'] = .2;

$labels_config['margin_left_label'] = .2;
$labels_config['margin_bottom_label'] = .2;

$labels_config['padding_left_label'] = .25;
$labels_config['padding_top_label'] = .25;

$labels_config['skip_first_row'] = true;
$labels_config['skip_last_row'] = true;

$labels_config['default_font'] = 'Times';
$labels_config['default_font_size'] = 9;

$labels_config['begin_at_label_num'] = 4; // Defaults begin with label num. 1


// Create an Instance of printable_labels_pdf()
$printable_labels_pdf = new printable_labels_pdf( $labels_config );

// Set draw borders to true
$printable_labels_pdf->draw_border( true );

// Generate 2.499 Labels
for( $i=1; $i<2500; $i++){
   
   
// Make a string of the html label
   
$label_html = '<b>label '.$i.'</b><br />'; // 1st row. Bold
   
$label_html .= '<i>Line 2</i><br />'; // 2nd row. Italic
   
$label_html .= '<u>Line 3</u><br />'; // 3th row. Underline
   
$label_html .= 'Line 4'; // 4th row. Standard text
   
    // send the html string to a new label
   
$printable_labels_pdf->write_label( $label_html );
}

// Generate Pdf file
$printable_labels_pdf->get_labels_pdf(); // Output a PDF file directly to the browser

/*

a) Show directly in the browser (default)

$printable_labels_pdf->get_labels_pdf('test.pdf', 'I'); // Output a PDF file directly to the browser



b) Get a String of the pdf to do something with it later:

$SomeVarPdfString = $printable_labels_pdf->get_labels_pdf('test.pdf', 'S'); // Get pdf in string format and assign to $SomeVarPdfString



c) Download
$printable_labels_pdf->get_labels_pdf('test.pdf', 'D'); // Download pdf



d) Save pdf file in server path
$printable_labels_pdf->get_labels_pdf('/some_dir/test.pdf', 'F'); // Save pdf in some dir of the server

*/
?>


Details

printable_labels_pdf

Generate pdf file with printable labels with PHP code.

## CREATE A PDF FILE WITH LABELS EASELY: You can get a pdf file with labels for print. It can set easely with any type of labels widths ang heights, papers, etc... You can draw a single border if you need it. You can get the pdf file generated into a browser, download, get a string of the pdf or save it to the server. 100% written in PHP (pure PHP). Mpdf library required.

SCREENSHOTS:

Screenshot of the pdf labels created in Pure PHP

Test with VirtualBox Ubuntu Server 20.04.2 LTS (Focal Fossa) with PHP7.4.3 in 18 seconds.

Screenshot of the pdf labels created in Pure PHP

Content of the pdf created.

You can see a test result here:

https://github.com/vivesweb/printable_labels_pdf/blob/main/pritable_labels_pdf.pdf

# REQUERIMENTS:

- A minimum (minimum, minimum, minimum requeriments is needed). Tested on:

- Simple Raspberry pi (B +	512MB	700 MHz ARM11) with Raspbian Lite PHP7.3 (i love this gadgets)  :heart_eyes:

- VirtualBox Ubuntu Server 20.04.2 LTS (Focal Fossa) with PHP7.4.3

- Red Hat Enterprise Linux Server release 6.10 (Santiago) PHP Version 7.3.25 (Production Server) 512Mb Memory Limit

- Red Hat Enterprise Linux release 8.4 (Ootpa). PHP Version 8.0.11 (Production Server) 512Mb Memory Limit

  • MPDF LIBRARY NEEDED TO BY INSTALLED: https://mpdf.github.io/
  • Ensure to add correct path at line 106 in the file _printable_labels_pdf_class.php_:

    require_once __DIR__ . '/mpdf/vendor/autoload.php';

FILES:

printable_labels_pdf_class.php -> Main File.

example.php -> Example File.

pritable_labels_pdf.pdf -> Example pdf with labels File created.

test_pdf_labels.png -> Screenshot of a pdf file created.

test_pdf_labels_2.png -> Screenshot of the content data inside the pdf (with benchmark).

# INSTALLATION: A lot of easy :smiley:. It is written in PURE PHP. Only need to include the files. Tested on basic PHP installation

     require_once( 'printable_labels_pdf_class.php' );
     
  • You need to install mpdf library. Remember to set writable mpdf/vendor/mpdf/mpdf/tmp. See https://mpdf.github.io/ documentation
  • If you change the mpdf dir installation, make sure mpdf library is included in printable_labels_pdf_class.php at line 106 in printable_labels_pdf_class.php:

    require_once __DIR__ . '/mpdf/vendor/autoload.php';

RESUME OF METHODS:

  • CREATE PRINTABLE LABELS PDF OBJECT:

$printable_labels_pdf = new printable_labels_pdf( $labels_config );

Example:

    $printable_labels_pdf = new printable_labels_pdf( $labels_config );

  • $labels_config CONFIGURATION VALUES:

ALL MEASURES ARE IN CM.

  • _width_label_: Width of each label (in cm.).
  • _height_label_: Height of each label (in cm.).
  • _num_rows_: Num of rows in the page.
  • _num_cols_: Num of cols in the page. If not set, 2 cols by default.
  • _margin_left_page_: Margin at left of page before begin first col of labels (in cm.). If not set, 0 by default.
  • _margin_top_page_: Margin at top of page before begin first row of labels (in cm.). If not set, 0 by default.
  • _margin_left_label_: margin between labels at left (in cm.). If not set, 0 by default.
  • _margin_bottom_label_: margin between labels at bottom (in cm.). If not set, 0 by default.
  • _padding_left_label_: space between border of label and html content inside the label, beginning at left (in cm.). If not set, 0 by default.
  • _padding_top_label_: space between border of label and html content inside the label, beginning at top (in cm.). If not set, 0 by default.
  • _skip_first_row_: set if you want to skip first row. Some printers cannot print the first row. You can to skip it (true|false). If not set, false by default.
  • _skip_last_row_: set if you want to skip last row. Some printers cannot print the last row. You can to skip it (true|false). If not set, false by default.
  • _page_format_: Measures of the page ('A4', for example). You can to give a values or a string format. See https://mpdf.github.io/ documentation.

    // array: [210,297].

    // string format: 'A0? - 'A10', 'B0' - 'B10', 'C0' - 'C10', '4A0', '2A0', 'RA0' - 'RA4', 'SRA0' - 'SRA4', 'Letter', 'Legal', 'Executive', 'Folio', 'Demy', 'Royal', 'A' (Type A paperback 111x178mm), 'B' (Type B paperback 128x198mm).

  • _page_orientation_: Portrait or Landscape. ('P'|'L'). If not set, 'P'ortrait by default.
  • _default_font_: String. If not set, 'Times'. See https://mpdf.github.io/ documentation for available fonts.
  • _default_font_size_: In pixels. If not set, 0 that means CSS default.
  • _draw_border_: (true|false). You can draw a border around the label. If not set, false that means without borders.
  • _start_at_label_: To recycle paper, if you have printed some labels and remain labels in blank for print, you can use another time the paper, begin at label num. that you specify here. If not set, 1 that means at first label. :recycle: :smiley:

    The distribution of number labels:

    --------------------- | label 1 | label 2 | --------------------- | label 3 | label 4 | --------------------- | label 5 | label 6 | --------------------- | label 7 | label 8 | --------------------- | label 9 | label 10| ---------------------

    Ex. Maybe you have a page printed labels from 1 to 5, and then have the rest labels in blank, without print. You can recycle the labels paper and use it another time!!!! If you want to print the first label beginning at position 6 (that is 3 row second col), then set $begin_at_label_num to 6 For our example, the pdf will be created as it: --------------------- | skipped | skipped | --------------------- | skipped | skipped | --------------------- | skipped | label 6 | --------------------- | label 7 | label 8 | --------------------- | label 9 | label 10| ---------------------

    IMPORTANT!!!! if you skip first row, the id's keep its natural order:

    --------------------- | skipped | skipped | --------------------- | label 3 | label 4 | --------------------- | label 5 | label 6 | --------------------- | label 7 | label 8 | --------------------- | label 9 | label 10| ---------------------

    Then, if whe skip first row and set $begin_at_label_num with 1, 2 or 3, will have the same result because in all the cases it will begin at label 3

  • DRAW BORDERS:

$printable_labels_pdf->draw_border( true );

Draw a border around the labels

Example:

    $printable_labels_pdf->draw_border( true );

  • WRITE LABEL:

$printable_labels_pdf->write_label( $label_html ); Send a content html for draw new label

  • You can use standard html tags as < b > bold, < i > italic, < u > underline or other. See example.php with some lines in different formats.

Example:

    // Make a string of the html label
      $label_html  = '<b>label bold</b><br />';       // 1st row. Bold
      $label_html .= '<i>Line 2 italic</i><br />';    // 2nd row. Italic
      $label_html .= '<u>Line 3 underline</u><br />'; // 3th row. Underline
      $label_html .= 'Line 4 normal';                 // 4th row. Standard text

      // send the html string to a new label
      $printable_labels_pdf->write_label( $label_html );

  • GET PDF WITH LABELS:

$printable_labels_pdf->get_labels_pdf( $file_name, $output); return the pdf in the browser in the output format

Examples:

    $printable_labels_pdf->get_labels_pdf( $file_name, $output);

a) Show directly in the browser (default)

$printable_labels_pdf->get_labels_pdf('test.pdf', 'I'); // Output a PDF file directly to the browser

b) Get a String of the pdf to do something with it later:

$SomeVarPdfString = $printable_labels_pdf->get_labels_pdf('test.pdf', 'S'); // Get pdf in string format and assign to $SomeVarPdfString

c) Download

$printable_labels_pdf->get_labels_pdf('test.pdf', 'D'); // Download pdf

d) Save pdf file in server path

$printable_labels_pdf->get_labels_pdf('/some_dir/test.pdf', 'F'); // Save pdf in some dir of the server. Ensure that you have writable perms to the folder

BENCHMARK:

You have included a Benchmark in the internal data of pdf properties (see creator application data) :smiley:

Configuration TEST in Virtual Ubuntu Linux: - Num labels: 2.499 - 4 lines each label - Line 1: Bold - Line 2: italic - Line 3: underline - Line 4: normal - Format Page: A4 - Distribution of labels 2 cols x 12 rows - Skip first row: true - Skip last row: true - Print Border: true - Begin at label 4 - Label Width: 8.89 - Label height: 2.33 - margin_left_page: 1.3 - margin_top_page: .2 - margin_left_label: .2 - margin_bottom_label: .2 - padding_left_label: .25 - padding_top_label: .25

RESULT: - 125 pages created in 18 seconds

  • See the original pdf created for benchmark here: https://github.com/vivesweb/printable_labels_pdf/blob/main/pritable_labels_pdf.pdf

In a simple Raspberry PI b 2 i get with the same config, with 999 labels, done in 2 minutes and 28 seconds :sweat_smile:

SOME NOTES:

I find some bugs floating divs and margins in mpdf. Only works with margin-left and margin-bottom label, but don't worry, there is no problem. All works ok :smile:.

Of course. You can use it freely :vulcan_salute::alien:

By Rafa.

@author Rafael Martin Soto

@author {@link http://www.inatica.com/ Inatica}

@blog {@link https://rafamartin10.blogspot.com/ Rafael Martin's Blog}

@since August 2021

@version 1.0.0

@license GNU General Public License v3.0


Screenshots  
  • 640.png
  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Plain text file printable_labels_pdf_class.php Class Class source
Accessible without login Plain text file pritable_labels_pdf.pdf Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Image file test_pdf_labels.png Data Auxiliary data
Accessible without login Image file test_pdf_labels_2.png Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 87%
Total:336
This week:2
All time:7,099
This week:96Up
User Ratings User Comments (3)
 All time
Utility:100%StarStarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:87%StarStarStarStarStar
Tests:-
Videos:-
Overall:78%StarStarStarStar
Rank:45
 
Thats a nice job, congratulations !
2 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar
Thats a nice job, congratulations !
2 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar
Thats a nice job, congratulations !
2 years ago (José Filipe Lopes Santos)
80%StarStarStarStarStar