Skip to content

Commit

Permalink
page count is now injected via placeholder as described in dompdf/dom…
Browse files Browse the repository at this point in the history
…pdf#1636

Signed-off-by: Christoph Massmann <[email protected]>
  • Loading branch information
ma4nn committed Dec 19, 2020
1 parent 28831fd commit 26cce38
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Model/Generator/Dompdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/
final class Dompdf extends AbstractGenerator
{
const TOTAL_PAGE_COUNT_PLACEHOLDER = '__PDF_TPC__';

/**
* @var \Dompdf\Dompdf
*/
Expand All @@ -51,6 +53,8 @@ public function renderPdfDocument(DocumentInterface $documentModel)
$this->domPdf->loadHtml($this->getHtmlContentsForDocument($documentModel));
$this->domPdf->render();

$this->injectPageCount();

return $this->domPdf->output();
}

Expand Down Expand Up @@ -106,4 +110,19 @@ private function getDompdfOptions()
'chroot' => $this->config->getChrootDir(),
);
}

/**
* @return void
*/
private function injectPageCount()
{
$canvas = $this->domPdf->getCanvas();
$pdf = $canvas->get_cpdf();

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

4 comments on commit 26cce38

@alexleach
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the usage of this documented anywhere? Looking through the code, is the idea that I can place __PDF_TPC__ somewhere in my html template, and that string will be replaced with the total page count? (Going to try this now, but would be nice if it was documented somewhere)

@MarcPinnell
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexleach - Did you get this working?

@alexleach
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Marc, thanks for checking in. I came here from the dompdf issue that's tagged in the commit message, and erroneously thought it was included in the dompdf code.

After realising it wasn't in core dompdf, basically I used your code to implement the same in the pdf rendering plugin that I've been working on for a timesheet app I'm using.

It works well, but I'm currently fiddling around with how to make the page numbering align to the right of the margin, as close as possible anyway. The code aligns the text in self::TOTAL_PAGE_COUNT_PLACEHOLDER, so I am trying to get that to be one character wide (with say &zwnj;&zwnj;_&zwnj;), but that's work in progress. Either way, yes I've got it working with your code better than I had it before, so progress! Thanks!

@ma4nn
Copy link
Member Author

@ma4nn ma4nn commented on 26cce38 Mar 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @alexleach,

thanks for your feedback.
You're right, it exactly works like that (i.e. __PDF_TPC__ will get replaced with the total page count).

I have updated the README file and added this information.

Best regards,
Chris

Please sign in to comment.