librairie html25pdf

Mammouth du PHP | 702 Messages

08 juin 2012, 10:44

Bonjpour à tous, j'ai un soucis avec la librairie html2pdf,

en fait j'ai besoin de générer une page et de la convertir en pdf, je fais donc ma page classiqeu avec mes requêtes php mais la ça bloque en fait, j'ai tout ça:
<p align="center">
  <?php
if (isset($_GET['tache']))
{
$date1= date('Y-m-d',strtotime($_GET['date1']));
$date2= date('Y-m-d',strtotime($_GET['date2']));
$sql = "SELECT * FROM agenda WHERE action LIKE '%".mysql_real_escape_string($_GET['tache'])."%' AND  date_action > '".$date1."' AND date_action < '".$date2."' AND date_validation='0000-00-00'";
$qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql);
$num_rows = mysql_num_rows($qry);
$lignes= $num_rows;
$get_date = function($data_jours) {
   $today = new DateTime();
   $today->setTime(0, 0);
   $jours = (isset($_POST['jours']) && ctype_digit($_POST['jours'])) ? $_POST['jours'] : 0;
   if (isset($_POST['submit']) && ($_POST['submit'] === 'moins')) {
      $jours = -$jours;
   }
   $nb       = $data_jours + $jours;
   $func     = ($nb < 0) ? 'sub' : 'add';
   $interval = new DateInterval("P{$nb}D");
   return $today->$func($interval)->format('d-m-Y');
};

$i = -1; // index des enregistrements
?><img src="images/agenda.png">
</p>
<table border="0" width="100%"><tr><td  nowrap>Du <?php echo $_GET['date1'] ; ?> Au <?php echo $_GET['date2'] ; ?>, Critères : <?php echo $_GET['tache'] ; ?></td><td  nowrap align="right">édité le <?php echo date('d-m-Y') ; ?></td></tr></table>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000" style="border:thin" rules="cols, rows" >
<THEAD> 
    <tr><th width="auto" nowrap><strong>CLIENT</strong></th>
    <th width="auto" nowrap><strong>N° DE DOSSIER</strong></th>
    <th width="auto" nowrap><strong>NOM</strong></th>
    <th width="auto" nowrap><strong>TEL</strong></th>
    <th width="auto" nowrap><strong>DATE </strong></th>
    <th width="auto" nowrap><strong>TRAITEMENT</strong></th>
    <th width="auto" nowrap><strong>DESCRIPTION</strong></th>
    <th width="auto" nowrap><strong>RESTANT DU</strong></th>
  </tr>
</THEAD>
 <?php while($row = mysql_fetch_assoc($qry)): ?>
<tbody>
  <tr>
         
         <td  nowrap><?php echo strtoupper($row['code_client']) ; ?></td>
         <td  nowrap><?php echo strtoupper($row['n_doss']) ; ?></td> <td nowrap><?php $sql2 = "SELECT * FROM CLIENT WHERE n_doss='".$row['n_doss']."' GROUP BY n_doss";
		$qry2 = mysql_query($sql2) or die(__LINE__.mysql_error().$sql2);
while($row2 = mysql_fetch_assoc($qry2)): 
if (!empty($row2['raison_sociale'])) { echo  strtoupper($row2['raison_sociale']) ; } else { echo  strtoupper($row2['nom']) ; }



?></td>
 <td nowrap><?php echo chunk_split($row2['tel'], 2, "."); endwhile ?></td><td  nowrap><?php 

$datetime1 = new DateTime("now");
$datetime2 = new DateTime($row['date_action']);
$interval = date_diff($datetime1, $datetime2);
   echo $get_date("".$interval->format('%a'));

 ?></td>
 <td  nowrap><?php echo strtoupper($row['action']); ?></td>
 <td  nowrap><?php echo strtoupper($row['description']); ?>
 </td>
 <td  nowrap><?php $sql7 = "SELECT SUM(montant) as montant FROM factures WHERE n_doss='".mysql_real_escape_string($row['n_doss'])."'";

