Skip to content

Commit

Permalink
Added a feature to write a page counter
Browse files Browse the repository at this point in the history
  • Loading branch information
amici-infotech committed Jun 29, 2024
1 parent 791c003 commit 47479f0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.0.2.6 - 2024-06-29
- Added a feature where pdf can have page counter and total variables to show a page counter in pdf file.

## 2.0.2.5 - 2023-05-19
- Solved a bug where strict syntax of PHP classes want allowing to set $dompdf as null. #17

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "amici/craft-super-pdf",
"description": "Create PDF files from html",
"version": "2.0.2.5",
"version": "2.0.2.6",
"type": "craft-plugin",
"keywords": ["craft", "cms", "craftcms", "craft-plugin","pdf", "amici", "dompdf", "twig-to-pdf", "create-pdf"],
"license": "proprietary",
Expand Down
2 changes: 1 addition & 1 deletion src/SuperPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SuperPdf extends Plugin

public static $app;
public static Plugin $plugin;
public string $schemaVersion = '2.0.2.5';
public string $schemaVersion = '2.0.2.6';
// public string $minVersionRequired = '1.0.0';
public bool $hasCpSection = false;
public bool $hasCpSettings = true;
Expand Down
35 changes: 28 additions & 7 deletions src/libraries/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ private function _generate()
$this->dompdf->loadHtml($this->html);
$this->dompdf->render();

if(strpos($this->html, "SUPER_PDF_TOTAL_PAGES") !== false) {
$this->injectPageCount();
}

if($this->settings['encrypt'])
{
$allow = [];
if($this->settings['print']) $allow[] = 'print';
if($this->settings['modify']) $allow[] = 'modify';
if($this->settings['copy']) $allow[] = 'copy';
if($this->settings['add']) $allow[] = 'add';

$this->dompdf->getCanvas()->get_cpdf()->setEncryption($this->settings['password'], $this->settings['adminPassword'], $allow);
$this->injectEncryption();
}

if($this->settings['type'] == 'url')
Expand Down Expand Up @@ -149,6 +147,29 @@ private function _generate()

}

public function injectPageCount(): void
{
$canvas = $this->dompdf->getCanvas();
$pdf = $canvas->get_cpdf();

foreach ($pdf->objects as &$o) {
if ($o['t'] === 'contents') {
$o['c'] = str_replace('SUPER_PDF_TOTAL_PAGES', $canvas->get_page_count(), $o['c']);
}
}
}

public function injectEncryption(): void
{
$allow = [];
if($this->settings['print']) $allow[] = 'print';
if($this->settings['modify']) $allow[] = 'modify';
if($this->settings['copy']) $allow[] = 'copy';
if($this->settings['add']) $allow[] = 'add';

$this->dompdf->getCanvas()->get_cpdf()->setEncryption($this->settings['password'], $this->settings['adminPassword'], $allow);
}

public function renderFileFromStorage($filename = "")
{
if($filename == "")
Expand Down

0 comments on commit 47479f0

Please sign in to comment.