jsLabel2PDF.js
A jQuery-based client-side PDF label generator. This script builds a PDF in memory, draws a grid of label “cells,” overlays custom text, and finally prompts the browser to download or display the PDF via a data: URL.
📋 Overview
jsLabel2PDF.js enables dynamic label creation in pure JavaScript.
It supports:
- Custom paper size, margins, and label grid dimensions
- Unit conversion between inches and millimeters
- Text placement within each label cell
- Browser download via Base64-encoded PDF
No server-side PDF library required; works fully in the browser.
🔧 Dependencies
- jQuery (tested with 1.x/2.x)
- Base64 encoder (global
Base64.encode)
Include before using:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="path/to/Base64.js"></script>
<script src="js/jsLabel2PDF.js"></script>
🚀 Usage
- Initialize template
- Add text entries
- Generate PDF
// 1. Configure label sheet (inches or mm)
$.CreateTemplate(
"inches", // units
8.5, 11, // paperWidth, paperHeight
0.5, 0.5, // marginLeft, marginTop
2.5, 1.0, // labelWidth, labelHeight
10, 3, // numRows, numColumns
0.1, 0.1 // horizontalSpace, verticalSpace
);
// 2. Queue text at label-local coords (x, y)
$.AddText(0.1, 0.2, "Item SKU: 1234", 12);
$.AddText(0.1, 0.6, "Price: $9.99", 10);
// 3. Render and download
$.DrawPDF();
🔑 Public API
| Method | Signature | Description |
|---|---|---|
| CreateTemplate | (units, paperW, paperH, mLeft, mTop, lW, lH, rows, cols, hSpace, vSpace) |
Set up sheet and label grid dimensions. |
| AddText | (x, y, text, fontSize) |
Queue a text snippet at offset (x,y) in each label. |
| DrawPDF | () |
Build PDF, encode to Base64, trigger download. |
units:
"inches"or"mm"
coordinates and sizes use the chosen unit.
🔍 Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
| units | string | "inches" |
Measurement unit: "inches" or "mm". |
| paperWidth | number | 8.268 | Page width in units. |
| paperHeight | number | 11.693 | Page height in units. |
| marginLeft | number | 0.3815 | Left margin from page edge. |
| marginTop | number | 0.5965 | Top margin from page edge. |
| labelWidth | number | 2.5 | Width of each label cell. |
| labelHeight | number | 1.5 | Height of each label cell. |
| numRows | integer | 7 | Number of label rows per page. |
| numColumns | integer | 3 | Number of label columns per page. |
| horizontalSpace | number | 0.0 | Horizontal gap between label cells. |
| verticalSpace | number | 0.0 | Vertical gap between label cells. |
🏗️ Internal Workflow
A high-level flow of the $.DrawPDF() process:
flowchart TD
A[Start DrawPDF] --> B[setupVersion]
B --> C[setupFonts]
C --> D[setupXObject]
D --> E[setupResources]
E --> F[setupPage]
F --> G[setupPages]
G --> H[setupOutlines]
H --> I[setupCatalog]
I --> J[setupTrailer]
J --> K[Output PDF URI]
- setupVersion: writes
%PDF-1.4header - setupFonts: embeds a Helvetica font object
- setupXObject: creates one “form XObject” per label cell
- setupResources: builds
/Fontand/XObjectresource dictionaries - setupPage: renders each page’s content stream
- setupPages: groups page objects under
/Pages - setupOutlines: creates empty bookmarks structure
- setupCatalog: links
/Pagesand/Outlines - setupTrailer: finalizes cross-references and EOF
🔍 Internal Functions Cheat-Sheet
| Function | Role |
|---|---|
| setupVersion | PDF header |
| setupFonts | Register font object |
| setupXObject | Draw label grid rectangles and queued text |
| setupResources | Document resources (fonts, XObjects) |
| setupPage | Create page object + content stream |
| setupPages | Assemble /Pages dictionary |
| setupOutlines | Create /Outlines (bookmarks) |
| setupCatalog | Define root /Catalog |
| setupTrailer | Write xref, trailer, startxref, and %%EOF |
📐 Coordinate & Unit Conversion
- Inches → Points: multiply by 72
- Millimeters → Points: multiply by 72/25.4
Internal _…Pt variables store all dimensions in PDF points.
⚠️ Known Issues & Notes
- The code uses
if (units="inches")instead of==/===.
This assigns rather than compares, forcing"inches"mode always.
🛠️ Fix by replacing=with===. - Only one font (
Helvetica) is supported. - No support for images or multi-font text.
- The default grid size is 7×3; adjust in
CreateTemplate.
🎨 Customization Tips
- Font: swap
/BaseFont /HelveticainsetupFonts(). - Colors: insert PDF graphics operators (e.g.
RGfor stroke color) in XObject streams. - Line Styles: add
w(line width) ord(dash pattern) commands.
📝 Extending & Integration
- Wrap calls in a jQuery plugin: see commented
$.fn.DrawPDFstub. - Integrate with form inputs to collect user data, then loop over
$.AddText. - Combine with a label preview by rendering on HTML
<canvas>first.
📦 Package Managers
{
"commands": {
"npm": "npm install jquery jslabel2pdf-base64",
"yarn": "yarn add jquery jslabel2pdf-base64",
"pnpm": "pnpm add jquery jslabel2pdf-base64",
"bun": "bun add jquery jslabel2pdf-base64"
}
}
Replace
jslabel2pdf-base64with your Base64 utility package.
Enjoy dynamically generating PDF label sheets entirely in the browser! 🎉