$req7 = mysql_query ($sql7) or die ('Erreur SQL !<br>'.$sql7.'<br>'.mysql_error ()); 

while($data9 = mysql_fetch_assoc ($req7)) 

{  $pp=$data9['montant'] ;
					  if (empty($data9['montant'])) {

echo '0,00'; }

else { 
 $pp = $data9['montant'] ; $nbr2 = $pp;

// Notation française
$pp = number_format($nbr2, 2, ',', ' ');
// 1 234,56
 }
 } ?><?php 
$sql = "select * from factures where n_doss='".mysql_real_escape_string($row['n_doss'])."' GROUP BY n_doss";
$query = mysql_query($sql);
while ($data=mysql_fetch_assoc($query))
{
  
   $principal = $data['montant'];
   $echeance = $data['date_echue']; 
   $day= date('y-m-d');

 $s = strtotime($day)-strtotime($echeance); 
 $d = intval($s/86400)+1;   
   

   $i = (0.0006);
 
$tx=  $principal*($i/360)*$d;
$i2= number_format($tx, 2, ',', ' ');

 }
 ?><?php $sql5 = "SELECT SUM(montant) as montant FROM versements WHERE n_doss='".mysql_real_escape_string($row['n_doss'])."' AND etat ='3'";

$req5 = mysql_query ($sql5) or die ('Erreur SQL !<br>'.$sql5.'<br>'.mysql_error ()); 

while($data10 = mysql_fetch_assoc ($req5)) 

{   $nbr4=$data10['montant'] ;
					   

// Notation française
$rgt2 = $nbr4;
$rgt = number_format($nbr4, 2, ',', ' ');
// 1 234,56

 }
$versement = $rgt ;
 ?><?php					   
$sql253 = "SELECT * FROM `liasses` WHERE  n_doss='".mysql_real_escape_string($row['n_doss'])."'";
$req117 = mysql_query ($sql253) or die ('Erreur SQL !<br>'.$sql253.'<br>'.mysql_error ()); 	
$num_rows = mysql_num_rows($req117)		;

if ($num_rows =='0') 
{
$sql17 = "SELECT SUM(`factures`.`montant`)   AS montant FROM `factures`  WHERE `factures`.`n_doss`='".mysql_real_escape_string($row['n_doss'])."'";
} 
else 
{
$sql17 = "SELECT SUM(`factures`.`montant`) + SUM(`liasses`.`montant`)  AS montant FROM `factures` INNER JOIN `liasses` ON `factures`.`n_doss`=`liasses`.`n_doss` WHERE `factures`.`n_doss`='".mysql_real_escape_string($row['n_doss'])."'";
}
$req17 = mysql_query ($sql17) or die ('Erreur SQL !<br>'.$sql17.'<br>'.mysql_error ()); 

while($data119 = mysql_fetch_assoc ($req17)) 
{ 
$sd1 = number_format(($data119['montant']+ $tx-$nbr4)  , 2, ',', ' ');
 $principal2= $data119['montant'] ;
}
$principal3=$principal2;
echo $sd1;
?>
 &euro;</td>
      </tr><?php endwhile; ?> <?php }?>
      
      </tbody>
       <tfoot>
     <!--rangées du tableau-->
   </tfoot>
      </table>   
      <p align="right"><?php echo $lignes ; ?> Actions à traiter.
</body>
ceci est le contenu de ma page et en fait ça me génère un agenda en fonction de certains critères sous la forme d'un tableau.

de l'autre côté j'ai ça qui me permet de générer le pdf grâce à la librairie:
<?php
    $content = "
