linuxfront-v2-logistics/labels.html

📄 Overview

This HTML page provides a simple client-side label generator. When the user clicks Generate Labels, a JavaScript function builds a PDF consisting of one or more labels and prompts a download. It leverages jQuery and a custom PDF-assembly plugin (jsLabel2PDF.js) to render labels entirely in the browser .


⚙️ Dependencies

  • jQuery 1.4.2 for DOM manipulation (js/jquery-1.4.2.min.js)
  • jsLabel2PDF.js: a jQuery plugin that assembles raw PDF streams
  • base64.js: encodes the raw PDF into Base64 for a data URI
  • sprintf.js: string-formatting helper used internally by the PDF plugin
File Purpose
js/jquery-1.4.2.min.js Core DOM library
js/jsLabel2PDF.js PDF-assembly plugin (CreateTemplate, AddText)
js/base64.js Base64 encoder for embedding PDF data
js/sprintf.js Utility for formatting numeric/textual output

🏗️ HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Label printing</title>
  <script src="js/jquery-1.4.2.min.js"></script>
  <script src="js/jsLabel2PDF.js"></script>
  <script src="js/base64.js"></script>
  <script src="js/sprintf.js"></script>
</head>
<body>
  <div class="container">
    <h3>Printing Labels</h3>
    <a href="javascript:label2PDF()" style="color:black">Generate Labels</a>
  </div>

  <script>
    function label2PDF() {
      // Initialize a 4×6-inch label template
      jQuery.CreateTemplate("inches", 4, 6, 0, 0, 4, 6, 1, 6, 0.40, 0);
      // Start label assembly
      jQuery.CreateLabel();
      // Place text: coordinates in inches, content, font size
      jQuery.AddText(0.8, 0.8, 'Test Labels', 12);
      jQuery.AddText(0.9, 0.60, 'Shipment#', 10);
      // Render and download PDF
      jQuery.DrawPDF();
    }
  </script>
</body>
</html>

Source: linuxfront-v2-logistics/labels.html


🖱️ JavaScript Workflow

  1. CreateTemplate
    • Defines page units, paper size, margins, label grid, and spacing.
  2. CreateLabel
    • Begins a new PDF page for labels.
  3. AddText(x, y, text, fontSize)
    • Queues text at the specified coordinates (inches or mm).
  4. DrawPDF
    • Chains together PDF header, fonts, pages, resources, and trailer.
    • Encodes the final PDF as Base64 and triggers a download via a data:application/pdf;base64,… URI.

📐 Template Configuration

Parameter Description Example
units Measurement units: "inches" or "mm" "inches"
paperWidth Paper width 4
paperHeight Paper height 6
marginLeft Left margin 0
marginTop Top margin 0
labelWidth Width of each label 4
labelHeight Height of each label 6
numRows Number of label rows per page 1
numColumns Number of label columns per page 6
horizontalSpace Space between columns 0.40
verticalSpace Space between rows 0

✏️ Text Placement

  • Coordinates (x,y) correspond to distances from top-left of label area in chosen units.
  • Font size is specified in points.
  • You can call AddText multiple times to place varied content on the same label.

📈 PDF Generation Flowchart

flowchart TD
  A[User clicks "Generate Labels"] --> B[label2PDF() invoked]
  B --> C[CreateTemplate configured]
  C --> D[CreateLabel: new PDF page]
  D --> E[AddText queued: "Test Labels"]
  D --> F[AddText queued: "Shipment#"]
  E & F --> G[DrawPDF: assemble PDF]
  G --> H[Download PDF via data URI]

🎨 Customization Tips

  • Multiple Labels: Loop over CreateLabel + AddText calls for multi-label PDFs.
  • Dynamic Content: Replace hard-coded strings with variables (e.g. shipment IDs).
  • Styling: Adjust font sizes or add AddText calls for barcodes, images (requires plugin extension).
  • Units: Switch to millimeters by passing "mm" to CreateTemplate.

  • labels.php (server-side PDF via DOMPDF) for environments without JavaScript-enabled browsers.
  • js/jsLabel2PDF.js: Plugin source implementing PDF internals (see /js/jsLabel2PDF.js).
  • base64.js, sprintf.js: Utility libraries used by the plugin.

This documentation covers the purpose, structure, and customization of labels.html within the linuxfront-v2-logistics module.