CREER UN PDF AVEC XML ET FOP

alex1er Messages postés 39 Date d'inscription jeudi 11 avril 2002 Statut Membre Dernière intervention 5 juin 2006 - 27 juin 2003 à 11:36
ouechTonton Messages postés 13 Date d'inscription samedi 1 décembre 2007 Statut Membre Dernière intervention 13 novembre 2011 - 11 juil. 2011 à 10:02
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/15611-creer-un-pdf-avec-xml-et-fop

ouechTonton Messages postés 13 Date d'inscription samedi 1 décembre 2007 Statut Membre Dernière intervention 13 novembre 2011
11 juil. 2011 à 10:02
Pour le problème de compilation sur les lignes
Fop fop = new Fop(MimeConstants.MIME_PDF);
et fop.setOutputStream(out);

Apache a changé la manière de construire l'objet Fop. Il faut remplacer les deux lignes précédentes par ça :

Fop fop = org.apache.fop.apps.FopFactory.newInstance().newFop(MimeConstants.MIME_PDF, out);
vaytess Messages postés 14 Date d'inscription samedi 18 octobre 2008 Statut Membre Dernière intervention 16 juillet 2009
3 août 2009 à 18:53
salut à tous..j me suis arrivé à compiler et exécuter l'application mais je n sais pas pourquoi ça m généré des erreurs lors de l'exécution.!!! Est ce que qlq1 a tester un exemple.. j'ai vraiment besoin d'aide..
vaytess Messages postés 14 Date d'inscription samedi 18 octobre 2008 Statut Membre Dernière intervention 16 juillet 2009
28 juil. 2009 à 14:10
salut UDDASA.. le message "Recompile -Xlint:deprecation for details." signifie que le code source n'est plus valide. c'est comme si tu as lancer un logiciel démo dont la période d'évaluation est dépassée. Ceci est aussi exprimé par "The Apache Software License".
Bon si qlq1 d'autre a réussi l'exécution de l'application, ça sera gentil de nous informé :))
Bonne Boss
legastu Messages postés 3 Date d'inscription dimanche 23 mars 2008 Statut Membre Dernière intervention 21 juillet 2009
30 juin 2009 à 18:04
Bonjour,

J'ai un problème pour ces 2 imports :

import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;

Apparemment ils n'existent pas...
Uddasa Messages postés 8 Date d'inscription jeudi 7 juillet 2005 Statut Membre Dernière intervention 1 août 2007
1 août 2007 à 09:55
J'ai le même problème, voici le message:

Note: C:\Documents and Settings\STAGIAIRE\TestPDF\src\testpdf\ExampleXML2PDF.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

java.lang.NoClassDefFoundError: testpdf/ExampleXML2PDF

J'ai 2 avertissements: à l'instanciation de Fop et lors de son utilisation. Dans la FAQ il est demandé de vérifier le classpath, mais je ne sais pas trop quoi vérifier...
bwwilly Messages postés 1 Date d'inscription mercredi 1 janvier 2003 Statut Membre Dernière intervention 22 juin 2007
22 juin 2007 à 12:31
salut
quelcun à essayer l'exemple ? :)
pour moi il me génère des fautes et surtous au niveau de cette ligne :
Fop fop = new Fop(MimeConstants.MIME_PDF);
j'utilise java 6
et fop 0.2.5
que dois-je faire
merciiii
Asnidren Messages postés 4 Date d'inscription mercredi 13 septembre 2006 Statut Membre Dernière intervention 28 novembre 2006
14 sept. 2006 à 11:18
Voici le site de FOP
http://xmlgraphics.apache.org/fop/
sebdijon Messages postés 1 Date d'inscription jeudi 27 janvier 2005 Statut Membre Dernière intervention 15 janvier 2006
15 janv. 2006 à 20:42
Pour la version 0.91 il n'y a plus de import org.apache.fop.apps.Driver;

Alors voilà l'exemple que j'ai trouvé :


/*
* Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* $Id: ExampleXML2PDF.java 332791 2005-11-12 15:58:07Z jeremias $ */

package embedding;

//Java
import java.io.File;
import java.io.OutputStream;

//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;

//FOP
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.MimeConstants;

