PDF Labels Generation Script (pdf_labels.php) 🏷️

This script generates individual shipping labels in PDF format for a given Bill of Lading (BOL) ID. It fetches shipment details from the database, constructs HTML for each piece, and uses DOMPDF to render and stream the final PDF to the browser.

Overview

  • Retrieves carrier, shipment item, pickup, and drop-off details for a specific shipment.
  • Builds an HTML template for each piece (“Piece 1 of N”, “Piece 2 of N”, etc.).
  • Renders the HTML as a multi-page PDF and streams it to the user.

Dependencies

  • 📦 DOMPDF: HTML-to-PDF conversion library
  • 🔧 Query() / GetObj(): Database helper functions for executing SQL and fetching results
  • 📁 dompdf_config.inc.php: Configuration bootstrap for DOMPDF
  • PHP’s built-in $_GET to capture the id parameter

Custom Paper Configuration

The script defines a custom paper size in points (1 point = 1/72 inch):

// 0,0 = top-left origin; 360pt width (~5"); 190pt height (~2.6")
$customPaper = array(0, 0, 360, 190);
$dompdf->set_paper($customPaper);

This setting ensures each label fits the target label stock dimensions.

Data Retrieval 🗄️

The script builds and runs an SQL query to join three tables:

  • shippment_details (sd)
  • carriers
  • shipment_items
$qry = '
  SELECT
    carriers.carrier_name,
    carriers.carrier_scac,
    shipment_items.quantity,
    shipment_items.weight,
    DATE_FORMAT(sd.pickup_date, "%m/%d/%Y") AS pickupdate,
    sd.pro_number,
    sd.bol_no,
    sd.pickup_company_name, sd.pickup_contact, sd.pickup_email,
    sd.pickup_address, sd.pickup_city, sd.pickup_state,
    sd.pickup_zip, sd.pickup_phone,
    sd.drop_company_name, sd.drop_contact, sd.drop_email,
    sd.drop_address, sd.drop_city, sd.drop_state,
    sd.drop_zip, sd.drop_phone
  FROM shippment_details AS sd
  JOIN carriers ON sd.carrier_id = carriers.id
  JOIN shipment_items ON sd.id = shipment_items.shipment_id
  WHERE sd.id = ' . $_GET['id'];
$qry = Query($qry);

This fetches all pieces’ data for the requested BOL.

HTML Construction 🖋️

The script initializes an HTML string and loops from 1 to quantity to create one <table> per piece. Each table includes:

  • Carrier SCAC & name
  • BOL number and PRO number
  • Scheduled ship date
  • “Pieces X of Y” label
  • Total commercial weight
  • “From” and “To” address blocks
$html = '<html><body style="font-family:sans-serif;font-size:150%;">';
while ($rows = GetObj($qry)) {
  // extract fields into PHP variables…
  for ($i = 1; $i <= $quantity; $i++) {
    $html .= '<table border="0" style="page-break-after:always">
      <tr>
        <td style="font-size:13px;">' . $carrier_scac . ' ' . $carrier_name . '</td>
        <td style="font-weight:bold;font-size:13px;text-align:right;">
          BOL: ' . $_GET['id'] . '
        </td>
      </tr>
      <!-- more rows for Pro, Dates, Pieces, Weight, Addresses -->
    </table>';
  }
}
$html .= '</body></html>';

This loop ensures each label prints on its own PDF page.

PDF Rendering and Output 📄

After HTML generation, the script feeds it to DOMPDF, renders the PDF, and streams it inline:

$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($_GET['id'] . '.pdf');
exit;

The user’s browser receives a PDF named {BOL_ID}.pdf.

Key Variable Mapping

PHP Variable Source Column Purpose
$carrier_scac carriers.carrier_scac SCAC code for carrier
$carrier_name carriers.carrier_name Human-readable carrier name
$quantity shipment_items.quantity Total pieces to label
$weight shipment_items.weight Commercial weight per piece
$pro_number sd.pro_number PRO number for shipment
$pickupdate formatted sd.pickup_date Scheduled ship date (MM/DD/YYYY)
Pickup / Drop blocks sd pickup_/drop_ Full address, contact, and email info

Usage

  1. Place the file under linuxfront-v2-logistics/.
  2. Ensure DOMPDF and database utilities (Query(), GetObj()) are accessible.
  3. Invoke via URL, for example:
    https://yourdomain.com/linuxfront-v2-logistics/pdf_labels.php?id=123
  4. The browser prompts to download or view 123.pdf, containing one label per shipment piece.