<page>
    <h1>Exemple d'utilisation</h1>
    <br>
    Ceci est un <b>exemple d'utilisation</b>
    de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";

    require_once(dirname(__FILE__).'/html2pdf.class.php');
    $html2pdf = new HTML2PDF('L','A4','fr');
    $html2pdf->WriteHTML($content);
    $html2pdf->Output('exemple.pdf');
?>
Le soucis c'est que je ne sais pas comment récupérer tout mon contenu générer avant en php dans une seule variable $content, j'ai bien essayé en tout concaténant, etc, mais j'ai pleins d'erreurs et ça ne fonctionne pas.

Par avance merci pour votre aide.

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

08 juin 2012, 10:47

salut,

effectivement il faut que tu mette le tout dans une variable (donc oui concaténation).


sinon la solution que je n'aime pas trop c'est d'utiliser la bufferisation de sortie.

@+
Il en faut peu pour être heureux ......

Mammouth du PHP | 702 Messages

08 juin 2012, 10:51

quelle solution est la plus évidente à mettre en place?

ViPHP
ViPHP | 2577 Messages

08 juin 2012, 11:19

Tu peux essayer un readfile('http://.....'); pour avoir le contenu d'une page.
Ca permet de générer le contenu de la même manière pour l'affichage et pour la transformation en PDF.

Mammouth du PHP | 702 Messages

08 juin 2012, 11:23

mais le soucis c'est avec les variables car en fait je génère un ageenda a partir d'un formulaire et l'affichage dépend des données du formulaire.

Mammouth du PHP | 702 Messages

08 juin 2012, 11:36

par exemple, la ça me met ça:

WriteHTML($content); $html2pdf->Output('agenda.pdf'); ?>
'".$date1."' AND date_action < '".$date2."' AND date_validation='0000-00-00'"; $qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql); $num_rows = mysql_num_rows($qry); $lignes= $num_rows; $get_date = function($data_jours) { $today = new DateTime(); $today->setTime(0, 0); $jours = (isset($_POST['jours']) && ctype_digit($_POST['jours'])) ? $_POST['jours'] : 0; if (isset($_POST['submit']) && ($_POST['submit'] === 'moins')) { $jours = -$jours; } $nb = $data_jours + $jours; $func = ($nb < 0) ? 'sub' : 'add'; $interval = new DateInterval("P{$nb}D"); return $today->$func($interval)->format('d-m-Y'); }; $i = -1; // index des enregistrements ?>

Du Au , Critères : édité le
CLIENT N° DE DOSSIER CLIENT TEL DATE TRAITEMENT DESCRIPTION RESTANT DU
format('%a')); ?> '.$sql7.'
'.mysql_error ()); while($data9 = mysql_fetch_assoc ($req7)) { $pp=$data9['montant'] ; if (empty($data9['montant'])) { echo '0,00'; } else { $pp = $data9['montant'] ; $nbr2 = $pp; // Notation française $pp = number_format($nbr2, 2, ',', ' '); // 1 234,56 } } ?>'.$sql5.'
'.mysql_error ()); while($data10 = mysql_fetch_assoc ($req5)) { $nbr4=$data10['montant'] ; // Notation française $rgt2 = $nbr4; $rgt = number_format($nbr4, 2, ',', ' '); // 1 234,56 } $versement = $rgt ; ?>'.$sql253.'
'.mysql_error ()); $num_rows = mysql_num_rows($req117) ; if ($num_rows =='0') { $sql17 = "SELECT SUM(`factures`.`montant`) AS montant FROM `factures` WHERE `factures`.`n_doss`='".mysql_real_escape_string($row['n_doss'])."'"; } else { $sql17 = "SELECT SUM(`factures`.`montant`) + SUM(`liasses`.`montant`) AS montant FROM `factures` INNER JOIN `liasses` ON `factures`.`n_doss`=`liasses`.`n_doss` WHERE `factures`.`n_doss`='".mysql_real_escape_string($row['n_doss'])."'"; } $req17 = mysql_query ($sql17) or die ('Erreur SQL !
'.$sql17.'
'.mysql_error ()); while($data119 = mysql_fetch_assoc ($req17)) { $sd1 = number_format(($data119['montant']+ $tx-$nbr4) , 2, ',', ' '); $principal2= $data119['montant'] ; } $principal3=$principal2; echo $sd1; ?> €
Actions à traiter. TCPDF ERROR: Some data has already been output, can't send PDF