/**
* This class demonstrates the conversion of an XML file to PDF using
* JAXP (XSLT) and FOP (XSL-FO).
*/
public class ExampleXML2PDF {

/**
* Main method.
* @param args command-line arguments
*/
public static void main(String[] args) {
try {
System.out.println("FOP ExampleXML2PDF\n");
System.out.println("Preparing...");

// Setup directories
File baseDir = new File(".");
File outDir = new File(baseDir, "out");
outDir.mkdirs();

// Setup input and output files
File xmlfile = new File(baseDir, "xml/xml/projectteam.xml");
File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
File pdffile = new File(outDir, "ResultXML2PDF.pdf");

System.out.println("Input: XML (" + xmlfile + ")");
System.out.println("Stylesheet: " + xsltfile);
System.out.println("Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("Transforming...");

// Construct fop with desired output format
Fop fop = new Fop(MimeConstants.MIME_PDF);

// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);

try {
fop.setOutputStream(out);

// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

// Set the value of a in the stylesheet
transformer.setParameter("versionParam", "2.0");

// Setup input for XSLT transformation
Source src = new StreamSource(xmlfile);

// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());

// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}

System.out.println("Success!");
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
}
posset Messages postés 1 Date d'inscription vendredi 29 avril 2005 Statut Membre Dernière intervention 29 avril 2005
29 avril 2005 à 11:29
Bonjour,

je suis utilisateur de fop mais pour l'exploiter complètement je recherche la javadoc mais je la trouve pas.

Est ce que quelqu'un sait où je peux la télécharger?

Merci par avance
obigero Messages postés 8 Date d'inscription mercredi 30 mars 2005 Statut Membre Dernière intervention 9 septembre 2005
14 avril 2005 à 11:16
comment on transforme un fichier texte en ficheir xml ? c possible ? en gros je dois faire un traitement de texte en java et utiliser FOP ... et je sais pas si je dois programmer en jaja et transformer en xml apres ou directeement travailler en xml. les 2 sont possibles ?
cs_annalou Messages postés 11 Date d'inscription mardi 29 juin 2004 Statut Membre Dernière intervention 8 novembre 2007
5 juil. 2004 à 13:50
salut.

j'utilise ce programme mais je voudrais savoir comment faire pour ne pas afficher les messages suivants

[ERROR] Logger not set
[INFO] building formatting object tree
[WARNING] Screen logger not set - Using ConsoleLogger.
[INFO] setting up fonts
[INFO] [1]
[INFO] [2]
[INFO] [3]
[INFO] [4]
[INFO] [5]
[INFO] [6]
[INFO] [7]
[INFO] [8]
[INFO] [9]
[INFO] Parsing of document complete, stopping renderer

Merci
eddie2070 Messages postés 1 Date d'inscription mercredi 28 mai 2003 Statut Membre Dernière intervention 14 mai 2004
14 mai 2004 à 15:38
J utilise actuellement ce source, mais j aurai voulu savoir comment récuperer mon fichier xml et xsl directement dans le buffer, et non en donnant son $chemin d'accès.

Par exemple j ai converti les infos d'un écran(une page d'adresses par exemple) dans mon xml, mais celui-ci n est pas sur disque, et j aimerai le convertir en pdf, donc en le recuperant ds le buffer ou autre, et non en accedant a un fichier ( hou j me répéte là ^^ )
Merci

eddie2070
dmothes Messages postés 56 Date d'inscription mardi 17 décembre 2002 Statut Membre Dernière intervention 11 novembre 2005
10 févr. 2004 à 11:51
merci!, c'est juste ce que jecherchais!!!
Pasqwal Messages postés 2 Date d'inscription lundi 10 mars 2003 Statut Membre Dernière intervention 2 décembre 2003
2 déc. 2003 à 16:33
Je te présente mes excuses, manifestement j'ai eu une réaction trop hative. C'est juste que j'ai vu rouge en voyant cela, mais apparement tu ne fais pas partie de cette catégorie de pseudo-développeurs. Honte sur moi.

Tu as raison sur le but du site, il ne s'agit pas de montrer que l'on sait développer mais bien de s'entraider. Et au nom de cette entraide, j'aimerais que tu n'enlèves pas le tout. Beaucoup de gens pourrait profiter de tes recherches.

Encore une fois lolofx, je te renouvelle mes plus plates excuses.

Amicalement,
Pasqwal.
lolofx Messages postés 17 Date d'inscription mardi 14 janvier 2003 Statut Membre Dernière intervention 6 mai 2004
2 déc. 2003 à 13:15
voila, chose faite, g mis la license, je savais pas qu"il y en avait une.
Pr monsieur ki crois que g fais ca pr me faire mousser, non, désolé, pr moi ces sites, sont des sites d'entraides, ayant eu d difficultés pr faire mon pdf, je trouver bien de le mettre sur le site voila c tout.
je mets pas d sources et montrer que je c développer.
Mais si toi c ton but de montrer ce que tu c faire ben tant mieux.

enfin c rectifier, g mis la license.
et j'espere que comme pr moi, ce code aidera ceux ki en ont besoin sans chercher pdt des heures.

et merci a dobel de m'avoir dit k'il y avait une license
Pasqwal Messages postés 2 Date d'inscription lundi 10 mars 2003 Statut Membre Dernière intervention 2 décembre 2003
2 déc. 2003 à 01:42
rhalala, c'est honteux de faire ça.
Les gens de l'OpenSource méritent notre reconnaissance.
Et ça c'est un sale coup. Et se faire mousser grâce au travail d'un autre, c'est pitoyable, vraiment aucune fièrté.
Et du coup, tu prends les habitués de ce sites pour des idiots.
he ben, ça doit être beau le reste de "tes" sources.
alex1er Messages postés 39 Date d'inscription jeudi 11 avril 2002 Statut Membre Dernière intervention 5 juin 2006
27 oct. 2003 à 15:18
Merci
cs_Dobel Messages postés 333 Date d'inscription dimanche 25 mai 2003 Statut Membre Dernière intervention 23 novembre 2009 1
24 oct. 2003 à 18:29
Euh tiens, il me semble avoir déjà vu ce code quelque part...
ah oui mais il y avait une license au début si je m'en rappelle bien...
fichier ExampleXML2PDF.java dans les exemples de l'API FOP
;-((

l'original :


/*
* $Id$
* ============================================================================
* The Apache Software License, Version 1.1
* ============================================================================
*
* Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
* include the following acknowledgment: "This product includes software
* developed by the Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself, if
* and wherever such third-party acknowledgments normally appear.
*
* 4. The names "FOP" and "Apache Software Foundation" must not be used to
* endorse or promote products derived from this software without prior
* written permission. For written permission, please contact
* apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache", nor may
* "Apache" appear in their name, without prior written permission of the
* Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ============================================================================
*
* This software consists of voluntary contributions made by many individuals
* on behalf of the Apache Software Foundation and was originally created by
* James Tauber <jtauber@jtauber.com>. For more information on the Apache
* Software Foundation, please see <http://www.apache.org/>.
*/
package embedding;

//Java
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;

//Avalon
import org.apache.avalon.framework.ExceptionUtil;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;

//FOP
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;

/**
* This class demonstrates the conversion of an XML file to PDF using
* JAXP (XSLT) and FOP (XSL:FO).
*/
public class ExampleXML2PDF {

public void convertXML2PDF(File xml, File xslt, File pdf)
throws IOException, FOPException, TransformerException {
//Construct driver
Driver driver = new Driver();

//Setup logger
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);

//Setup Renderer (output format)
driver.setRenderer(Driver.RENDER_PDF);

//Setup output
OutputStream out = new java.io.FileOutputStream(pdf);
try {
driver.setOutputStream(out);

//Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslt));

//Setup input for XSLT transformation
Source src = new StreamSource(xml);

//Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(driver.getContentHandler());

//Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
}


public static void main(String[] args) {
try {
System.out.println("FOP ExampleXML2PDF
");
System.out.println("Preparing...");

//Setup directories
File baseDir = new File(".");
File outDir = new File(baseDir, "out");
outDir.mkdirs();

//Setup input and output files
File xmlfile = new File(baseDir, "xml/xml/projectteam.xml");
File xsltfile = new File(baseDir, "xml/xslt/projectteam2FO.xsl");
File pdffile = new File(outDir, "ResultXML2PDF.pdf");

System.out.println("Input: XML (" + xmlfile + ")");
System.out.println("Stylesheet: " + xsltfile);
System.out.println("Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("Transforming...");

ExampleXML2PDF app = new ExampleXML2PDF();
app.convertXML2PDF(xmlfile, xsltfile, pdffile);

System.out.println("Success!");
} catch (Exception e) {
System.err.println(ExceptionUtil.printStackTrace(e));
System.exit(-1);
}
}
}
alex1er Messages postés 39 Date d'inscription jeudi 11 avril 2002 Statut Membre Dernière intervention 5 juin 2006
27 juin 2003 à 11:36
Salut
J'ai entendu parler de FOP et ton code à l'air bien.
Pourrais tu m'envoyer par mail en fichier XML et XSL pour voir la tete qu'ils ont et pour tester ton programme.

MErci pour ta source
Rejoignez-nous