Transformer un xml en pdf
bonjour,
Voilà j'essaye de creer un fichier pdf à partir d'un fichier xml.
J'ai vu qu'il y a déjà un exemple sur la FAQ mais le problème c'est que j'arrive pas à le faire marcher sur mon ordi.
Voilà, j'ai pris le meme fichier xml
Code:
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
| <?xml version="1.0" encoding="ISO-8859-1"?>
<annuaire>
<personne id="0">
<nom>nom0</nom>
<prenom>nom0</prenom>
<adresse>adresse0</adresse>
</personne>
<personne id="1">
<nom>nom1</nom>
<prenom>nom1</prenom>
<adresse>adresse1</adresse>
</personne>
<personne id="2">
<nom>nom2</nom>
<prenom>nom2</prenom>
<adresse>adresse2</adresse>
</personne>
<personne id="3">
<nom>nom3</nom>
<prenom>nom3</prenom>
<adresse>adresse3</adresse>
</personne>
<personne id="4">
<nom>nom4</nom>
<prenom>nom4</prenom>
<adresse>adresse4</adresse>
</personne>
</annuaire> |
le même fichier XSL
Code:
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
| <?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">
HAUT DE PAGE
</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>
BAS DE PAGE - <fo:page-number/> -
</fo:block>
</fo:block>
</xsl:template>
</xsl:stylesheet> |
et biensure la même classe JAVA
Code:
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
| 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 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();}
}
} |
J'ai bien installé la librairie FOP, mais au moment de la compilation j'ai des erreurs.Voilà ce qui m'affiche sur la console :
Code:
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
| [ERROR] Logger not set
javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: java.net.MalformedURLException: unknown protocol: c
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:805)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52)
Caused by: javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: java.net.MalformedURLException: unknown protocol: c
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:1002)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
... 2 more
Caused by: javax.xml.transform.TransformerException: java.net.MalformedURLException: unknown protocol: c
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:996)
... 3 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
... 3 more
---------
javax.xml.transform.TransformerConfigurationException: javax.xml.transform.TransformerException: java.net.MalformedURLException: unknown protocol: c
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:1002)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52)
Caused by: javax.xml.transform.TransformerException: java.net.MalformedURLException: unknown protocol: c
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:996)
... 3 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
... 3 more
---------
javax.xml.transform.TransformerException: java.net.MalformedURLException: unknown protocol: c
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:996)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52)
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
... 3 more
---------
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52)
---------
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52)
---------
javax.xml.transform.TransformerException: java.net.MalformedURLException: unknown protocol: c
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:996)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52)
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
... 3 more
---------
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52)
---------
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.processor.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:972)
at org.apache.xalan.processor.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:788)
at fichierMRX91.CreationPDF.creerPDF(CreationPDF.java:43)
at fichierMRX91.CreationPDF.main(CreationPDF.java:52) |
J'ai cherché un peu sur le forum, mais j'ai pas vraiment trouvé une réponse à cette question. Merci pour votre aide.
ca devrait résolver ton problème
j'avais le même soucis, en fait le paramètre doit être defini comme ca :
URI - representing the base URI. The URI should be fully resolved.
For example:
"http://www. xmlmill.com/test/" or
"file:///c:/xmlmill/test/"
dans mon cas j'avais oublié "file:///" devant mon chemin.
et ca marche :yaisse2:
PDF > XML, c'est possible?
Bonjour à tous,
je ne suis pas certain d'être au bon endroit, mais je cherche à faire l'inverse du sujet de ce TOPIC:
Transformer un PDF en XML.
Il existe des fonctions d'export dans Acrobat du PDF en XML, mais les données récupérées dans le XML sont en "vrac " (du moins il met tous les blocs à la suite sans que je puisse savoir quel bloc est sur quelle page) et j'aimerais en récupérer un peu plus...
Du style taille et positions des blocs images et textes par exemple ainsi que la page à laquelle ils appartiennent.
Quelqu'un sait il si il existe un logiciel qui réaliserait l'export du PDF vers XML avec un peu plus d'infos que l'export à partir d'Acrobat?
Ou pensez vous qu'il y a moyen de réaliser un retraitement du XML réalisé à partir d'Acrobat, et d'en tirer un peu plus que ce qu'il me donne actuellement?
D'avance, merci pour toutes vos suggestions. :D