Skip to content

Instantly share code, notes, and snippets.

@NorskNoobing
Created May 2, 2023 10:45
Show Gist options
  • Save NorskNoobing/86d40989a602d016c4fc016c648bbb3f to your computer and use it in GitHub Desktop.
Save NorskNoobing/86d40989a602d016c4fc016c648bbb3f to your computer and use it in GitHub Desktop.
Custom labels snipeit
@php
// Most of the code is taken from this issue https://github.com/snipe/snipe-it/issues/3526#issuecomment-665558690
// No, this code shouldn't be here
// But it's being hacked in-place to minimize the number of file changes until it's properly integrated into the Laravel structure for a PR
require(resource_path('views/hardware/fpdf/fpdf.php'));
// Settings (per label mm chosen)
// All dimensions in mm (except font size)
$labelSettings = array();
$labelSettings[12]['margin'] = 0; // Extra margin on all sides
$labelSettings[12]['labelHeight'] = 12-$labelSettings[12]['margin']; // Overall label height
$labelSettings[12]['labelWidth'] = 32-$labelSettings[12]['margin']; // Overall label width
$labelSettings[12]['qrCodeSize'] = $labelSettings[12]['labelHeight']; // Width/height (square) size of QR code
$labelSettings[12]['textWidth'] = $labelSettings[12]['labelWidth']-$labelSettings[12]['qrCodeSize']; // Width of the label text area
$labelSettings[12]['fontSize'] = 8; // Font size in points
$labelSettings[12]['fontLineHeight'] = 2.5; // Line spacing in mm between text lines
$labelSettings[12]['textFromQrCodeSpacing'] = 1; // Extra padding between QR code and label text
$labelSettings[12]['heightSpacing'] = .8; // Extra padding from top to the label text
$labelSettings[18]['margin'] = 0; // Extra margin on all sides
$labelSettings[18]['labelHeight'] = 24-$labelSettings[18]['margin']; // Overall label height
$labelSettings[18]['labelWidth'] = 24-$labelSettings[18]['margin']; // Overall label width
$labelSettings[18]['qrCodeSize'] = $labelSettings[18]['labelHeight']; // Width/height (square) size of QR code
$labelSettings[18]['textWidth'] = $labelSettings[18]['labelWidth']-$labelSettings[18]['qrCodeSize']; // Width of the label text area
$labelSettings[18]['fontSize'] = 9; // Font size in points
$labelSettings[18]['fontLineHeight'] = 2.8; // Line spacing in mm between text lines
$labelSettings[18]['textFromQrCodeSpacing'] = 13; // Extra padding between QR code and label text
$labelSettings[18]['heightSpacing'] = 1.5; // Extra padding from top to the label text
$labelSettings[24]['margin'] = 0; // Extra margin on all sides
$labelSettings[24]['labelHeight'] = 24-$labelSettings[24]['margin']; // Overall label height
$labelSettings[24]['labelWidth'] = 89-$labelSettings[24]['margin']; // Overall label width
$labelSettings[24]['qrCodeSize'] = $labelSettings[24]['labelHeight']; // Width/height (square) size of QR code
$labelSettings[24]['textWidth'] = $labelSettings[24]['labelWidth']-$labelSettings[24]['qrCodeSize']; // Width of the label text area
$labelSettings[24]['fontSize'] = 9; // Font size in points
$labelSettings[24]['fontLineHeight'] = 2.8; // Line spacing in mm between text lines
$labelSettings[24]['textFromQrCodeSpacing'] = 13; // Extra padding between QR code and label text
$labelSettings[24]['heightSpacing'] = 1.5; // Extra padding from top to the label text
$labelAction = false;
if (isset($_POST['labelaction']) && preg_match("/\(([0-9]+)mm\)/", $_POST['labelaction'], $matches)) {
$labelAction = true;
$size = (int)$matches[1];
if (!isset($labelSettings[$size])) {
echo "Internal error invalid size {$size} doesn't match any settings";
return;
}
$chosenSettings = $labelSettings[$size];
// Output file
$labelFile = "labels-".time().".pdf";
$outputFile = sys_get_temp_dir()."/{$labelFile}";
$pdf = new FPDF('L', 'mm', array($chosenSettings['labelWidth'], $chosenSettings['labelHeight']));
$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(false);
foreach ($assets as $asset) {
$pdf->AddPage();
$pdf->SetY(0);
$pdf->SetX(0);
// Rect(float x, float y, float w, float h [, string style])
$pdf->Rect(0, 0, $chosenSettings['labelWidth'], $chosenSettings['labelHeight'], "D");
// SetFont(string family [, string style [, float size]])
$pdf->SetFont('Arial','B', $chosenSettings['fontSize']);
// QR Code
$qrCode = public_path().'/uploads/barcodes/qr-'.str_slug($asset->asset_tag).'-'.str_slug($asset->id).'.png';
if (!file_exists($qrCode)) {
echo "QR code doesn't exist: {$qrCode} - try reloading the original page and wait until all QR codes are displayed</br>";
}
// Image(string file [, float x [, float y [, float w [, float h [, string type [, mixed link]]]]]])
$pdf->Image($qrCode, 0, 0, $chosenSettings['qrCodeSize'], $chosenSettings['qrCodeSize'], "PNG");
// Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
$pdf->Cell($chosenSettings['labelWidth'], $chosenSettings['heightSpacing'], "", 0, 1);
// Spacing from the right of the QR code before the text
$pdf->Cell($chosenSettings['textFromQrCodeSpacing']);
// Uses Snipe's "Label visible fields" settings
// Displayed on the label in the order of the array
$labelFields = array (
array($settings->labels_display_company_name, (isset($asset->company) ? $asset->company->name : null)),
array($settings->labels_display_tag, $asset->asset_tag),
array($settings->labels_display_name, $asset->name),
array($settings->labels_display_serial, $asset->serial),
array($settings->labels_display_model, "{$asset->model->name} {$asset->model->model_number}"),
);
$labelText = "";
$setOne = false;
foreach ($labelFields as $field) {
list($enabled, $text) = $field;
if ($enabled == '1') {
if ($setOne) {
$labelText .= "\n";
} else {
$setOne = true;
}
$labelText .= $text;
}
}
// UTF-8 Encoding
$labelText = iconv('UTF-8', 'windows-1252', $labelText);
// MultiCell(float w, float h, string txt [, mixed border [, string align [, boolean fill]]])
$pdf->MultiCell($chosenSettings['textWidth'], $chosenSettings['fontLineHeight'], $labelText, 0, "L");
// Create the PDF file
$pdf->Output("F", $outputFile);
switch ($_POST['labelaction']) {
case "Download (12mm)":
case "Download (18mm)":
case "Download (24mm)":
if (preg_match("/^labels\-[0-9]+\.pdf$/", $labelFile)) {
$file = sys_get_temp_dir()."/{$labelFile}";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
return;
}
} else {
echo "Invalid label file specified for download: {$labelFile} <br/>";
}
break;
case "Print":
$cmd = '"'.$adobePath.'"';
$args = '/n /t "'.$outputFile.'" "'.$printerName.'"';
echo "Running {$cmd} {$args} to print PDF<br/>";
runAsynchronously($cmd, $args);
break;
}
}
}
@endphp
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Labels</title>
</head>
<body>
<style>
@if ($snipeSettings->custom_css)
{{ $snipeSettings->show_custom_css() }}
@endif
body {
background-color: #CCC;
margin: 0;
padding: 0;
}
.download {
margin-bottom: 4px;
}
.table-header {
font-weight: bold;
}
</style>
@if ($labelAction == false)
<form action="/hardware/bulkedit" method="POST">
<input type="hidden" name="bulk_actions" value="labels">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="hidden" name="btSelectItem" value="{{isset($_POST['btSelectItem']) ? $_POST['btSelectItem'] : ''}}">
<input type="hidden" name="search" value="{{isset($_POST['search']) ? $_POST['search'] : ''}}">
@foreach ($_POST['ids'] as $id)
<input type="hidden" name="ids[{{$id}}]" value="{{$id}}">
@endforeach
<div>
Notes:
<ul>
<li>Wait for this page to load fully before using any of the below options. The QR codes are generated as they are displayed by your browser.</li>
<li>This ignores most of the "Label Settings" dimensions in Snipe, except for "Label visible fields"</li>
</ul>
</div>
<table border="0">
<tr>
<td class="table-header"><div>Download PDF File</div><div>(includes all labels)</div></td>
<td>
<!-- <div class="download">
<input type="submit" name="labelaction" value="Download (12mm)">
</div>
--> <div class="download">
<button name="labelaction" value="Download (18mm)">Download (24mm) QR-only</button>
</div>
<div class="download">
<input type="submit" name="labelaction" value="Download (24mm)">
</div>
</td>
</tr>
<!-- <tr>
<td class="table-header">12mm Print Settings</td>
<td>Landscape, {{ $labelSettings[12]['labelWidth'] }}mm length</td>
--> </tr>
<tr>
<td class="table-header">24mm QR-only Print Settings</td>
<td>Landscape, {{ $labelSettings[18]['labelWidth'] }}mm length</td>
</tr>
<tr>
<td class="table-header">24mm Print Settings</td>
<td>Landscape, {{ $labelSettings[24]['labelWidth'] }}mm length</td>
</tr>
</div>
@foreach ($assets as $asset)
<tr><td class="table-header">Asset {{ $asset->id }}</td><td><img src="./{{ $asset->id }}/qr_code" class="qr_img"></td></tr>
@endforeach
</table>
</form>
@endif
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment