Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added total page count #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
DOMPDFModule
============

Project based on https://github.com/raykolbe/DOMPDFModule

Master: [![Build Status](https://secure.travis-ci.org/raykolbe/DOMPDFModule.png?branch=master)](http://travis-ci.org/raykolbe/DOMPDFModule)

The DOMPDF module integrates the DOMPDF library with Zend Framework 2 with minimal
Expand All @@ -21,7 +23,7 @@ PHP Composer, please visit the official [PHP Composer site](http://getcomposer.o
```json
{
"require": {
"dino/dompdf-module": "dev-master"
"victor-alencar/dompdf-module": "dev-master"
}
}
```
Expand Down Expand Up @@ -54,7 +56,12 @@ class ReportController extends AbstractActionController
$pdf->setOption('filename', 'monthly-report'); // Triggers PDF download, automatically appends ".pdf"
$pdf->setOption('paperSize', 'a4'); // Defaults to "8x11"
$pdf->setOption('paperOrientation', 'landscape'); // Defaults to "portrait"
$pdf->setOption('positionPageCounter', 'top-center'); // Defaults to "none". accepted values: "top-left", "top-center", "top-right", "bottom-left", "bottom-center", "bottom-right"
$pdf->setOption('textPageCounter', '{PAGE_NUM}/{PAGE_COUNT}'); // Defaults to "Page {PAGE_NUM} of {PAGE_COUNT}"

// or use array
// $pdf->setOptions(array('filename' => 'monthly-report', 'paperSize' => 'a4', ... ));

// To set view variables
$pdf->setVariables(array(
'message' => 'Hello'
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "dino/dompdf-module",
"name": "victor-alencar/dompdf-module",
"type": "library",
"description": "A Zend Framework 2 module for incorporating DOMPDF support.",
"description": "A Zend Framework 2 module for incorporating DOMPDF support. Based on https://github.com/raykolbe/DOMPDFModule",
"keywords": ["pdf","dompdf", "zf2"],
"homepage": "http://raymondkolbe.com",
"homepage": "https://github.com/VictorAlencar/DOMPDFModule",
"license": "MIT",
"authors": [
{
Expand Down
4 changes: 3 additions & 1 deletion src/DOMPDFModule/View/Model/PdfModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class PdfModel extends ViewModel
'paperSize' => '8x11',
'paperOrientation' => 'portrait',
'basePath' => '/',
'fileName' => null
'fileName' => null,
'positionPageCounter' => 'none',
'textPageCounter' => 'Page {PAGE_NUM} of {PAGE_COUNT}'
);

/**
Expand Down
56 changes: 56 additions & 0 deletions src/DOMPDFModule/View/Renderer/PdfRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class PdfRenderer implements Renderer
private $dompdf = null;
private $resolver = null;
private $htmlRenderer = null;
private $yPageCounter = null;
private $xPageCounter = null;

public function setHtmlRenderer(Renderer $renderer)
{
Expand Down Expand Up @@ -65,13 +67,24 @@ public function render($nameOrModel, $values = null)
$paperSize = $nameOrModel->getOption('paperSize');
$paperOrientation = $nameOrModel->getOption('paperOrientation');
$basePath = $nameOrModel->getOption('basePath');
$textPageCounter = $nameOrModel->getOption('textPageCounter');
$positionPageCounter = $nameOrModel->getOption('positionPageCounter');
$fontPageCounter = \Font_Metrics::get_font("helvetica", "normal");
$sizeFontPageCounter = 9;
$width_text_counter = mb_strwidth($textPageCounter);

$pdf = $this->getEngine();
$pdf->set_paper($paperSize, $paperOrientation);
$pdf->set_base_path($basePath);

$pdf->load_html($html);
$pdf->render();

if($positionPageCounter != 'none') {
$canvas = $pdf->get_canvas();
$this->calculatePositionCounterPage($positionPageCounter, $canvas->get_width(), $canvas->get_height(), $width_text_counter);
$canvas->page_text($this->xPageCounter, $this->yPageCounter, $textPageCounter, $fontPageCounter, $sizeFontPageCounter);
}

return $pdf->output();
}
Expand All @@ -81,4 +94,47 @@ public function setResolver(Resolver $resolver)
$this->resolver = $resolver;
return $this;
}

/**
* Calculates coordinates x and y where the text 'textPageCounter' (property) where will be printed
*
* @param string $positionPageCounter The fixed position
* @param integer $page_width The width of page
* @param integer $page_height The height of page
* @param string $width_text The width value calculated of 'textPageCounter' (property)
*/
private function calculatePositionCounterPage($positionPageCounter, $page_width, $page_height, $width_text)
{
switch ($positionPageCounter)
{
case 'top-center':
$this->yPageCounter = 15;
$this->xPageCounter = $page_width/2 - $width_text/2;
break;
case 'top-left':
$this->yPageCounter = 15;
$this->xPageCounter = 15;
break;
case 'top-right':
$this->yPageCounter = 15;
$this->xPageCounter = $page_width - 15 - $width_text;
break;
case 'bottom-left':
$this->yPageCounter = $page_height - 24;
$this->xPageCounter = 15;
break;
case 'bottom-center':
$this->yPageCounter = $page_height - 24;
$this->xPageCounter = $page_width/2 - $width_text/2;
break;
case 'bottom-right':
$this->yPageCounter = $page_height - 24;
$this->xPageCounter = $page_width - 15 - $width_text;
break;
default:
$this->yPageCounter = $page_height - 24;
$this->xPageCounter = $page_width - 15 - $width_text;
break;
}
}
}