Skip to content

Commit c689e55

Browse files
committed
fix: Allow scaling labels to page width
1 parent ae7091e commit c689e55

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

website/app-templates/smarty/elements/label_form_common.tpl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424
</div>
2525
</div>
2626

27+
<div class="form-group">
28+
<label for="fit_to_page_width" class="col-sm-2 control-label">{t}Label Size{/t}</label>
29+
<div class="col-sm-10">
30+
<div class="checkbox">
31+
<label>
32+
<input type="checkbox" name="fit_to_page_width" id="fit_to_page_width" value="1" />
33+
{t}Fit label to page width (100%){/t}
34+
</label>
35+
</div>
36+
<p class="help-block">{t}If checked, the label will be scaled to fit 100% of the page width. If unchecked, the label will be printed at its default size.{/t}</p>
37+
</div>
38+
</div>
39+
2740
<div class="form-group">
2841
<div class="col-sm-offset-2 col-sm-10">
2942
{call csrf}

website/app-templates/smarty/forms/geokrety_labels.tpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@
4141
<ul class="col-sm-4 alert alert-success hidden list-unstyled" id="nrResult"></ul>
4242
</div>
4343

44+
<div class="row">
45+
<div class="col-sm-8">
46+
<div class="form-group">
47+
<label for="fit_to_page_width" class="col-sm-2 control-label">{t}Label Size{/t}</label>
48+
<div class="col-sm-10">
49+
<div class="checkbox">
50+
<label>
51+
<input type="checkbox" name="fit_to_page_width" id="fit_to_page_width" value="1" />
52+
{t}Fit each label to page width (100%){/t}
53+
</label>
54+
</div>
55+
<p class="help-block">{t}If checked, each label will be scaled to fit 100% of the page width. If unchecked, labels will be printed at their default size.{/t}</p>
56+
</div>
57+
</div>
58+
</div>
59+
</div>
60+
4461
<div class="form-group">
4562
<div class="col-sm-offset-2 col-sm-10">
4663
{call csrf}

website/app-templates/smarty/pages/geokrety_labels.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
{block name=title}{t}Mass label generator{/t}{/block}
44

5+
{\GeoKrety\Assets::instance()->addCss(GK_CDN_TOM_SELECT_CSS) && ''}
6+
{\GeoKrety\Assets::instance()->addJs(GK_CDN_TOM_SELECT_JS) && ''}
7+
58
{block name=content}
69
<h1>{t}Mass label generator{/t}</h1>
710
{include file='forms/geokrety_labels.tpl'}
811
{/block}
912

1013
{block name=javascript}
1114
{include file = "js/dialogs/dialog_geokret_move_select_from_inventory.tpl.js"}
15+
{include file = "js/moves/tomselect-inventory.tpl.js"}
1216
{include file = "js/moves/geokret_nr.validation.tpl.js"}
1317
{include file = "js/moves/geokret_move.inventory.tpl.js"}
1418

website/app/GeoKrety/Controller/Pages/GeokretLabel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ public function pdf() {
106106
$this->prepare_values();
107107
$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
108108
$pdf->addGeokrety($this->geokret);
109+
110+
// Apply fit to page width if requested
111+
$f3 = \Base::instance();
112+
$fitToPageWidth = !empty($f3->get('POST.fit_to_page_width'));
113+
$pdf->setFitToPageWidth($fitToPageWidth);
114+
109115
$pdf->setLanguages($this->langs);
110116
$pdf->render();
111117
}

website/app/GeoKrety/Controller/Pages/GeokretyLabels.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function pdf(\Base $f3) {
4646

4747
$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
4848
$pdf->addGeokrety(...(array) $geokrety);
49+
50+
// Apply fit to page width if requested
51+
$fitToPageWidth = !empty($f3->get('POST.fit_to_page_width'));
52+
$pdf->setFitToPageWidth($fitToPageWidth);
53+
4954
$pdf->setLanguages($this->languages);
5055
$pdf->render();
5156
}

website/app/GeoKrety/Service/Labels/Pdf.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Pdf extends \TCPDF {
1010
public const LABEL_OUTPUT_DPI = 300;
1111

1212
private array $geokrety = [];
13+
private bool $fitToPageWidth = false;
1314

1415
public function __construct($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false) {
1516
parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
@@ -96,12 +97,19 @@ public function addGeokrety(Geokret ...$geokrety) {
9697
$this->geokrety = array_merge($this->geokrety, $geokrety);
9798
}
9899

100+
public function setFitToPageWidth(bool $fitToPageWidth) {
101+
$this->fitToPageWidth = $fitToPageWidth;
102+
}
103+
99104
public function render() {
100105
$startLeft = $posX = PDF_MARGIN_LEFT;
101106
$startTop = $posY = 13;
102107
$image = new Image();
103108
$image->setLanguages($this->languages);
104109

110+
// Calculate available page width
111+
$availablePageWidth = $this->getPageWidth() - PDF_MARGIN_LEFT - PDF_MARGIN_RIGHT;
112+
105113
$imgPrevH = 0;
106114
for ($i = 0; $i < sizeof($this->geokrety); ++$i) {
107115
$labelPNGData = $image->png($this->geokrety[$i]);
@@ -111,13 +119,22 @@ public function render() {
111119
$imgW = $imageSize[0] * 25.4 / self::LABEL_OUTPUT_DPI;
112120
$imgH = $imageSize[1] * 25.4 / self::LABEL_OUTPUT_DPI;
113121

122+
// Apply fit to page width scaling if enabled
123+
if ($this->fitToPageWidth) {
124+
$scaleFactor = $availablePageWidth / $imgW;
125+
$imgW *= $scaleFactor;
126+
$imgH *= $scaleFactor;
127+
}
128+
114129
if ($posX + $imgW > $this->getPageWidth() - $startLeft) {
115130
$posX = $startLeft;
116131
$posY += $imgPrevH;
132+
$imgPrevH = 0; // Reset row height for new row
117133
}
118134

119135
if ($posY + $imgH > $this->getPageHeight() - $startTop) {
120136
$posY = $startTop;
137+
$imgPrevH = 0; // Reset row height for new page
121138
$this->AddPage();
122139
}
123140

0 commit comments

Comments
 (0)