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

XSL/XSLT/XPATH XML Discussion :

[FOP] Conversion XML en PDF avec FOP


Sujet :

XSL/XSLT/XPATH XML

  1. #1
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut [FOP] Conversion XML en PDF avec FOP
    Bonjour,
    J'essaye de faire un petit exemple de conversion de xml en pdf à l'aide de FOP mais j'obtiens des erreurs dans le code.
    Voici mon code
    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
     
    package creation.pdf;
     
    import javax.xml.parsers.*; 
    import org.w3c.dom.*; 
    import org.xml.sax.*; 
    import javax.xml.transform.*; 
    import javax.xml.transform.sax.*; 
    import javax.xml.transform.dom.*; 
    import javax.xml.transform.stream.*; 
    import java.io.*; 
    import org.apache.fop.apps.Driver;
    import java.util.*; 
     
     
    public class CreationPDF{
    	public static void creerPDF(String xml, String xsl, String pdf) throws Exception{
    		// création du résultat (pdf)
    		Driver driver = new Driver() {};
    		driver.setRenderer(Driver.RENDER_PDF);
    		driver.setOutputStream(new java.io.FileOutputStream(pdf));
    		Result resultat = new SAXResult(driver.getContentHandler());
     
    		// récupération de la source xml
    		Source source = new StreamSource(xml);
     
    		// création du transformer en fonction du xsl
    		Source style = new StreamSource(xsl);
    		TransformerFactory transformerFactory = TransformerFactory.newInstance();
    		Transformer transformer = transformerFactory.newTransformer(style);
     
    		// transformation
    		transformer.transform(source, resultat);
    	}
    	public static void main(String[] args){
    		try{
    			creerPDF("Annuaire.xml", "AnnuaireFOP.xsl", "Annuaire.pdf");
    		}catch(Exception e){e.printStackTrace();}
    	}
    }
    le netbeans me signale une erreur au niveau de
    import org.apache.fop.apps.Driver;
    Driver driver = new Driver();
    driver.setRenderer(Driver.RENDER_PDF);

    Si quelq'un peut m'aider ???

    Merci

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    Quelle erreur?

  3. #3
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    can't find symbole au niveau de import org.apache.fop.apps.Driver;
    et de meme pour les autres.
    Il me recommande d'importer java.sql.Driver !!!

    Merci

  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    As-tu bien ajouté FOP à ton projet sous netbeans? Si oui comment as-tu procédé?

  5. #5
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    J'ai telechargé le FOP 1 puis j'ai ajouté ma bibliothèque (qui comporte le jar de FOP) aux bibliothèques du projet

  6. #6
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    un jar (fop) dans un autre jar (ta bibliothèque) ca ne fonctionne pas en java. Met directement le jar de fop dans ton projet.

  7. #7
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    Je l'avais ajouté mais le même problème !

    En fait, j'ai crée une nouvelle biblio et c'est dans cette biblio que je mis mes jar téléchargé et j'importe ma biblio dans le projet.

  8. #8
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    tu importe la bibliothèque compilée ou le projet (ce n'est pas la même chose). Comment as-tu rajouté le jar pusque tu dit que tu as ajouté le jar de fop et que ça ne marche toujours pas.

  9. #9
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    Je crois que ce n'est un problème d'emplacement de jar, parce que déja dans le code il y a des classes propre à FOP et que mon IDE n'a pas signalé d'erreur en les utilisant. Si vous pouvez me donner un tuto expliquant comment faire avec le FOP version 1,car j'ai cherché dans les sites mais je trouve toujours des codes qui ont rapport avec les versions d'avant
    Merci beaucoup pour vos réponses.

  10. #10
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    quelle version de fop?

    cette classe n'existe plus dans les versions récentes de FOP

  11. #11
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    J'utilise la version 1.0.
    Si vous pouvez me donner un tuto pour comprendre comment faire pour l'utiliser ? Parce que c'est la 1er fois que je travaille avec cette API

    Voila j'ai mis se code et n'affiche pas d"erreur
    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
    import javax.xml.transform.*;
    import javax.xml.transform.sax.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    import java.io.*;
    import java.util.*;
    import org.apache.fop.apps.Driver;
    public class CreationPDF{
    	public static void creerPDF(String xml, String xsl, String pdf) throws Exception{
    		// création du résultat (pdf)
    		Driver driver = new Driver();
    		driver.setRenderer(Driver.RENDER_PDF);
    		driver.setOutputStream(new java.io.FileOutputStream(pdf));
    		Result resultat = new SAXResult(driver.getContentHandler());
     
    		// récupération de la source xml
    		Source source = new StreamSource(xml);
     
    		// création du transformer en fonction du xsl
    		Source style = new StreamSource(xsl);
    		TransformerFactory transformerFactory = TransformerFactory.newInstance();
    		Transformer transformer = transformerFactory.newTransformer(style);
     
    		// transformation
    		transformer.transform(source, resultat);
    	}
    	public static void main(String[] args){
    		try{
    			creerPDF("Annuaire.xml", "AnnuaireFOP.xsl", "Annuaire.pdf");
    		}catch(Exception e){e.printStackTrace();}
    	}
    }
    Mais quand j'execute il me donne ces erreurs
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Have no file for C:\Program Files\Java\jdk1.6.0_31\jre\lib\modules\jdk.boot.jar
    java.lang.NoClassDefFoundError: creationpdf/Main
    Caused by: java.lang.ClassNotFoundException: creationpdf.Main
            at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    Could not find the main class: creationpdf.Main.  Program will exit.
    Exception in thread "main" Java Result: 1

  12. #12
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    Could not find the main class: creationpdf.Main. Program will exit.
    D'après le code que tu as montré, ta classe main s'appelle "CreationPDF" et non pas "creationpdf.Main" comme tu tente de l'exécuter.

  13. #13
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    Le problème se trouve dans cette instruction
    Driver driver = new Driver();
    Elle n'est pas exécuté et je ne sais pas pourquoi !!! même si j'ai téléchargé le jar de FOP 0.20.5 et je l'avais importé dans la lib du projet et j'ai mis son chemin dans le classpath !!!
    .....................................
    Cela me rend malheureuse !!!

  14. #14
    Membre émérite Avatar de tsuji
    Inscrit en
    Octobre 2011
    Messages
    1 558
    Détails du profil
    Informations forums :
    Inscription : Octobre 2011
    Messages : 1 558
    Points : 2 736
    Points
    2 736
    Par défaut
    Le temps de gloire utilisant de la classe Driver semble bien dépassé: il date de l'époque fop-0.2x. Depuis fop-0.9x+ au moins, pour les cas simples, le codage s'écrit quelque peu comme ça.
    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
        public static void creerPDF(String xml, String xsl, String pdf) throws Exception{
            // création du résultat (pdf)
            /*
            Driver driver = new Driver();
            driver.setRenderer(Driver.RENDER_PDF);
            driver.setOutputStream(new java.io.FileOutputStream(pdf));
            Result resultat = new SAXResult(driver.getContentHandler());
            */
     
            //------------------proposé--------
            OutputStream outpdf=new BufferedOutputStream(new FileOutputStream(new File(pdf)));
            FopFactory fopFactory = FopFactory.newInstance(); 
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outpdf);
     
            Result resultat=new SAXResult(fop.getDefaultHandler());    //ou getContentHandler si vous avez un
            //------------------proposé--------
     
     
            // récupération de la source xml
            Source source = new StreamSource(xml);
     
            // création du transformer en fonction du xsl
            Source style = new StreamSource(xsl);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer(style);
     
            // transformation
            transformer.transform(source, resultat);
     
            //------------------proposé--------
            outpdf.flush();
            outpdf.close();
            //------------------proposé--------
     
        }
    ...rajoutant et/ou modifiant si necéssaire d'après des "throws" et "try-catch-final". Aussi vous ajoutez la plupart des jars empaquêtés dans le faiseau fop dans /build/ et /lib/ pour la convenience des utilisateurs.

  15. #15
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    J'ai bien intégré votre code
    Mais .... encore des erreurs !!!
    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
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlgraphics/image/loader/ImageContext
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
            at CreationPDF.creerPDF(CreationPDF.java:21)
            at CreationPDF.main(CreationPDF.java:49)
    Caused by: java.lang.ClassNotFoundException: org.apache.xmlgraphics.image.loader.ImageContext
            at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
            ... 14 more
    Java Result: 1

  16. #16
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    java.lang.NoClassDefFoundError: org/apache/xmlgraphics/image/loader/ImageContext
    Il te manque cette classe. As-tu bien ajouté toutes les dépendances de FOP à ton projet?

  17. #17
    Membre émérite Avatar de tsuji
    Inscrit en
    Octobre 2011
    Messages
    1 558
    Détails du profil
    Informations forums :
    Inscription : Octobre 2011
    Messages : 1 558
    Points : 2 736
    Points
    2 736
    Par défaut
    Aussi vous ajoutez la plupart des jars empaquêtés dans le faiseau fop dans /build/ et /lib/ pour la convenience des utilisateurs.
    Je veux dire ajouter dans le "classpath". Dans fop_home, quoi-qu'il en soit, /lib/, vous devrez trouver un jar comme xmlgraphics-commons-x.x.x.jar. Il doit y se trouver dans le "classpath". La même chose pour (pratiquement) tous les restes dans /lib/ - c'est ce que je voulais dire.

  18. #18
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    Enfin !!!! J'arrive à créer un PDF avec FOP
    Alors le problème étais que je dois ajouter aussi les jars qui se trouvent dans le dossier fop/lib à la lib de mon projet et ce n'est pas juste le fop.jar !!! parce que au début j'ai cru que tous les classes dont j'aurais besoin se trouveront dans le fop.jar.
    Merci beaucoup pour votre aide

  19. #19
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Points : 48 807
    Points
    48 807
    Par défaut
    il faut ajouter aussi toutes les autres bibiliothèques dont dépend fop à ton projet. La liste de ces bibliothèque doit être donnée quelque part sur le site de fop.

  20. #20
    Membre régulier Avatar de azassma
    Femme Profil pro
    Ph.D - Computer Vision
    Inscrit en
    Avril 2012
    Messages
    159
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ph.D - Computer Vision
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2012
    Messages : 159
    Points : 118
    Points
    118
    Par défaut
    Pour aider les autres qui peuvent rencontrer le meme problème que moi voila les codes complets :

    Le fichier XML Annuaire.xml
    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
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <annuaire>
    	<personne id="0">
    		<nom>nom0</nom>
    		<prenom>prenom0</prenom>
    		<adresse>adresse0</adresse>
    	</personne>
    	<personne id="1">
    		<nom>nom1</nom>
    		<prenom>prenom1</prenom>
    		<adresse>adresse1</adresse>
    	</personne>
    	<personne id="2">
    		<nom>nom2</nom>
    		<prenom>prenom2</prenom>
    		<adresse>adresse2</adresse>
    	</personne>
     
    	<personne id="3">
    		<nom>nom3</nom>
    		<prenom>prenom3</prenom>
    		<adresse>adresse3</adresse>
    	</personne>
    </annuaire>
    Le fichier AnnuaireFOP.xsl

    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
     
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    	<xsl:template match="/">
    		<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    					xmlns:fox="http://xml.apache.org/fop/extensions">
    			<fo:layout-master-set>
    				<fo:simple-page-master master-name="all"
    										page-height="29.7cm" 
    										page-width="21cm"
    										margin-top="1cm" 
    										margin-bottom="2cm" 
    										margin-left="2.5cm" 
    										margin-right="2.5cm">
    					<fo:region-body margin-top="3cm" margin-bottom="3cm"/>
    					<fo:region-before extent="3cm"/>
    					<fo:region-after extent="2.5cm"/>
    				</fo:simple-page-master>
    			</fo:layout-master-set>
     
    			<fo:page-sequence master-reference="all">
    				<fo:static-content flow-name="xsl-region-before">
    					<xsl:call-template name="entete"/>	
    				</fo:static-content> 
    				<fo:static-content flow-name="xsl-region-after">
    					<xsl:call-template name="basDePage"/>
    				</fo:static-content> 
    				<fo:flow flow-name="xsl-region-body">
    					<fo:block>
    						<xsl:call-template name="miseEnPage"/>
    					</fo:block>
    				</fo:flow>
    			</fo:page-sequence>
    		</fo:root> 
    	</xsl:template>
     
    	<xsl:template name="entete">
    		<fo:block text-align="center" 
    					font-size="8pt" 
    					line-height="10pt"
    					border-bottom="#D1D7DC"
    					border-bottom-style="solid"
    					border-bottom-width="1pt"
    					padding-top="2pt"
    					padding-right="2pt"
    					padding-left="2pt"
    					padding-bottom="2pt">
    					Resultats de la recherche
    		</fo:block>
    	</xsl:template>
     
    	<xsl:template name="miseEnPage">
    		<xsl:apply-templates select="annuaire"/>
    	</xsl:template>
     
    	<xsl:template match="annuaire">
    		<fo:block>
    			ANNUAIRE	
    		</fo:block>
    		<fo:block>
    			Actuellement, <xsl:value-of select="count(personne)"/> personnes dans l'annuaire.	
    		</fo:block>
    		<fo:block>
    			<fo:table table-layout="fixed" width="80%">
    				<fo:table-column column-width="proportional-column-width(1)"/>
    				<fo:table-column column-width="proportional-column-width(1)"/>
    				<fo:table-column column-width="proportional-column-width(1)"/>
    				<fo:table-column column-width="proportional-column-width(1)"/>
    				<fo:table-header>
    					<fo:table-row font-weight="bold" 
    									text-align="center" 
    									vertical-align="middle"
    						  			background-color="#A6A5C2">
    						<fo:table-cell border="black"
    									border-style="solid"
    									border-width="1pt">
    							<fo:block>ID</fo:block>
    						</fo:table-cell>
    						<fo:table-cell border="black"
    									border-style="solid"
    									border-width="1pt">
    							<fo:block>Nom</fo:block>
    						</fo:table-cell>
    						<fo:table-cell border="black"
    									border-style="solid"
    									border-width="1pt">
    							<fo:block>Prenom</fo:block>
    						</fo:table-cell>	
    						<fo:table-cell border="black"
    									border-style="solid"
    									border-width="1pt">
    							<fo:block>Adresse</fo:block>
    						</fo:table-cell>	
    					</fo:table-row>	
    				</fo:table-header>
    				<fo:table-body>
    					<xsl:apply-templates select="personne"/>
    				</fo:table-body>
    			</fo:table>	
    		</fo:block>
    	</xsl:template>
     
    	<xsl:template match="personne">
    		<fo:table-row>
    			<fo:table-cell border="black"
    							border-style="solid"
    							border-width="1pt">
    				<fo:block>
    					<xsl:value-of select="@id"/>
    				</fo:block>
    			</fo:table-cell>
    			<fo:table-cell border="black"
    							border-style="solid"
    							border-width="1pt">
    				<fo:block>
    					<xsl:value-of select="nom"/>
    				</fo:block>
    			</fo:table-cell>
    			<fo:table-cell border="black"
    							border-style="solid"
    							border-width="1pt">
    				<fo:block>
    					<xsl:value-of select="prenom"/>
    				</fo:block>
    			</fo:table-cell>
    			<fo:table-cell border="black"
    							border-style="solid"
    							border-width="1pt">
    				<fo:block>
    					<xsl:value-of select="adresse"/>
    				</fo:block>
    			</fo:table-cell>
    		</fo:table-row>	
    	</xsl:template>
     
    	<xsl:template name="basDePage">
    		<fo:block text-align="center" 
    					font-size="8pt" 
    					line-height="10pt"
    					border-top="#D1D7DC"
    					border-top-style="solid"
    					border-top-width="1pt"
    					padding-top="2pt"
    					padding-right="2pt"
    					padding-left="2pt"
    					padding-bottom="2pt">
    			<fo:block>
    				Douane - <fo:page-number/> -
    			</fo:block>
    		</fo:block>
    	</xsl:template>
    </xsl:stylesheet>
    Et mon fichier CreationPDF.java
    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
     
     
    package PackPDF;
     
    import javax.xml.transform.*;
    import javax.xml.transform.sax.*;
    import javax.xml.transform.stream.*;
    import java.io.*;
    import org.apache.fop.apps.FOUserAgent;
    import org.apache.fop.apps.FopFactory;
    import org.apache.fop.apps.Fop;
    import org.apache.fop.apps.MimeConstants;
    public class CreationPDF{
    	 public static void creerPDF(String xml, String xsl, String pdf) throws Exception{
     
     
            OutputStream outpdf=new BufferedOutputStream(new FileOutputStream(new File(pdf)));
            FopFactory fopFactory = FopFactory.newInstance();
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outpdf);
     
            Result resultat=new SAXResult(fop.getDefaultHandler());    //ou getContentHandler si vous avez un
     
     
            Source source = new StreamSource(xml);
     
            // création du transformer en fonction du xsl
            Source style = new StreamSource(xsl);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer(style);
     
            // transformation
            transformer.transform(source, resultat);
     
     
            outpdf.flush();
            outpdf.close();
     
     
        }
     
    	public static void main(String[] args){
    		try{
                        System.out.println("*****************");
                    creerPDF("./Annuaire.xml","./AnnuaireFOP.xsl", "monPDF.pdf");
    		}catch(Exception e){}
    	}
    }

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Conversion XML vers PDF avec ITEXT
    Par adelpiero dans le forum Format d'échange (XML, JSON...)
    Réponses: 1
    Dernier message: 29/07/2008, 10h16
  2. [FOP] conversion XML en PDF sous JBuilder10
    Par belassel_z dans le forum APIs
    Réponses: 1
    Dernier message: 09/06/2006, 13h17
  3. [FOP][ERROR][XSD] XML to PDF avec vérification XSD
    Par cash3000 dans le forum Valider
    Réponses: 1
    Dernier message: 09/05/2006, 09h53
  4. RTF to PDF avec FOP
    Par cash3000 dans le forum XML/XSL et SOAP
    Réponses: 8
    Dernier message: 14/04/2006, 08h55
  5. [FOP] Transformation XML to PDF par FOP
    Par JustAGphy dans le forum Format d'échange (XML, JSON...)
    Réponses: 13
    Dernier message: 12/05/2004, 15h19

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