linuxfront-v2-logistics/labels.php
This script automates the creation of PDF labels using the DOMPDF library. It defines a custom paper size, loads simple HTML for each label (with forced page breaks), renders the PDF, and streams it to the browser for download. 🚀
Overview
labels.php is designed for logistics operations where quick, printable labels are required. It:
- Loads DOMPDF configuration
- Instantiates the renderer
- Defines a custom label size
- Embeds HTML for individual labels (each on its own page)
- Renders and streams a single PDF file
Dependencies
- DOMPDF Library: Converts HTML/CSS to PDF.
- dompdf_config.inc.php: Configuration and autoloading for DOMPDF.
Process Flow
flowchart TD
A[Start: Request labels.php] --> B[Include DOMPDF config]
B --> C[Create DOMPDF instance]
C --> D[Define custom paper size]
D --> E[Load HTML with page breaks]
E --> F[Render PDF]
F --> G[Stream PDF as download]
G --> H[Exit script]
Code Breakdown
1. Include DOMPDF Configuration
require_once("../tech-templates/inc/components/dompdf/dompdf_config.inc.php");
- Loads core DOMPDF settings and dependencies.
2. Instantiate DOMPDF
$dompdf = new DOMPDF();
- Creates a new PDF renderer.
3. Define Custom Paper Size
$customPaper = array(0, 0, 240, 360);
$dompdf->set_paper($customPaper);
- Sets dimensions in points (
[x, y, width, height]). - Here, 240×360 points match a specific label size.
4. Load HTML for Labels
$dompdf->load_html('
<table border="0" style="page-break-after:always">
<tr><td> label 1</td></tr>
</table>
<table border="0" style="page-break-after:always">
<tr><td> label 2</td></tr>
</table>
<table border="0" style="page-break-after:always">
<tr><td> label 3</td></tr>
</table>
');
- Three simple
<table>blocks, each ending withpage-break-after:alwaysto force new pages. - Replace the inner text (
label 1, etc.) with dynamic data as needed.
5. Render and Stream the PDF
$dompdf->render();
$dompdf->stream('test.pdf');
exit;
render()processes the HTML/CSS into the PDF canvas.stream()sends headers and outputstest.pdffor download.exitensures no further output.
Key Variables
| Variable | Type | Description | Example |
|---|---|---|---|
$dompdf |
object | The DOMPDF renderer instance | new DOMPDF() |
$customPaper |
array | Paper dimensions in points [x, y, w, h] |
[0,0,240,360] |
| HTML string | string | Contains label markup with page-break rules | see code above |
Usage & Extension
- Access: Navigate to
/linuxfront-v2-logistics/labels.phpin your browser. - Customize: Modify the HTML payload for dynamic content (e.g., shipment numbers, barcodes).
- Sizing: Adjust
$customPaperarray to match different label stock sizes. - Advanced: Integrate with database queries or templating engines to generate variable labels on the fly.
🎯 Purpose
This lightweight script provides a quick way to generate printable PDF labels without manual formatting. Ideal for small-batch label printing in warehouse or shipping workflows.
🛠️ Next Steps
- Parameterize label content via query parameters or POST data.
- Abstract HTML into a separate template file.
- Implement error handling around DOMPDF exceptions.
- Add styling (fonts, images) to the label HTML for branding.