Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pdf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
lib
unicaen
pdf
Commits
f36fa5c5
Commit
f36fa5c5
authored
2 years ago
by
Bertrand Gauthier
Browse files
Options
Downloads
Patches
Plain Diff
Reprise manuelle des dernières évolutions de l'Exporter dans unicaen/app
parent
18497c10
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Pipeline
#20309
failed
2 years ago
Stage: publish
Stage: tests
Changes
1
Pipelines
3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/UnicaenPdf/Exporter/PdfExporter.php
+101
-28
101 additions, 28 deletions
src/UnicaenPdf/Exporter/PdfExporter.php
with
101 additions
and
28 deletions
src/UnicaenPdf/Exporter/PdfExporter.php
+
101
−
28
View file @
f36fa5c5
...
...
@@ -2,15 +2,18 @@
namespace
UnicaenPdf\Exporter
;
use
Mpdf\Mpdf
as
mPDF
;
use
Laminas\View\Renderer\PhpRenderer
;
use
Laminas\View\Resolver\TemplatePathStack
;
use
LogicException
;
use
Mpdf\Mpdf
as
mPDF
;
use
RuntimeException
;
use
Laminas\View\Renderer\PhpRenderer
;
/**
* Classe utilitaire permettant de fabriquer un document PDF à partir
* de code HTML.
*
* NB: bibliothèque mPDF requise (http://www.mpdf1.com/mpdf/).
*
* @author Unicaen
*/
class
PdfExporter
implements
ExporterInterface
...
...
@@ -61,6 +64,14 @@ class PdfExporter implements ExporterInterface
* @var array
*/
private
$scriptVars
=
[];
/**
* @var array
*/
private
$footerVars
=
[];
/**
* @var array
*/
private
$headerVars
=
[];
/**
* @var boolean
*/
...
...
@@ -128,6 +139,13 @@ class PdfExporter implements ExporterInterface
*/
private
$logo
;
/**
* Chemin absolu du répertoire contenant les scripts de vue par défaut.
*
* @var string
*/
protected
$defaultScriptsDirPath
=
__DIR__
.
'/scripts'
;
/**
* Constructeur.
*
...
...
@@ -150,6 +168,9 @@ class PdfExporter implements ExporterInterface
$this
->
orientationPaysage
=
$orientationPaysage
;
$this
->
defaultFontSize
=
$defaultFontSize
;
$this
->
setHeaderScriptToDefault
();
$this
->
setFooterScriptToDefault
();
// $this->setLogo(file_get_contents(__DIR__ . "/../../../public/unicaen/app/img/logo-unicaen.png"));
}
...
...
@@ -161,7 +182,7 @@ class PdfExporter implements ExporterInterface
* @param string|null $oddOrEven 'O', 'E' ou null
* @return self
*/
public
function
setHeaderScript
(
string
$script
=
null
,
string
$oddOrEven
=
null
):
self
public
function
setHeaderScript
(
string
$script
=
null
,
string
$oddOrEven
=
null
,
array
$scriptVars
=
[]
):
self
{
if
(
!
$script
)
{
$this
->
headerScripts
=
[];
...
...
@@ -174,6 +195,36 @@ class PdfExporter implements ExporterInterface
$this
->
headerScripts
[
'O'
]
=
$script
;
$this
->
headerScripts
[
'E'
]
=
$script
;
}
if
(
$scriptVars
)
{
$this
->
headerVars
=
$scriptVars
;
}
return
$this
;
}
/**
* Spécifie qu'aucun script ne soit utilisé pour les entêtes de pages.
* Autrement dit, les pages du document PDF n'auront pas d'entête !
*
* @return self
*/
public
function
setHeaderScriptToNone
()
{
$this
->
headerScripts
=
array
();
return
$this
;
}
/**
* Restaure le script de vue par défaut pour les entêtes de pages (pages paires et impaires).
*
* @return self
*/
public
function
setHeaderScriptToDefault
()
{
$this
->
headerScripts
[
'O'
]
=
'header-odd.phtml'
;
$this
->
headerScripts
[
'E'
]
=
'header-even.phtml'
;
return
$this
;
}
...
...
@@ -225,7 +276,7 @@ class PdfExporter implements ExporterInterface
* @param string|null $oddOrEven 'O' (pages impaires seulement), 'E' (pages paires seulement) ou null (pages paires et impaires)
* @return self
*/
public
function
setFooterScript
(
string
$script
=
null
,
string
$oddOrEven
=
null
):
self
public
function
setFooterScript
(
string
$script
=
null
,
string
$oddOrEven
=
null
,
array
$scriptVars
=
[]
):
self
{
if
(
!
$script
)
{
$this
->
footerScripts
=
[];
...
...
@@ -237,6 +288,36 @@ class PdfExporter implements ExporterInterface
$this
->
footerScripts
[
'O'
]
=
$script
;
$this
->
footerScripts
[
'E'
]
=
$script
;
}
if
(
$scriptVars
)
{
$this
->
footerVars
=
$scriptVars
;
}
return
$this
;
}
/**
* Restaure le script de vue par défaut pour les pieds de pages (pages paires et impaires).
*
* @return self
*/
public
function
setFooterScriptToDefault
()
{
$this
->
footerScripts
[
'O'
]
=
'footer-odd.phtml'
;
$this
->
footerScripts
[
'E'
]
=
'footer-even.phtml'
;
return
$this
;
}
/**
* Spécifie qu'aucun script ne soit utilisé pour les pieds de pages.
* Autrement dit, les pages du document PDF n'auront pas de pied de page !
*
* @return self
*/
public
function
setFooterScriptToNone
()
{
$this
->
footerScripts
=
array
();
return
$this
;
}
...
...
@@ -254,9 +335,10 @@ class PdfExporter implements ExporterInterface
* PdfExporter::DESTINATION_STRING :
* Return the document as a string. filename is ignored.
*
* @param string $destination Exemple : PdfExporter::
self
* @param string|null $memoryLimit
* @param string $destination Exemple : PdfExporter::
DESTINATION_BROWSER
* @param string|null $memoryLimit
Quantité de mémoire maximum utilisée (memory_limit PHP), ex: '256M'
* @return string|null
*
* @throws \Mpdf\MpdfException
*/
public
function
export
(
string
$filename
=
null
,
string
$destination
=
PdfExporter
::
DESTINATION_BROWSER
,
string
$memoryLimit
=
null
)
...
...
@@ -392,29 +474,22 @@ class PdfExporter implements ExporterInterface
{
$headerOdd
=
$headerEven
=
null
;
$scriptVars
=
array
(
$scriptVars
=
[
'headerTitle'
=>
$this
->
headerTitle
,
'headerSubtitle'
=>
$this
->
headerSubtitle
,
'logo'
=>
$this
->
logo
);
$scriptVars
=
array_merge
(
$scriptVars
,
current
(
$this
->
scriptVars
)
?:
[]);
'logo'
=>
$this
->
logo
,
];
$scriptVars
=
array_merge
(
$this
->
headerVars
,
$scriptVars
);
// le logo doit être passé ainsi pour pouvoir être référencé dans la balise <img> sous la forme "var:logo"
$this
->
getMpdf
()
->
imageVars
[
'logo'
]
=
$this
->
logo
;
if
(
isset
(
$this
->
headerScripts
[
'O'
]))
{
$headerOdd
=
$this
->
getRenderer
()
->
render
(
$this
->
headerScripts
[
'O'
],
$scriptVars
);
}
elseif
(
file_exists
(
$filepath
=
$this
->
getDefaultScriptsPath
()
.
'/header-odd.phtml'
))
{
ob_start
();
include
$filepath
;
$headerOdd
=
ob_get_clean
();
}
if
(
isset
(
$this
->
headerScripts
[
'E'
]))
{
$headerEven
=
$this
->
getRenderer
()
->
render
(
$this
->
headerScripts
[
'E'
],
$scriptVars
);
}
elseif
(
file_exists
(
$filepath
=
$this
->
getDefaultScriptsPath
()
.
'/header-even.phtml'
))
{
ob_start
();
include
$filepath
;
$headerEven
=
ob_get_clean
();
}
if
(
$headerOdd
)
{
...
...
@@ -482,24 +557,17 @@ class PdfExporter implements ExporterInterface
{
$footerOdd
=
$footerEven
=
null
;
$scriptVars
=
array
(
'footerTitle'
=>
$this
->
footerTitle
);
$scriptVars
=
array_merge
(
$scriptVars
,
current
(
$this
->
scriptVars
)
?:
[]);
$scriptVars
=
[
'footerTitle'
=>
$this
->
footerTitle
,
];
$scriptVars
=
array_merge
(
$this
->
footerVars
,
$scriptVars
);
if
(
isset
(
$this
->
footerScripts
[
'O'
]))
{
$footerOdd
=
$this
->
getRenderer
()
->
render
(
$this
->
footerScripts
[
'O'
],
$scriptVars
);
}
elseif
(
file_exists
(
$filepath
=
$this
->
getDefaultScriptsPath
()
.
'/footer-odd.phtml'
))
{
ob_start
();
include
$filepath
;
$footerOdd
=
ob_get_clean
();
}
if
(
isset
(
$this
->
footerScripts
[
'E'
]))
{
$footerEven
=
$this
->
getRenderer
()
->
render
(
$this
->
footerScripts
[
'E'
],
$scriptVars
);
}
elseif
(
file_exists
(
$filepath
=
$this
->
getDefaultScriptsPath
()
.
'/footer-even.phtml'
))
{
ob_start
();
include
$filepath
;
$footerEven
=
ob_get_clean
();
}
if
(
$footerOdd
)
{
...
...
@@ -521,6 +589,11 @@ class PdfExporter implements ExporterInterface
public
function
setRenderer
(
PhpRenderer
$renderer
):
self
{
$this
->
renderer
=
$renderer
;
$this
->
renderer
->
resolver
()
->
attach
(
new
TemplatePathStack
([
'script_paths'
=>
[
$this
->
defaultScriptsDirPath
,
]]));
return
$this
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment