Bonjour à tous,
J'aimerais convertir un fichier HTML en PDF avec Itext, je pense que mon problème se situe dans la "complexité" des styles que j'emploie. Lors de l'exécution de ma méthode save qui prend en paramètre un fichier html, aucun de mes styles n'est appliqué. Je me demandais donc quels étaient les styles autorisés sur Itext ou alors si j'avais fait une erreur lors de la manipulation d'Itext que je viens de découvrir.
Merci d'avance pour vos conseils,
Julien
Voici ma classe RapportPdf
Ma classe Rapport
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 package creationrapport; import java.io.FileOutputStream; import java.io.FileReader; import java.util.ArrayList; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.html.simpleparser.HTMLWorker; import com.lowagie.text.html.simpleparser.StyleSheet; import com.lowagie.text.pdf.PdfWriter; /** * Cette classe sauvegarde dans un fichier PDF des rapports HTML (résultat de * SELECT sur des BDD). * * @version 1.0 (13/03/07 FR) * @author Julien GAVARD */ public class RapportPdf extends Rapport { /** * Variable contenant le chemin vers le fichier CSS des rapports. */ public String cheminCss; // CONSTRUCTEUR /** * Constructeur null. */ public RapportPdf() { super(); this.cheminCss = ""; } /** * Constructeur avec paramètre avec un nom de fichier par défaut * (defaut.pdf). * * @param chemin * contient le chemin absolu de sauvegarde du rapport */ public RapportPdf(final String chemin) { super(chemin, "defaut", "pdf"); } /** * Constructeur avec paramètre. * * @param chemin * contient le chemin absolu de sauvegarde du rapport * @param fichier * contient le nom de sauvegarde du fichier */ public RapportPdf(final String chemin, final String fichier) { super(chemin, fichier, "pdf"); this.cheminCss = ""; } /** * Constructeur avec paramètre. * * @param chemin * chemin du rapport * @param fichier * nom de fichier du rapport * @param cheminCss * chemin du fichier CSS du rapport */ public RapportPdf(final String chemin, final String fichier, final String chemCss) { super(chemin, fichier, "pdf"); this.cheminCss = chemCss; } // ACCESSEUR /** * Méthode qui renvoie l'extension d'un fichier PDF donc une chaîne * contenant pdf. * * @return Retourne une chaîne de caractère contenant l'extension d'un * fichier pdf */ public String getExtension() { return "pdf"; } // METHODE public void save(RapportHtml rapport) { Document document = new Document(); try { StyleSheet styles = new StyleSheet(); styles.loadTagStyle("caption", "margin", "auto"); styles.loadTagStyle("caption", "font-family", "Arial"); styles.loadTagStyle("caption", "font-weight", "bold"); styles.loadTagStyle("caption", "font-size", "1.2em"); styles.loadTagStyle("caption", "color", "#00900"); styles.loadTagStyle("caption", "margin-bottom", "20px"); styles.loadTagStyle("table", "margin", "auto"); //styles.loadTagStyle("table", "border", "4px outset green"); styles.loadTagStyle("table", "border-collapse", "collapse"); styles.loadTagStyle("th", "background-color", "#00600"); styles.loadTagStyle("th", "color", "white"); styles.loadTagStyle("th", "font-size", "1.1em"); styles.loadTagStyle("th", "font-family", "Arial"); //styles.loadTagStyle("td", "border", "1px solid black"); styles.loadTagStyle("td", "text-align", "center"); styles.loadTagStyle("td", "padding", "5px"); PdfWriter.getInstance(document, new FileOutputStream( this.cheminRapport + this.fileName + "." + this.extension)); document.open(); ArrayList objects; objects = HTMLWorker.parseToList(new FileReader(rapport.cheminRapport + rapport.fileName + "." + rapport.extension), styles); for (int k = 0; k < objects.size(); ++k) { document.add((Element) objects.get(k)); } } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } document.close(); } }
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 package creationrapport; import java.io.FileOutputStream; import java.io.FileReader; import java.util.ArrayList; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.html.simpleparser.HTMLWorker; import com.lowagie.text.html.simpleparser.StyleSheet; import com.lowagie.text.pdf.PdfWriter; /** * Cette classe sauvegarde dans un fichier PDF des rapports HTML (résultat de * SELECT sur des BDD). * * @version 1.0 (13/03/07 FR) * @author Julien GAVARD */ public class RapportPdf extends Rapport { /** * Variable contenant le chemin vers le fichier CSS des rapports. */ public String cheminCss; // CONSTRUCTEUR /** * Constructeur null. */ public RapportPdf() { super(); this.cheminCss = ""; } /** * Constructeur avec paramètre avec un nom de fichier par défaut * (defaut.pdf). * * @param chemin * contient le chemin absolu de sauvegarde du rapport */ public RapportPdf(final String chemin) { super(chemin, "defaut", "pdf"); } /** * Constructeur avec paramètre. * * @param chemin * contient le chemin absolu de sauvegarde du rapport * @param fichier * contient le nom de sauvegarde du fichier */ public RapportPdf(final String chemin, final String fichier) { super(chemin, fichier, "pdf"); this.cheminCss = ""; } /** * Constructeur avec paramètre. * * @param chemin * chemin du rapport * @param fichier * nom de fichier du rapport * @param cheminCss * chemin du fichier CSS du rapport */ public RapportPdf(final String chemin, final String fichier, final String chemCss) { super(chemin, fichier, "pdf"); this.cheminCss = chemCss; } // ACCESSEUR /** * Méthode qui renvoie l'extension d'un fichier PDF donc une chaîne * contenant pdf. * * @return Retourne une chaîne de caractère contenant l'extension d'un * fichier pdf */ public String getExtension() { return "pdf"; } // METHODE public void save(RapportHtml rapport) { Document document = new Document(); try { StyleSheet styles = new StyleSheet(); styles.loadTagStyle("caption", "margin", "auto"); styles.loadTagStyle("caption", "font-family", "Arial"); styles.loadTagStyle("caption", "font-weight", "bold"); styles.loadTagStyle("caption", "font-size", "1.2em"); styles.loadTagStyle("caption", "color", "#00900"); styles.loadTagStyle("caption", "margin-bottom", "20px"); styles.loadTagStyle("table", "margin", "auto"); //styles.loadTagStyle("table", "border", "4px outset green"); styles.loadTagStyle("table", "border-collapse", "collapse"); styles.loadTagStyle("th", "background-color", "#00600"); styles.loadTagStyle("th", "color", "white"); styles.loadTagStyle("th", "font-size", "1.1em"); styles.loadTagStyle("th", "font-family", "Arial"); //styles.loadTagStyle("td", "border", "1px solid black"); styles.loadTagStyle("td", "text-align", "center"); styles.loadTagStyle("td", "padding", "5px"); PdfWriter.getInstance(document, new FileOutputStream( this.cheminRapport + this.fileName + "." + this.extension)); document.open(); ArrayList objects; objects = HTMLWorker.parseToList(new FileReader(rapport.cheminRapport + rapport.fileName + "." + rapport.extension), styles); for (int k = 0; k < objects.size(); ++k) { document.add((Element) objects.get(k)); } } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } document.close(); } }
Partager