shark59112
Messages postés7Date d'inscriptionjeudi 17 décembre 2009StatutMembreDernière intervention22 décembre 2009
-
19 déc. 2009 à 18:28
kindingjoel
Messages postés1Date d'inscriptionmardi 3 novembre 2009StatutMembreDernière intervention 7 juin 2010
-
7 juin 2010 à 13:08
Bonjour,
Je souhaite créer un .PDF à partir d'un fichier XML mais je ne sais pas du tout comment m'y prendre... =x (projet)
J'ai alors effectué des recherches intensive sur le net où j'ai pu dénicher pas mal d'information (fop, xsl, xsl:fo, etc...) mais aucun tutoriel partant de A à Z.
Pour commencer, voici mon fichier .xml de base -->
Après beaucoup de recherche, j'ai lu qu'il fallait créer un xsl pour mettre en forme le xml j'ai donc codé ceci en regardant sur le net -->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
version="1.0" >
<!-- rule for the whole document: root element is page -->
<xsl:template match="channel">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<!-- Definition of a single master page. It is simple (no headers etc.) -->
<fo:simple-page-master
master-name="first"
margin-left="2cm" margin-right="2cm"
margin-bottom="0.5cm" margin-top="0.75cm"
page-width="21cm" page-height="29.7cm"
>
<!-- required element body -->
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<!-- Definition of a page sequence -->
<fo:page-sequence master-reference="first">
<fo:flow flow-name="xsl-region-body" font-size="14pt" line-height="14pt">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- A series of XSLT rules that produce fo:blocks to be inserted above -->
<xsl:template match="channel/title">
<fo:block font-size="36pt" text-align="center" line-height="40pt" space-
before="0.5cm" space-after="1.0cm">
<xsl:apply-templates/></fo:block>
</xsl:template>
<xsl:template match="link">
<fo:block text-align="justify" space-before="0.5cm">
<xsl:apply-templates/></fo:block>
</xsl:template>
<xsl:template match="description">
<fo:block font-size="12pt" text-align="start" space-before="0.7cm" font-
style="italic">
<xsl:apply-templates/></fo:block>
</xsl:template>
</xsl:stylesheet>
Tout d'abord je voudrais savoir si m'ont .xsl est "bon", car je ne sais absolument pas comment je peux tester cela! =x
Sinon, d'après ce que j'ai compris, depuis ces 2 fichier, il faut que j'établis un xsl:fo à partir de ces 2 fichiers en utilisant FOP, est-exact? Si c'est le cas, je ne vois pas du tout comment m'y prendre donc si vous pouviez m'aider cela m'aiderais vraiment beaucoup beaucoup!!!
shark59112
Messages postés7Date d'inscriptionjeudi 17 décembre 2009StatutMembreDernière intervention22 décembre 2009 22 déc. 2009 à 13:42
Ayant continué mes recherche, j'ai vu que sur le site officiel (de fop/apache) qu'il y avait le code pour créer un pdf à partir d'un xml et xsl.
Voici le code (suivi du problème):
package projet;
//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.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
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 Pdf {
/**
* 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, "rssEnregistre.xml");
File xsltfile = new File(baseDir, "rssEnregistre.xsl");
File pdffile = new File(outDir, "ResultXML2PDF.pdf");
System.out.println("Fichier instancié DONE!");
System.out.println("Input: XML (" + xmlfile + ")");
System.out.println("Stylesheet: " + xsltfile);
System.out.println("Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("Transforming...");
// configure fopFactory as desired
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent as desired
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, 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);
}
}
}
Le problème était que sur mes "import" de FOP, j'avais des erreur: "access restriction", je tiens à préciser que j'utilise Eclipse SDK!
J'ai donc vu sur le net qu'il fallais aller dans les option d'éclipse (error/warning) et mettre sur "warning" au lieu d'erreur le "Forbidden reference (acess rules)", à partir de ça, j'ai donc pu lancer le programme mais il y a un autre problème...
Voici ce que me donne ma console:
FOP ExampleXML2PDF
Preparing...
Fichier instancié DONE!
Input: XML (./rssEnregistre.xml)
Stylesheet: ./rssEnregistre.xsl
Output: PDF (./out/ResultXML2PDF.pdf)
Transforming...
Exception in thread "main" java.lang.ExceptionInInitializerError
at projet.Pdf.main(Pdf.java:54)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
at org.apache.fop.apps.FopFactory.<clinit>(FopFactory.java:64)
... 1 more
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
... 5 more
Caused by: java.lang.NullPointerException
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
... 6 more
J'ai l'impression que c'est un soucis avec les librairies, si vous pouviez m'aider à résoudre ce problème svp...