IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Génération d'un pdf à partir d'une entité, formulaire et données sur symfony 4


Sujet :

Symfony PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Femme Profil pro
    Développeur Web
    Inscrit en
    Octobre 2010
    Messages
    414
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Seine Maritime (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Octobre 2010
    Messages : 414
    Par défaut Génération d'un pdf à partir d'une entité, formulaire et données sur symfony 4
    Bonjour

    J'essai de générer un pdf avec la librairie HTML2PDF sur mon projet symfony 4. Mon formulaire symfony envoie bien les données dans la base de données. J'aimerais afficher les données au format pdf.
    Mais j'obtiens cette erreur:
    (2/2) LoaderLoadException
    A service definition must be an array or a string starting with "@" but string found for service "app.html2pdf" in C:\xampp\htdocs\devis_facture\config/services.yaml. Check your YAML syntax in C:\xampp\htdocs\devis_facture\config/services.yaml (which is loaded in resource "C:\xampp\htdocs\devis_facture\config/services.yaml").
    Merci de m'aider !

    Je m'inspire ce ce tuto : https://www.supinfo.com/articles/sin...ml2pdf-symfony


    1- Installation de la librairie HTML2PDF
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    composer require spipu/html2pdf
    2- Création d'un service pour générer les PDF
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    services:    app.html2pdf:
            class:App\Service\Html2Pdf
    3-Création de notre template
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    <page backtop="0mm" backright="10mm" backbottom="20mm" backleft="10mm">	<style type="text/css">
    		<!--  table
    		{
    			width: 100%;
    			border-collapse:collapse;
    		font-size:16px}
     
     
    		.header td
    		{
    			width: 50%;
    		vertical-align: top}
     
     
    		.text-left
    		{
    		text-align: left}
     
     
    		.text-right
    		{
    		text-align: right}
     
     
    		.text-center
    		{
    		text-align: center}
     
     
    		.separator
    		{
    			height: 20px;
    		width: 100%}
     
     
    		.content td
    		{
    			border:solid 1px #CFD1D2;
    			padding: 5px;
    		}
     
     
    		.content th
    		{
    			border:solid 1px #000000;
    			padding: 5px;
    			background-color: #000000;
    		color: #FFFFFF}
     
     
    		.ligne1Content
    		{
    		background-color:#57B223}
     
     
    		.couleurgris
    		{
    			background-color:#DDDDDD;
    			height:auto;
    		}
     
     
    		.tht, .taxe, .ttc
    		{
    			font-size: 1.2em;
    		}
     
     
    		.ttc
    		{
    			color:#57B223;
    			font-weight:bold;
    		}
     
     
    		.couleurverte
    		{
    		background-color: #57B223}
     
     
    		.couleurmoinsgris
    		{
    			background: #EEEEEE;
    		}
     
     
    		.taille1
    		{
    			width:40%;
    		}
     
     
    		.taille2
    		{
    			width:15%;
    		}
     
     
    		.taille3
    		{
    			width:15%;
    		}
     
     
    		.taille4
    		{
    			width:20%;
    		}
     
     
    		.taille5
    		{
    			width:10%;
    		}
     
     
    		.header1
    		{
    		width:50%}
     
     
    		.header2
    		{
    		width:50%}
     
     
    		.tailleligne
    		{
    			height:auto;
    		}
     
     
    		.taille1, taille2, taille3, taille4, taille5
    		{
    			height:auto;
    		}
     
     
    		span
    		{
    			font-size:14px;
    			font-weight:bold;
    			color:#57B223;
    		}
     
     
    		h1, h2, h3
    		{
    			color:#57B223;
    		}
     
     
    		.colorwhite
    		{
    		color:white}
    	</style>
    	<table class="header">
    		<tr>
    			<td class="text-lef">
    				<h1>Facture</h1>
    				<br>
    			</td>
    		</tr>
    		<tr>
    			<td class="text-left">
    				<span>DE</span>
    				<br/>
    				<br/>
    				{{ facture.vosinfos }}
    				<br>
    			</td>
    			<td class="text-right">
    				<span>FACTURE N°:</span>
    				 {{ facture.numfacture }}
    				<br/>
    				<br/>
    				<span>DATE: </span>
    				{{ facture.datefacture ? facture.datefacture|date('Y-m-d H:i:s') : '' }}
    				<br/>
    				<br/>
    				<span>N° TVA:</span>
    				{{ facture.numtva }}
    			</td>
    		</tr>
    		<tr>
    			<td class="text-left">
    				<br/>
    				<br/>
    				<br/>
    				<br/>
    				<br/>
    				<br/>
    				<br/>
    				<span>FACTURÉ À</span>
    				<br/>
    				<br/>
    				{{ facture.infosclient }}
    				<br>
    			</td>
    			<td class="text-right"></td>
    		</tr>
    	</table>
    	<table class="content">
    		<thead>
    			<tr class="ligne1Content">
    				<td class="text-left couleurgris taille1">
    					<b>DESIGNATION</b>
    				</td>
    				<td class="text-center couleurmoinsgris taille2">
    					<b>QUANTITE</b>
    				</td>
    				<td class="text-center couleurgris taille3">
    					<b>PRIX HT</b>
    				</td>
    				<td class="text-center couleurgris taille5">
    					<b>TAXE</b>
    				</td>
    				<td class="text-center couleurverte taille4 colorwhite">
    					<b>TOTAL HT</b>
    				</td>
    			</tr>
    		</thead>
    		<tbody>
    			<tr class="couleurgris">
    				<td class="text-left couleurgris taille1">
    					{{ facture.designation1 }}
    				</td>
    				<td class="text-center couleurmoinsgris taille2">
    					{{ facture.quantite1 }}
    				</td>
    				<td class="text-center taille3">
    					{{ facture.prixht1 }}
    				</td>
    				<td class="text-center taille5">
    					{{ facture.taxe1 }}
    				</td>
    				<td class="text-center couleurverte taille4 colorwhite">
                         {{ facture.quantite1 }} * {{ facture.prixht1 }}
    				</td>
    			</tr>
     
     
    <tr class="couleurgris">
    				<td class="text-left couleurgris taille1">
    					{{ facture.designation2 }}
    				</td>
    				<td class="text-center couleurmoinsgris taille2">
    					{{ facture.quantite2 }}
    				</td>
    				<td class="text-center taille3">
    					{{ facture.prixht2 }}
    				</td>
    				<td class="text-center taille5">
    					{{ facture.taxe2 }}
    				</td>
    				<td class="text-center couleurverte taille4 colorwhite">
                         {{ facture.quantite2 }} * {{ facture.prixht2 }}
    				</td>
    			</tr>
    <tr class="couleurgris">
    				<td class="text-left couleurgris taille1">
    					{{ facture.designation3 }}
    				</td>
    				<td class="text-center couleurmoinsgris taille2">
    					{{ facture.quantite3 }}
    				</td>
    				<td class="text-center taille3">
    					{{ facture.prixht3 }}
    				</td>
    				<td class="text-center taille5">
    					{{ facture.taxe3 }}
    				</td>
    				<td class="text-center couleurverte taille4 colorwhite">
                         {{ facture.quantite3 }} * {{ facture.prixht3 }}
    				</td>
    			</tr>
     
     
    		</tbody>
    		<tfoot>
    			<tr>
    				<td colspan="4" class="text-right tht">
    					TOTAL HT
    				</td>
    				<td class="tht text-center">
    					{{ facture.quantite1 }} * {{ facture.prixht1 }} + {{ facture.quantite2 }} * {{ facture.prixht2 }} + {{ facture.quantite3 }} * {{ facture.prixht3 }}
    				</td>
    			</tr>
    			<tr>
    				<td colspan="4" class="text-right tht">
    					TAXE à {{ facture.taxe1 }}
    				</td>
    				<td class="text-center">
    					{{ facture.taxe2 }}
    				</td>
    			</tr>
    			<tr>
    				<td colspan="4" class="text-right">
    					<h3>TOTAL TTC À PAYER</h3>
    				</td>
    				<td class="text-center">
    					<h3> €</h3>
    				</td>
    			</tr>
    			<tr>
    				<td colspan="5">
    					<span>CONDITIONS ET MOYENS DE PAIEMENT:</span>
    					<br/>
    					{{ facture.conditions }}
    				</td>
    			</tr>
    		</tfoot>
    	</table>
    </page>
    4-Génération de notre PDF
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    /**     * @Route("facture_pdf/{id}", name="facture_pdf", methods={"GET"})
         */
        public function showPdf(Facture $facture): Response
        {
            return $template = $this->renderView('::facture/pdf.html.twig', [
                'facture' => $facture,
            ]);
     
     
          $html2pdf = $this->get('app.html2pdf');
          $html2pdf->create('P', 'A4', 'fr', true, 'UTF-8', array(10, 15, 10, 15));
     
     
          return $html2pdf->generatePdf($template, "facture");
        }

  2. #2
    Membre chevronné
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    351
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 351
    Par défaut
    Salut,

    Le message d'erreur semble indiquer une erreur de syntaxe dans le fichier C:\xampp\htdocs\devis_facture\config/services.yaml

    Essaye avec ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    services:
        app.html2pdf:
            class:'@App\Service\Html2Pdf'
    Par contre attention, le tutoriel que tu utilises date de 2016 et donc peut-être que depuis la manière de déclarer de nouveaux services à changé dans Symfony 4 (je pense à ça en particulier : https://symfony.com/doc/current/serv...y-name-or-type).

Discussions similaires

  1. Génération de pdf à partir d'une bdd
    Par glesker dans le forum Autres langages pour le Web
    Réponses: 1
    Dernier message: 27/10/2008, 21h08
  2. Génération PDF à partir d'une page JSP
    Par fatenatwork dans le forum Servlets/JSP
    Réponses: 4
    Dernier message: 04/12/2007, 21h15
  3. Générer un PDF à partir d'une page ASP
    Par Poussy-Puce dans le forum ASP
    Réponses: 1
    Dernier message: 30/03/2006, 17h45
  4. [ezPDF] Ouvrir un pdf à partir d'une page php
    Par aude_1603 dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 28/02/2006, 12h06
  5. [ java.net ] récupérer un fichier PDF à partir d'une URL
    Par nico2280 dans le forum Entrée/Sortie
    Réponses: 7
    Dernier message: 10/11/2005, 11h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo