Generate pdf file with printable labels

Overview

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) 😍

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

  • 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 πŸ˜ƒ . 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. ♻️ πŸ˜ƒ

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) πŸ˜ƒ

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

In a simple Raspberry PI b 2 i get with the same config, with 999 labels, done in 2 minutes and 28 seconds πŸ˜…

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 πŸ˜„ .

Of course. You can use it freely πŸ–– πŸ‘½

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

You might also like...
PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage

Snappy Snappy is a PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopd

TCPDF - PHP PDF Library - https://tcpdf.org

tc-lib-pdf PHP PDF Library UNDER DEVELOPMENT (NOT READY) UPDATE: CURRENTLY ALL THE DEPENDENCY LIBRARIES ARE ALMOST COMPLETE BUT THE CORE LIBRARY STILL

Pdf and graphic files generator library written in php

Information Examples Sample documents are in the "examples" directory. "index.php" file is the web interface to browse examples, "cli.php" is a consol

Convert html to an image, pdf or string
Convert html to an image, pdf or string

Convert a webpage to an image or pdf using headless Chrome The package can convert a webpage to an image or pdf. The conversion is done behind the sce

A PHP tool that helps you write eBooks in markdown and convert to PDF.
A PHP tool that helps you write eBooks in markdown and convert to PDF.

Artwork by Eric L. Barnes and Caneco from Laravel News ❀️ . This PHP tool helps you write eBooks in markdown. Run ibis build and an eBook will be gene

Laravel Snappy PDF

Snappy PDF/Image Wrapper for Laravel 5 and Lumen 5.1 This package is a ServiceProvider for Snappy: https://github.com/KnpLabs/snappy. Wkhtmltopdf Inst

Sign PDF files with valid x509 certificate
Sign PDF files with valid x509 certificate

Sign PDF files with valid x509 certificate Require this package in your composer.json and update composer. This will download the package and the depe

Convert a pdf to an image
Convert a pdf to an image

Convert a pdf to an image This package provides an easy to work with class to convert PDF's to images. Spatie is a webdesign agency in Antwerp, Belgiu

PHP library allowing PDF generation or snapshot from an URL or an HTML page. Wrapper for Kozea/WeasyPrint

PhpWeasyPrint PhpWeasyPrint is a PHP library allowing PDF generation from an URL or an HTML page. It's a wrapper for WeasyPrint, a smart solution help

Comments
  • Cannot include an image

    Cannot include an image

    trying to include an image on the label, it does not render.

    Code used is: $label_html .= '<img src="/logo-cirle-vert.png" width="75" height="75">' ``;

    Is this supported?

    Thanks

    opened by lleibovici 1
Owner
Rafael Martin Soto
PHP programmer. Passionate about PHP programming and artificial intelligence. Nice projects done with servers and raspberry pi ^_^
Rafael Martin Soto
Magento 2 Invoice PDF Generator - helps you to customize the pdf templates for Magento 2

Magento 2 Invoice PDF Generator - helps you to customize the pdf templates for Magento 2. If you have an enabled template and a default template for the store you need your template the system will print the pdf template.

EAdesign 64 Oct 18, 2021
Official clone of PHP library to generate PDF documents and barcodes

TCPDF PHP PDF Library Please consider supporting this project by making a donation via PayPal category Library author Nicola Asuni [email protected] co

Tecnick.com LTD 3.6k Jan 6, 2023
Generate simple PDF invoices with PHP

InvoiScript Generate simple PDF invoices with PHP. Installation Run: composer require mzur/invoiscript Usage Example use Mzur\InvoiScript\Invoice; re

Martin Zurowietz 16 Aug 24, 2022
Generate PDF invoices for your customers in laravel

What is Invoices? Invoices is a Laravel library that generates a PDF invoice for your customers. The PDF can be either downloaded or streamed in the b

Erik C. ForΓ©s 399 Jan 2, 2023
Rapidly Generate Simple Pdf, CSV, & Excel Report Package on Laravel

Laravel Report Generators (PDF, CSV & Excel) Rapidly Generate Simple Pdf Report on Laravel (Using barryvdh/laravel-dompdf or barryvdh/laravel-snappy)

Jimmy Setiawan 513 Dec 31, 2022
Official clone of PHP library to generate PDF documents and barcodes

TCPDF PHP PDF Library Please consider supporting this project by making a donation via PayPal category Library author Nicola Asuni [email protected] co

Tecnick.com LTD 3.6k Dec 26, 2022
PdfParser, a standalone PHP library, provides various tools to extract data from a PDF file.

PdfParser Pdf Parser, a standalone PHP library, provides various tools to extract data from a PDF file. Website : https://www.pdfparser.org Test the A

Sebastien MALOT 1.9k Jan 2, 2023
Convert HTML to PDF using Webkit (QtWebKit)

wkhtmltopdf and wkhtmltoimage wkhtmltopdf and wkhtmltoimage are command line tools to render HTML into PDF and various image formats using the QT Webk

wkhtmltopdf 13k Jan 4, 2023
HTML to PDF converter for PHP

Dompdf Dompdf is an HTML to PDF converter At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is

null 9.3k Jan 1, 2023
PHP library generating PDF files from UTF-8 encoded HTML

mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF (see CREDITS), with a number of enhancement

null 3.8k Jan 2, 2023