ViPHP
ViPHP | 2577 Messages

08 juin 2012, 11:38

Passes les paramètres dans l'url et récupère les dans $_REQUEST à la place de $_POST.

Il faut bien faire le readfile d'une url avec http:// au début, pas lire le fichier directement.

Mammouth du PHP | 702 Messages

08 juin 2012, 11:48

les paramètres sont bien en get/

ViPHP
ViPHP | 2577 Messages

08 juin 2012, 13:29

Donc rien ne t'emêche de faire un truc genre :
$content = file_get_contents('http://www.example.com/page.php?par=111&pr2=456');

Désolé pour la fonction readfile() qui n'était pas adaptée car elle envoie le contenu dans la sortie standard.

Mammouth du PHP | 702 Messages

08 juin 2012, 14:03

la j'ai donc fais ceci:
une fois que ma page php est générée j'ai fais ceci:

<?php
header('Location: liste.php?date1='.$_REQUEST['date1'].'&date2='.$_REQUEST['date2'].'&tache='.$_REQUEST['tache'].'');
?>

et dans la page liste.php j'ai mis cela:

<?php
$content = file_get_contents('http://localhost/GESTIONNAIRE/impressio ... .$_REQUEST['date1'].'&date2='.$_REQUEST['date2'].'&tache='.$_REQUEST['tache'].'');

require_once(dirname(__FILE__).'/html2pdf.class.php');
$html2pdf = new HTML2PDF('L','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('agenda.pdf');
?>

mais cela me renvoit l'erreur sous jacente.
( ! ) Warning: file_get_contents(http://localhost/GESTIONNAIRE/impressio ... 012&tache=) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\liste.php on line 2Call Stack# Time Memory Function Location1 0.0004 678336 {main}( ) ..\liste.php:02 0.0004 678544 file_get_contents ( ) ..\liste.php:2TCPDF ERROR: Some data has already been output, can't send PDF file

Mammouth du PHP | 702 Messages

08 juin 2012, 15:11

j'ai finalement essayé ainsi:
<?php ob_start(); ?> 
 
<table border="0" width="100%"><tr><td align="center" colspan="2"><?php 
if (isset($_REQUEST['tache'])) 
{ 
$date1= date('Y-m-d',strtotime($_REQUEST['date1'])); 
$date2= date('Y-m-d',strtotime($_REQUEST['date2'])); 
$sql = "SELECT * FROM agenda WHERE action LIKE '%".mysql_real_escape_string($_REQUEST['tache'])."%' AND  date_action > '".$date1."' AND date_action < '".$date2."' AND date_validation='0000-00-00' ORDER BY date_action"; 
$qry = mysql_query($sql) or die(__LINE__.mysql_error().$sql); 
$num_rows = mysql_num_rows($qry); 
$lignes= $num_rows; 
$get_date = function($data_jours) { 
   $today = new DateTime(); 
   $today->setTime(0, 0); 
   $jours = (isset($_POST['jours']) && ctype_digit($_POST['jours'])) ? $_POST['jours'] : 0; 
   if (isset($_POST['submit']) && ($_POST['submit'] === 'moins')) { 
      $jours = -$jours; 
   } 
   $nb       = $data_jours + $jours; 
   $func     = ($nb < 0) ? 'sub' : 'add'; 
   $interval = new DateInterval("P{$nb}D"); 
   return $today->$func($interval)->format('d-m-Y'); 
}; 
 
$i = -1; // index des enregistrements 
?><img src="images/agenda.png" width="168" height="24"></td></tr><tr><td  nowrap>Du <?php echo $_REQUEST['date1'] ; ?> Au <?php echo $_REQUEST['date2'] ; ?>, Critères : <?php echo $_REQUEST['tache'] ; ?></td><td  nowrap align="right">édité le <?php echo date('d-m-Y') ; ?> Par <?php echo strtoupper($_SESSION['login']) ; ?></td></tr></table> 
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#000" style="border:thin" rules="cols, rows" > 
<THEAD>  
    <tr><th width="auto" nowrap><strong>CLIENT</strong></th> 
    <th width="auto" nowrap><strong>N° DE DOSSIER</strong></th> 
    <th width="auto" nowrap><strong>DEBITEUR</strong></th> 
    <th width="auto" nowrap><strong>TEL</strong></th> 
    <th width="auto" nowrap><strong>DATE </strong></th> 
    <th width="auto" nowrap><strong>TRAITEMENT</strong></th> 
    <th width="auto" nowrap><strong>DESCRIPTION</strong></th> 
    <th width="auto" nowrap><strong>RESTANT DU</strong></th> 
  </tr> 
</THEAD><tbody> 
 <?php while($row = mysql_fetch_assoc($qry)): ?> 
 
  <tr> 
          
         <td  nowrap><?php echo strtoupper($row['code_client']) ; ?></td> 
         <td  nowrap><?php echo strtoupper($row['n_doss']) ; ?></td> <td nowrap><?php $sql2 = "SELECT * FROM debiteur WHERE n_doss='".$row['n_doss']."' GROUP BY n_doss"; 
		$qry2 = mysql_query($sql2) or die(__LINE__.mysql_error().$sql2); 
while($row2 = mysql_fetch_assoc($qry2)):  
if (!empty($row2['raison_sociale'])) { echo  strtoupper($row2['raison_sociale']) ; } else { echo  strtoupper($row2['nom']) ; } 
 
 
 
?></td> 
 <td nowrap><?php echo chunk_split($row2['tel'], 2, "."); endwhile ?></td><td  nowrap><?php  
 
$datetime1 = new DateTime("now"); 
$datetime2 = new DateTime($row['date_action']); 
$interval = date_diff($datetime1, $datetime2); 
   echo $get_date("".$interval->format('%a')); 
 
 ?></td> 
 <td  nowrap><?php echo strtoupper($row['action']); ?></td> 
 <td  nowrap><?php echo strtoupper($row['description']); ?> 
 </td> 
 <td  nowrap><?php $sql7 = "SELECT SUM(montant) as montant FROM factures WHERE n_doss='".mysql_real_escape_string($row['n_doss'])."'"; 
 
$req7 = mysql_query ($sql7) or die ('Erreur SQL !<br>'.$sql7.'<br>'.mysql_error ());  
 
while($data9 = mysql_fetch_assoc ($req7))  
 
{  $pp=$data9['montant'] ; 
					  if (empty($data9['montant'])) { 
 
echo '0,00'; } 
 
else {  
 $pp = $data9['montant'] ; $nbr2 = $pp; 
 
// Notation française 
$pp = number_format($nbr2, 2, ',', ' '); 
// 1 234,56 
 } 
 } ?><?php  
$sql = "select * from factures where n_doss='".mysql_real_escape_string($row['n_doss'])."' GROUP BY n_doss"; 
$query = mysql_query($sql); 
while ($data=mysql_fetch_assoc($query)) 
{ 
   
   $principal = $data['montant']; 
   $echeance = $data['date_echue'];  
   $day= date('y-m-d'); 
 
 $s = strtotime($day)-strtotime($echeance);  
 $d = intval($s/86400)+1;    
    
 
   $i = (0.0006); 
  
$tx=  $principal*($i/360)*$d; 
$i2= number_format($tx, 2, ',', ' '); 
 
 } 
 ?><?php $sql5 = "SELECT SUM(montant) as montant FROM versements WHERE n_doss='".mysql_real_escape_string($row['n_doss'])."' AND etat ='3'"; 
 
$req5 = mysql_query ($sql5) or die ('Erreur SQL !<br>'.$sql5.'<br>'.mysql_error ());  
 
while($data10 = mysql_fetch_assoc ($req5))  
 
{   $nbr4=$data10['montant'] ; 
					    
 
// Notation française 
$rgt2 = $nbr4; 
$rgt = number_format($nbr4, 2, ',', ' '); 
// 1 234,56 
 
 } 
$versement = $rgt ; 
 ?><?php					    
$sql253 = "SELECT * FROM `liasses` WHERE  n_doss='".mysql_real_escape_string($row['n_doss'])."'"; 
$req117 = mysql_query ($sql253) or die ('Erreur SQL !<br>'.$sql253.'<br>'.mysql_error ()); 	 
$num_rows = mysql_num_rows($req117)		; 
 
if ($num_rows =='0')  
{ 
$sql17 = "SELECT SUM(`factures`.`montant`)   AS montant FROM `factures`  WHERE `factures`.`n_doss`='".mysql_real_escape_string($row['n_doss'])."'"; 
}  
else  
{ 
$sql17 = "SELECT SUM(`factures`.`montant`) + SUM(`liasses`.`montant`)  AS montant FROM `factures` INNER JOIN `liasses` ON `factures`.`n_doss`=`liasses`.`n_doss` WHERE `factures`.`n_doss`='".mysql_real_escape_string($row['n_doss'])."'"; 
} 
$req17 = mysql_query ($sql17) or die ('Erreur SQL !<br>'.$sql17.'<br>'.mysql_error ());  
 
while($data119 = mysql_fetch_assoc ($req17))  
{  
$sd1 = number_format(($data119['montant']+ $tx-$nbr4)  , 2, ',', ' '); 
 $principal2= $data119['montant'] ; 
} 
$principal3=$principal2; 
echo $sd1; 
?> 
 €</td> 
    </tr><?php endwhile; ?> <?php }?> 
      </tbody> 
       <tfoot> 
     <!--rangées du tableau--> 
   </tfoot> 
</table>    
      <p align="right"><?php echo $lignes ; ?> Actions à traiter.</p> 
    <?php $content=ob_get_contents(); 
    ob_end_clean(); 
    require_once('html2pdf.class.php'); 
    $html2pdf = new HTML2PDF('P','A4','fr'); 
    $html2pdf->WriteHTML($content); 
    $html2pdf->Output('monagenda.pdf'); 
?>
mais il me renvoi l'erreur sous jacente:

( ! ) Notice: Undefined offset: 17 in C:\wamp\www\NEOGETCASH\GESTIONNAIRE\html2pdf.class.php on line 4957
Call Stack
# Time Memory Function Location
1 0.0008 769664 {main}( ) ..\impressionagenda.php:0
2 0.2740 17309816 HTML2PDF->writeHTML( ) ..\impressionagenda.php:169
3 0.3597 18018624 HTML2PDF->_makeHTMLcode( ) ..\html2pdf.class.php:408
4 1.2486 18725512 HTML2PDF->_executeAction( ) ..\html2pdf.class.php:1218
5 1.2486 18725648 HTML2PDF->_tag_close_TABLE( ) ..\html2pdf.class.php:1255
TCPDF ERROR: Some data has already been output, can't send PDF file

je ne sais pas comment m'en sortir.

ViPHP
ViPHP | 2577 Messages

08 juin 2012, 15:24

Je pense que le html que tu génères n'est pas correct. Valide le source de la page html sur http://validator.w3.org/#validate_by_input.

Mammouth du PHP | 702 Messages

08 juin 2012, 16:49

y'a quelques balises alt qui sont manquantes mais a priori cela ne vient pas de ça