Skip to content

Commit e60b467

Browse files
Added a feature to write a page counter
1 parent bc1e9f0 commit e60b467

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.1.1 - 2024-06-29
4+
- Added a feature where pdf can have page counter and total variables to show a page counter in pdf file.
5+
36
## 1.1.0 - 2023-11-16
47
- Upgraded to use DomPdf v2.x.
58

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "amici/craft-super-pdf",
33
"description": "Create PDF files from html",
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"type": "craft-plugin",
66
"keywords": ["craft", "cms", "craftcms", "craft-plugin","pdf", "amici", "dompdf", "twig-to-pdf", "create-pdf"],
77
"license": "proprietary",

src/SuperPdf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SuperPdf extends Plugin
2727
public $hasCpSection = false;
2828
public $hasCpSettings = true;
2929
public static $pluginHandle = 'super-pdf';
30-
public $schemaVersion = '1.1.0';
30+
public $schemaVersion = '1.1.1';
3131

3232
public function init()
3333
{

src/libraries/Pdf.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,13 @@ private function _generate()
110110
$this->dompdf->loadHtml($this->html);
111111
$this->dompdf->render();
112112

113+
if(strpos($this->html, "SUPER_PDF_TOTAL_PAGES") !== false) {
114+
$this->injectPageCount();
115+
}
116+
113117
if($this->settings['encrypt'])
114118
{
115-
$allow = [];
116-
if($this->settings['print']) $allow[] = 'print';
117-
if($this->settings['modify']) $allow[] = 'modify';
118-
if($this->settings['copy']) $allow[] = 'copy';
119-
if($this->settings['add']) $allow[] = 'add';
120-
121-
$this->dompdf->getCanvas()->get_cpdf()->setEncryption($this->settings['password'], $this->settings['adminPassword'], $allow);
119+
$this->injectEncryption();
122120
}
123121

124122
if($this->settings['type'] == 'url')
@@ -150,6 +148,29 @@ private function _generate()
150148

151149
}
152150

151+
public function injectPageCount(): void
152+
{
153+
$canvas = $this->dompdf->getCanvas();
154+
$pdf = $canvas->get_cpdf();
155+
156+
foreach ($pdf->objects as &$o) {
157+
if ($o['t'] === 'contents') {
158+
$o['c'] = str_replace('SUPER_PDF_TOTAL_PAGES', $canvas->get_page_count(), $o['c']);
159+
}
160+
}
161+
}
162+
163+
public function injectEncryption(): void
164+
{
165+
$allow = [];
166+
if($this->settings['print']) $allow[] = 'print';
167+
if($this->settings['modify']) $allow[] = 'modify';
168+
if($this->settings['copy']) $allow[] = 'copy';
169+
if($this->settings['add']) $allow[] = 'add';
170+
171+
$this->dompdf->getCanvas()->get_cpdf()->setEncryption($this->settings['password'], $this->settings['adminPassword'], $allow);
172+
}
173+
153174
public function renderFileFromStorage($filename = "")
154175
{
155176
if($filename == "")

0 commit comments

Comments
 (0)