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

Bibliothèques & Frameworks Discussion :

[jsPDF] Obliger la sélection du répertoire lors d'un download


Sujet :

Bibliothèques & Frameworks

  1. #1
    Membre actif Avatar de Zebulon777
    Homme Profil pro
    Informaticien
    Inscrit en
    Février 2005
    Messages
    1 319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Informaticien

    Informations forums :
    Inscription : Février 2005
    Messages : 1 319
    Points : 286
    Points
    286
    Par défaut [jsPDF] Obliger la sélection du répertoire lors d'un download
    Bonjour à tous,

    j'utilise la librairie jsPDF pour créer des fichiers PDF et qui fonctionne relativement bien.

    Mon souci est que je n'arrive pas à faire sélectionner un fichier (chemin + nom) pour créer le fichier PDF ?
    Peut être possible en php ?

    Voici le code que j'utilise (fait par un collègue) :
    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
     
    // Enregistrement au format PDF
    function saveAsPdf(){
     
     
    	var NbDoc = 0;
    	var NomPDF = "Export";
    	var Ext = ".PDF";
    	var FicPDF = "";
     
    	var doc = new jsPDF();
     
    	var pdfDocWidth  = doc.internal.pageSize.width;
    	var pdfDocHeight = doc.internal.pageSize.height;
     
    	var xMarginGraph = 5;
    	var yMarginGraph = 10;
     
    	var yPos = 0;
     
    /*-------------------------------------------------------------------------------------------------------------------*/
    	canvas = document.querySelector('#lfpo_hg canvas');
    	canvasImg = canvas.toDataURL('image/png', 1.0);
     
    	graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    	graphHeight = graphWidth * canvas.height / canvas.width;
     
    	doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
     
    	yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
    	canvas = document.querySelector('#lfpb_hg canvas');
    	canvasImg = canvas.toDataURL('image/png', 1.0);
     
    	graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    	graphHeight = graphWidth * canvas.height / canvas.width;
     
    	doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
     
    	yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
    	canvas = document.querySelector('#lfpb_hg canvas');
    	canvasImg = canvas.toDataURL('image/png', 1.0);
     
    	graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    	graphHeight = graphWidth * canvas.height / canvas.width;
     
    	doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
     
    	yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
     
    	FicPDF = NomPDF + Ext
    //		doc.save('export00.pdf');
    	doc.save(FicPDF);
     
    /*-------------------------------------------------------------------------------------------------------------------*/
     
    	alert("Fichier PDF créé.");
    }
    Zeb'...

  2. #2
    Membre actif Avatar de Zebulon777
    Homme Profil pro
    Informaticien
    Inscrit en
    Février 2005
    Messages
    1 319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Informaticien

    Informations forums :
    Inscription : Février 2005
    Messages : 1 319
    Points : 286
    Points
    286
    Par défaut
    Salut la compagnie,

    j'ai fait ça, mais çe ne fonctionne pas et je ne comprends pas pourquoi...

    La création du PDF fonctionne parfaitement.

    PS : J'avoue sans honte que le code PHP a été trouvé sur le net

    Merci d'avance pour votre aide.

    Code javascript : 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
    // Enregistrement au format PDF
    function saveAsPdf(){
    	
    	var WRep = window.confirm("Confirmation de l'exportation en PDF ?");
    	
    	if (WRep)
    		{
    			var NbDoc = 0;
    			var NomPDF = "Export";
    			var Ext = ".PDF";
    			var FicPDF = "";
    
    			var doc = new jsPDF();
    
    			var pdfDocWidth  = doc.internal.pageSize.width;
    			var pdfDocHeight = doc.internal.pageSize.height;
    
    			var xMarginGraph = 5;
    			var yMarginGraph = 10;
    
    			var yPos = 0;
    
    /*-------------------------------------------------------------------------------------------------------------------*/
    			canvas = document.querySelector('#lfpo_hg canvas');
    			canvasImg = canvas.toDataURL('image/png', 1.0);
    
    			graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    			graphHeight = graphWidth * canvas.height / canvas.width;
    
    			doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
    
    			yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
    			canvas = document.querySelector('#lfpb_hg canvas');
    			canvasImg = canvas.toDataURL('image/png', 1.0);
    
    			graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    			graphHeight = graphWidth * canvas.height / canvas.width;
    
    			doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
    
    			yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
    			canvas = document.querySelector('#lfpb_hg canvas');
    			canvasImg = canvas.toDataURL('image/png', 1.0);
    
    			graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    			graphHeight = graphWidth * canvas.height / canvas.width;
    
    			doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
    
    			yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
    
    			FicPDF = NomPDF + Ext
    			
    		$.ajax({
    			url:"./scripts/PHP/EnregistrerSous.php",
    			type:'POST',
    			datatype: 'html',
    			data:{'data' : FicPDF},
    			success : function(result) {
    				 
    				var answer = "<?php require('./scripts/PHP/EnregistrerSous.php');?>";
    				alert(answer);
    				alert('ok');
    			},
    			error: function(result){
    				alert('error');
    				 
    			},
    			 
    			 
    		});
    		
    		alert('FIN');
    ...
    ...
    ...

    Code php : 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
     
    <?php
    function downloadFile( $fullPath ){
     
      // Must be fresh start
      if( headers_sent() )
        die('Headers Sent');
     
      // Required for some browsers
      if(ini_get('zlib.output_compression'))
        ini_set('zlib.output_compression', 'Off');
     
      // File Exists?
      if( file_exists($fullPath) ){
     
        // Parse Info / Get Extension
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
     
        // Determine Content Type
        switch ($ext) {
          case "pdf": $ctype="application/pdf"; break;
          case "exe": $ctype="application/octet-stream"; break;
          case "zip": $ctype="application/zip"; break;
          case "doc": $ctype="application/msword"; break;
          case "xls": $ctype="application/vnd.ms-excel"; break;
          case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
          case "gif": $ctype="image/gif"; break;
          case "png": $ctype="image/png"; break;
          case "jpeg":
          case "jpg": $ctype="image/jpg"; break;
          default: $ctype="application/force-download";
        }
     
        header("Pragma: public"); // required
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false); // required for certain browsers
        header("Content-Type: $ctype");
        header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".$fsize);
        ob_clean();
        flush();
        readfile( $fullPath );
     
      } else
        die('File Not Found');
     
    }
    ?>
    Zeb'...

  3. #3
    Membre actif Avatar de Zebulon777
    Homme Profil pro
    Informaticien
    Inscrit en
    Février 2005
    Messages
    1 319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Informaticien

    Informations forums :
    Inscription : Février 2005
    Messages : 1 319
    Points : 286
    Points
    286
    Par défaut
    Je continu mes tests, mais non concluant pour le moment.

    Code javascript : 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
     
     
    // Enregistrement au format PDF
    function saveAsPdf(){
     
    	var WRep = window.confirm("Confirmation de l'exportation en PDF ?");
     
    	if (WRep)
    		{
    			var NbDoc = 0;
    			var NomPDF = "Export";
    			var Ext = ".PDF";
    			var FicPDF = "";
     
    			var doc = new jsPDF();
     
    			var pdfDocWidth  = doc.internal.pageSize.width;
    			var pdfDocHeight = doc.internal.pageSize.height;
     
    			var xMarginGraph = 5;
    			var yMarginGraph = 10;
     
    			var yPos = 0;
     
    /*-------------------------------------------------------------------------------------------------------------------*/
    			canvas = document.querySelector('#lfpo_hg canvas');
    			canvasImg = canvas.toDataURL('image/png', 1.0);
     
    			graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    			graphHeight = graphWidth * canvas.height / canvas.width;
     
    			doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
     
    			yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
    			canvas = document.querySelector('#lfpb_hg canvas');
    			canvasImg = canvas.toDataURL('image/png', 1.0);
     
    			graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    			graphHeight = graphWidth * canvas.height / canvas.width;
     
    			doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
     
    			yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
    			canvas = document.querySelector('#lfpb_hg canvas');
    			canvasImg = canvas.toDataURL('image/png', 1.0);
     
    			graphWidth  = pdfDocWidth - 2 * xMarginGraph;
    			graphHeight = graphWidth * canvas.height / canvas.width;
     
    			doc.addImage(canvasImg, 'PNG', xMarginGraph, (yPos + yMarginGraph), graphWidth, graphHeight);
     
    			yPos += (yMarginGraph + graphHeight);
    /*-------------------------------------------------------------------------------------------------------------------*/
     
    			FicPDF = NomPDF + Ext
    			doc.save(FicPDF);
     
    //            $.ajax({
    //                url:"./scripts/PHP/EnregistrerSous.php?fichier="+FicPDF,
    //                type:'POST',
    //                data:
    //                {
    //                    myFunction:'downloadFile',
    //					param: FicPDF,
    //				},
    //                success : function(result)
    //				{
    //                     
    //                    var answer = "<?php require('./scripts/PHP/EnregistrerSous.php');?>";
    //                    alert(answer);
    //                    alert('ok');
    //                },
    //                error: function(result)
    //				{
    //                    alert('error');
    //                     
    //                },
    //            });
     
    var resultat = $.get("./scripts/PHP/EnregistrerSous.php?fichier=".concat(FicPDF));
    console.log("resultat="+resultat);
    console.log();
     
    		alert('FIN');
     
     
     
     
    /*-------------------------------------------------------------------------------------------------------------------*/
     
    		alert("Fichier PDF '" + FicPDF + "' créé dans le répertoire 'Téléchargements' de votre ordinateur.");
    	}
    }
     
     
    });

    Code php : 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
     
    <?php
     
    $fullPath  = (isset($_GET['fichier'])) ? $_GET['fichier'] : NULL;
     
    echo($fullPath);
     
      // Must be fresh start
      if( headers_sent() )
        die('Headers Sent');
     
      // Required for some browsers
      if(ini_get('zlib.output_compression'))
        ini_set('zlib.output_compression', 'Off');
     
      // File Exists?
      if( file_exists($fullPath) ){
     
        // Parse Info / Get Extension
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);
     
        // Determine Content Type
        switch ($ext) {
          case "pdf": $ctype="application/pdf"; break;
          case "exe": $ctype="application/octet-stream"; break;
          case "zip": $ctype="application/zip"; break;
          case "doc": $ctype="application/msword"; break;
          case "xls": $ctype="application/vnd.ms-excel"; break;
          case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
          case "gif": $ctype="image/gif"; break;
          case "png": $ctype="image/png"; break;
          case "jpeg":
          case "jpg": $ctype="image/jpg"; break;
          default: $ctype="application/force-download";
        }
     
     
    header('Content-disposition: attachment; filename='.$fullPath); 
    header('Content-Type: application/force-download'); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".filesize($fullPath));
    header("Pragma: no-cache");
    header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
    header("Expires: 0");
    readfile($fullPath);	  
     
    //    header("Pragma: public"); // required
    //    header("Expires: 0");
    //    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    //    header("Cache-Control: private",false); // required for certain browsers
    //    header("Content-Type: $ctype");
    //    header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";" );
    //    header("Content-Transfer-Encoding: binary");
    //    header("Content-Length: ".$fsize);
    //    ob_clean();
    //    flush();
    //    readfile( $fullPath );
     
      } else
        die('File Not Found');
     
     
    ?>

    C'est vraiment une découverte pour moi, j'essaye de me débrouiller, mais là, je cale, donc une grosse aide ne serait vraiment pas de refus.

    Merci...
    Zeb'...

  4. #4
    Membre actif Avatar de Zebulon777
    Homme Profil pro
    Informaticien
    Inscrit en
    Février 2005
    Messages
    1 319
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Informaticien

    Informations forums :
    Inscription : Février 2005
    Messages : 1 319
    Points : 286
    Points
    286
    Par défaut
    Z'allez rire... (ou pas...)

    La librairie jsPdf gère le "enregistrer sous" de manière automatique, mais pour cela il suffit de bien configurer son navigateur en sélectionnant la possibilité de choisir où télécharger les fichiers.

    Voila, voila, c'était tout bête...

    En fait, j'utilise pratiquement tout le temps Firefox qui est bien configuré, mais je teste avec Chrome (dont le debugger est vraiment mieux, je trouve) qui, lui, était "mal" configuré...

    Je me marre tout seul devant mon écran !

    Donc, heu, je me cache...
    Zeb'...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Sélection de répertoire
    Par G_angel dans le forum WebDev
    Réponses: 7
    Dernier message: 02/10/2007, 14h45
  2. Inno Setup: sélection du répertoire d'installation
    Par franckgar dans le forum Outils
    Réponses: 4
    Dernier message: 24/07/2007, 10h34
  3. sélection de répertoire
    Par mouss31 dans le forum Qt
    Réponses: 2
    Dernier message: 12/12/2006, 11h10
  4. Création de répertoire lors d'un déploiment
    Par universdupc dans le forum Installation, Déploiement et Sécurité
    Réponses: 2
    Dernier message: 28/09/2006, 07h58
  5. [Swing] Browser (sélection de répertoire)
    Par _KB_ dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 31/03/2006, 23h24

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