PDFService
in package
Classe parent pour les services de génération de PDF (à partir d'un contenu HMTL ou d'une vue Twig).
Table of Contents
Constants
- MIME_TYPES = array('PDF' => [".pdf", "application/pdf"], 'PNG' => [".png", "image/png"])
- Types mimes & extensions de fichiers disponibles
Properties
- $confs : mixed
- Valeurs par défaut (options & chemins) pour les générations Twig|HTML vers PDF|PNG
- $debug : mixed
- $encore : mixed
- $paths : mixed
- $temp_files : mixed
- $twig : mixed
Methods
- __construct() : mixed
- Construction du service en chargeant la configuration des parameters.
- __destruct() : mixed
- initEngine() : PDFService
- Initialisation du moteur html2pdf avec ses options.
- loadHtmlData() : PDFService
- Initialisation du moteur html2pdf à partir d'un contenu HTML.
- loadHtmlFile() : PDFService
- Initialisation du moteur html2pdf à partir d'un fichier HTML.
- loadTwigView() : PDFService
- Initialisation du moteur html2pdf à partir d'une vue Twig à générer.
- renderData() : string
- Returns the PDF data as a string.
- renderFile() : string
- Returns the PDF in a local file.
- renderResponse() : Response
- Returns the PDF in a symfony Response object (clean integration).
- renderStream() : StreamedResponse
- Returns the PDF in a symfony StreamedResponse object (less memory usage).
- view2data() : string
- Initialisation à partir d'une vue Twig pour générer un PDF retourné dans une variable.
- view2file() : string
- Initialisation à partir d'une vue Twig pour générer un PDF retourné dans un fichier local.
- view2http() : StreamedResponse
- Initialisation à partir d'une vue Twig pour générer un PDF retourné dans une StreamedResponse.
- createTempFile() : string
- Création d'un fichier temporaire avec le contenu donné.
- fixFilename() : string
- fixFilepath() : string
- fixHtmlFile() : string
- fixTwigView() : string
- getFileType() : array<string|int, mixed>|null
- getHttpHeaders() : array<string|int, mixed>
- purgeTempFiles() : mixed
- Suppression des fichiers temporaires (sauf en mode debug).
Constants
MIME_TYPES
Types mimes & extensions de fichiers disponibles
public
mixed
MIME_TYPES
= array('PDF' => [".pdf", "application/pdf"], 'PNG' => [".png", "image/png"])
Properties
$confs
Valeurs par défaut (options & chemins) pour les générations Twig|HTML vers PDF|PNG
protected
mixed
$confs
= array(
'debug' => false,
// dump des options, debug CSS et fichier de log
'generator' => "auto",
// auto|pdf = PDFLib|CPDF|wkhtmltopdf ; image|png = GD|wkhtmltoimage
'pdflib_license' => null,
// [dompdf] si usage de PDFLib avec une licence
'media_type' => "screen",
// CSS3 media type: screen | print
'paper_size' => "A4",
// page format: A4, letter...
'orientation' => "portrait",
// page orientation: portrait | landscape
'default_font' => "DejaVu Sans",
// [dompdf] helvetica, times-roman, courier...
'font_ratio' => 1.1,
// [dompdf] ratio sur la hauteur (pour rendu proche des navigateurs)
'dpi' => 96,
// DPI setting (images & fonts)
'default_twigview' => "layout_print",
// nom de la vue twig par défault (sans l'extension ".html.twig")
'default_filename' => "document",
// nom du fichier de sortie (sans l'extension ".pdf")
'base_url' => null,
)
$debug
protected
mixed
$debug
= false
$encore
protected
mixed
$encore
$paths
protected
mixed
$paths
= array(
'root_dir' => null,
// racine des fichiers accessibles en chargement (chroot|allow)
'path_source' => null,
// dossier parent par défaut pour les fichiers chargés (HTML)
'path_output' => null,
// dossier parent par défaut pour les fichiers générés (PDF|PNG)
'path_logdir' => null,
// dossier parent pour les fichiers de log (debug)
'path_tmpdir' => null,
// dossier parent pour les fichiers temporaires (défaut sys_get_temp_dir)
'wkhtml_pdf' => __DIR__ . '../../../../bin/wkhtmltopdf-amd64',
// [wkhtml]
'wkhtml_image' => __DIR__ . '../../../../bin/wkhtmltoimage-amd64',
)
$temp_files
protected
mixed
$temp_files
= array()
$twig
protected
mixed
$twig
Methods
__construct()
Construction du service en chargeant la configuration des parameters.
public
__construct([array<string|int, mixed> $confs = [] ][, array<string|int, mixed> $paths = [] ][, Environment $twig = null ][, TranslatorInterface $translator = null ][, EntrypointLookupInterface $encore = null ]) : mixed
Parameters
- $confs : array<string|int, mixed> = []
-
Liste des options pour le paramétrage du service
- $paths : array<string|int, mixed> = []
-
Liste des chemins pour le paramétrage du service
- $twig : Environment = null
-
Instance du moteur de rendu pour générer les vues HTML
- $translator : TranslatorInterface = null
-
Service d'internationalisation pour changer la locale
- $encore : EntrypointLookupInterface = null
-
Accès à la réinitialisation de Webpack Encore
__destruct()
public
__destruct() : mixed
initEngine()
Initialisation du moteur html2pdf avec ses options.
public
abstract initEngine([array<string|int, mixed> $pdf_options = [] ]) : PDFService
Parameters
- $pdf_options : array<string|int, mixed> = []
-
Surcharge des options du moteur html2pdf
Return values
PDFServiceloadHtmlData()
Initialisation du moteur html2pdf à partir d'un contenu HTML.
public
abstract loadHtmlData(string $html_data[, array<string|int, mixed> $pdf_options = [] ]) : PDFService
Parameters
- $html_data : string
-
Contenu du document HTML à charger
- $pdf_options : array<string|int, mixed> = []
-
Surcharge des options du moteur html2pdf
Return values
PDFServiceloadHtmlFile()
Initialisation du moteur html2pdf à partir d'un fichier HTML.
public
abstract loadHtmlFile(string $html_file[, array<string|int, mixed> $pdf_options = [] ]) : PDFService
Parameters
- $html_file : string
-
Chemin du fichier HTML à charger
- $pdf_options : array<string|int, mixed> = []
-
Surcharge des options du moteur html2pdf
Return values
PDFServiceloadTwigView()
Initialisation du moteur html2pdf à partir d'une vue Twig à générer.
public
loadTwigView([string $twig_view = null ][, array<string|int, mixed> $view_params = [] ][, array<string|int, mixed> $pdf_options = [] ]) : PDFService
NOTE: avec gestion de la locale et reset de webpack encore (en cas d'usages multiples)
Parameters
- $twig_view : string = null
-
Chemin du template twig à générer
- $view_params : array<string|int, mixed> = []
-
Variables à injecter dans le template (peut contenir 'locale')
- $pdf_options : array<string|int, mixed> = []
-
Surcharge des options du moteur html2pdf
Return values
PDFServicerenderData()
Returns the PDF data as a string.
public
abstract renderData([bool $nocompress = false ]) : string
Parameters
- $nocompress : bool = false
-
Désactivation de la compression PDF
Return values
string —Données binaires du document PDF
renderFile()
Returns the PDF in a local file.
public
renderFile([string $filepath = null ][, bool $nocompress = false ]) : string
Parameters
- $filepath : string = null
-
Chemin du fichier PDF à enregistrer
- $nocompress : bool = false
-
Désactivation de la compression PDF
Return values
string —Chemin du fichier PDF généré
renderResponse()
Returns the PDF in a symfony Response object (clean integration).
public
renderResponse([string $filename = null ][, bool $inline = false ][, bool $nocompress = false ]) : Response
Parameters
- $filename : string = null
-
Nom du document PDF à télécharger
- $inline : bool = false
-
Affichage du PDF sans forcer son enregistrement
- $nocompress : bool = false
-
Désactivation de la compression PDF
Return values
Response —Objet à retourner par le controlleur
renderStream()
Returns the PDF in a symfony StreamedResponse object (less memory usage).
public
renderStream([string $filename = null ][, bool $inline = false ][, bool $nocompress = false ]) : StreamedResponse
Parameters
- $filename : string = null
-
Nom du document PDF à télécharger
- $inline : bool = false
-
Affichage du PDF sans forcer son enregistrement
- $nocompress : bool = false
-
Désactivation de la compression PDF
Return values
StreamedResponse —Objet à retourner par le controlleur
view2data()
Initialisation à partir d'une vue Twig pour générer un PDF retourné dans une variable.
public
view2data([string $twig_view = null ][, array<string|int, mixed> $view_params = [] ][, array<string|int, mixed> $pdf_options = [] ]) : string
Parameters
- $twig_view : string = null
-
Chemin du template twig à générer
- $view_params : array<string|int, mixed> = []
-
Variables à injecter dans le template (peut contenir 'locale')
- $pdf_options : array<string|int, mixed> = []
-
Surcharge des options du moteur html2pdf
Return values
string —Données binaires du document PDF
view2file()
Initialisation à partir d'une vue Twig pour générer un PDF retourné dans un fichier local.
public
view2file(string $filepath[, string $twig_view = null ][, array<string|int, mixed> $view_params = [] ][, array<string|int, mixed> $pdf_options = [] ]) : string
Parameters
- $filepath : string
-
Chemin du fichier PDF à enregistrer
- $twig_view : string = null
-
Chemin du template twig à générer
- $view_params : array<string|int, mixed> = []
-
Variables à injecter dans le template (peut contenir 'locale')
- $pdf_options : array<string|int, mixed> = []
-
Surcharge des options du moteur html2pdf
Return values
string —Chemin du fichier PDF généré
view2http()
Initialisation à partir d'une vue Twig pour générer un PDF retourné dans une StreamedResponse.
public
view2http(string $filename[, string $twig_view = null ][, array<string|int, mixed> $view_params = [] ][, array<string|int, mixed> $pdf_options = [] ]) : StreamedResponse
Parameters
- $filename : string
-
Nom du document PDF à télécharger
- $twig_view : string = null
-
Chemin du template twig à générer
- $view_params : array<string|int, mixed> = []
-
Variables à injecter dans le template (peut contenir 'locale')
- $pdf_options : array<string|int, mixed> = []
-
Surcharge des options du moteur html2pdf
Return values
StreamedResponse —Objet à retourner par le controlleur
createTempFile()
Création d'un fichier temporaire avec le contenu donné.
protected
createTempFile([string $prefix = "" ][, string $suffix = null ][, string $file_data = null ]) : string
Parameters
- $prefix : string = ""
-
Chaine pour le début du nom du fichier (sous-dossier si commence par un /)
- $suffix : string = null
-
Chaine à utiliser comme extension du nom du fichier
- $file_data : string = null
-
Contenu à placer dans le fichier à générer
Return values
string —Chemin absolu du fichier temporaire (généré/à générer)
fixFilename()
protected
fixFilename([string|null $filename = null ][, string|null $extension = null ]) : string
Parameters
- $filename : string|null = null
- $extension : string|null = null
Return values
stringfixFilepath()
protected
fixFilepath([string|null $filepath = null ]) : string
Parameters
- $filepath : string|null = null
Return values
stringfixHtmlFile()
protected
fixHtmlFile(string $html_file) : string
Parameters
- $html_file : string
Return values
stringfixTwigView()
protected
fixTwigView([string|null $twig_view = null ]) : string
Parameters
- $twig_view : string|null = null
Return values
stringgetFileType()
protected
getFileType([string $type = "PDF" ]) : array<string|int, mixed>|null
Parameters
- $type : string = "PDF"
Return values
array<string|int, mixed>|nullgetHttpHeaders()
protected
getHttpHeaders([string|null $filename = null ][, bool $inline = false ]) : array<string|int, mixed>
Parameters
- $filename : string|null = null
- $inline : bool = false
Return values
array<string|int, mixed>purgeTempFiles()
Suppression des fichiers temporaires (sauf en mode debug).
protected
purgeTempFiles() : mixed