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

Documents Java Discussion :

generation d'un pdf à partir d'un formulaire utilisateur


Sujet :

Documents Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    rs
    Inscrit en
    Novembre 2016
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : rs

    Informations forums :
    Inscription : Novembre 2016
    Messages : 15
    Points : 10
    Points
    10
    Par défaut generation d'un pdf à partir d'un formulaire utilisateur
    Bonjour à tous,

    J'ai créé un formulaire où l'utilisateur remplit des champs (nom, prenom, nombre, date1, date2 et à le choix entre 3 propositions : homme, femme, enfants)
    Je souhaiterais pouvoir stocker ces champs (pour les mettre dans une base de données) et les incorporer dans un fichier pdf

    mais je n'y arrive pas à faire quelque chose de fonctionnel pour mon pdf
    comment faire? avez vous des conseils, des exemples à me proposer?
    faut il que je cree un template pdf avant et qu'il soit rempli au fur et a mesure par l'utilisateur?
    c'est ce que j'ai essayé de faire avec 'itextpdf' mais sans succès
    une autre stratégie?

    help me!

    ci joint mes codes :

    fichier data

    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
    public class Data {
     
     
    	private String name;
    	private String surname;
    	private Integer numb;
    	private String date1;
    	private String date2;
     
    	public Patient(String name, String surname, Integer numb, String date1, String date2) {
    		super();
    		this.name = name;
    		this.surname = surname;
    		this.numb = numb;
    		this.date1 = date1;
    		this.date2 = date2;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public String getSurname() {
    		return surname;
    	}
     
    	public Integer getNumb() {
    		return numb;
    	}
     
    	public String getDate1() {
    		return date1;
    	}
     
    	public String getDate2() {
    		return date2;
    	}
     
    }
    fichier dataManager



    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
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
     
    import main;
    import models.data;
     
    public class DataManager {
     
     
    	private Main main;
     
    	public DataManager(Main main) {
    		super();
    		this.main = main;
    	}
     
    	public void addData(data p) {
    		String query = "INSERT INTO Data(name, surname, numb, date1, date2) VALUES ("
    				+ "\"" + p.getName() + "\", "
    				+ "\"" + p.getSurname() + "\", "
    				+ "\"" + p.getNumb() + "\", "
    				+ "\"" + p.getDate1() + "\", "
    				+ "\"" + p.getDate2() + "\");";
    		try {
    			this.main.getDatabase().connect();
    			this.main.getDatabase().updateValue(query);
    		}
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    		finally {
    			this.main.getDatabase().disconnect();
    		}
    	}
     
    	public ArrayList<Data> getData() {
    		ArrayList<Data> data = new ArrayList<Data>();
     
    		String query = "SELECT name, surname, numb, Date1, Date2 FROM Data ORDER BY name";
    		try {
    			this.main.getDatabase().connect();
    			ResultSet rslt = this.main.getDatabase().getResultOf(query.toString());
    			while (rslt.next()) {
    				data.add(new Data(rslt.getString("name"), rslt.getString("surname"), rslt.getInt("numb"),rslt.getString("Date1"),rslt.getString("Date2")));
    			}
    			rslt.close();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		finally {
    			this.main.getDatabase().disconnect();
    		}
    		return data;
    	}
    fichier options


    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
     
    public class Options {
     
     
    	public enum Ref {
    		homme("homme"),
    		femme("femme"),
    		enfant("enfant");
     
    		private final String refName;
     
    		Ref(String goalName){
    			this.refName = goalName;
    		}
     
    		@Override
    		public String toString() {
    			return refName;
    		}
    	}
    	private Ref ref;
     
    	public Ref getRef() {
    		return ref;
    	}
     
     
    }





    fichier formulaireController


    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
    import Main;
    import models.options;
     
     
     
    public class FormulaireController{
     
    	Map<String,Ref> refsMap = new HashMap<String,Ref>();
     
        private Main main;
     
        private JFrame frame;
     
     
        TextArea ta = new TextArea();
     
        public void setMain(Main main) {
            this.main = main;
        }
     
    	@FXML
    	private TextField tfCharName;
     
    	@FXML
    	private TextField tfCharSurname;
     
    	@FXML
    	private TextField tfNum;
     
    	@FXML
    	private TextField tfDate1;
     
    	@FXML
    	private TextField tfDate2;
     
    	@FXML
    	private ChoiceBox<String> cbGoal;
     
    	@FXML
    	private Button bChoose;
     
    	@FXML
    	private Button bFinish;
     
    	@FXML
    	private String eFinish;
     
     
     
    	@FXML public void initialize(){
     
        Ref[] refs = Ref.values();
    	for (Ref ref : refs) {
    		refsMap.put(ref.toString(), ref);
    	}
     
    	cbGoal.setItems(FXCollections.observableArrayList(refsMap.keySet()));
     
    	}
     
    	@FXML
    	public void handleButtonAction(ActionEvent event) throws IOException{
    	    Button pressedButton = (Button) event.getSource();
     
    		if(pressedButton==bChoose){
    			FileChooser fileChooser = new FileChooser();
    			File selectedFile = fileChooser.showOpenDialog(null);
    			if (selectedFile != null) {
     
     
     
    	                try {
    	                    ta.setText(new String(Files.readAllBytes(selectedFile.toPath())));
    	                } catch (IOException ex) {}
    			}
     
    		}
     
     
    		else if(pressedButton==bFinish){
     
    			//fields
    			String name = tfCharName.getText().trim();
    			String surname = tfCharSurname.getText().trim();
    			String numb = tfNum.toString().trim();
    			String date1 = tfDate1.getText().trim();
    			String date2 = tfDate2.getText().trim();
     
    //					AddToReport field = new AddToReport (name+" "+surname+" "+number+" "+date1+" "+date2);
    //					AddToReport field = new AddToReport ();
    //					this.dataManager.updateData(new Data(this.name, surname, number, date1, date2));
    //					Button pressedButton = (Button) event.getSource();
    					Stage mainStage = (Stage) pressedButton.getScene().getWindow();
    					FXMLLoader loader = new FXMLLoader();
    	 	             loader.setLocation(MainApp.class.getResource("../view/suite.fxml"));
     
    	 	             AnchorPane page = (AnchorPane) loader.load();
    	 	             Stage dialogStage = new Stage();
    	 	             dialogStage.setTitle("suite");
    	 	             dialogStage.initModality(Modality.WINDOW_MODAL);
    	 	             Window primaryStage = null;
    					dialogStage.initOwner(primaryStage);
    	 	             Scene scene = new Scene(page);
    	 	             dialogStage.setScene(scene);
     
    	 	             dialogStage.show();
     
    	}
    	}
    }

    fichier formulaire.fxml

    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
    <?xml version="1.0" encoding="UTF-8"?>
     
    <?import javafx.scene.chart.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.ChoiceBox?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.control.RadioButton?>
    <?import javafx.scene.control.ScrollPane?>
    <?import javafx.scene.control.Slider?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.control.ToggleGroup?>
    <?import javafx.scene.image.ImageView?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.VBox?>
    <?import javafx.scene.text.Font?>
     
     
    <VBox alignment="TOP_CENTER" prefHeight="550.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.FormulaireController">
     
       <children>
    		<Label alignment="CENTER" contentDisplay="CENTER" text="New analysis" textAlignment="CENTER" underline="true">
    			<font>
    				<Font size="29.0" />
    			</font>
    			<VBox.margin>
    				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    			</VBox.margin>
    		</Label>
          <ScrollPane>
             <content>
                <VBox alignment="BOTTOM_CENTER">
                	<children>
                		<HBox fillHeight="false" VBox.vgrow="ALWAYS">
                			<VBox.margin>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</VBox.margin>
                			<children>
                				<Label text="Enter patient name and surname" />
                				<TextField fx:id="tfCharSurname" promptText="Name">
                					<HBox.margin>
                						<Insets left="5.0" right="5.0" />
                					</HBox.margin>
                				</TextField>
                				<TextField fx:id="tfCharSurname" promptText="Surname" />
                			</children>
                			<padding>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</padding>
                		</HBox>
     
                		<HBox fillHeight="false" VBox.vgrow="ALWAYS">
                			<VBox.margin>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</VBox.margin>
                				<Label text="number" />
                				<TextField fx:id="tfNum" promptText="number">
                					<HBox.margin>
                						<Insets left="5.0" right="5.0" />
                					</HBox.margin>
                				</TextField>
                			<padding>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</padding>
                		</HBox>
                		<HBox fillHeight="false" VBox.vgrow="ALWAYS">
                			<VBox.margin>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</VBox.margin>
                				<Label text="date1" />
                				<TextField fx:id="tfDate1" promptText="date1">
                					<HBox.margin>
                						<Insets left="5.0" right="5.0" />
                					</HBox.margin>
                				</TextField>
                			<padding>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</padding>
                		</HBox>
                		<HBox fillHeight="false" VBox.vgrow="ALWAYS">
                			<VBox.margin>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</VBox.margin>
                				<Label text="date2" />
                				<TextField fx:id="tfDate2" promptText="date2">
                					<HBox.margin>
                						<Insets left="5.0" right="5.0" />
                					</HBox.margin>
                				</TextField>
                			<padding>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</padding>
                		</HBox>
                		<HBox fillHeight="false" VBox.vgrow="ALWAYS">
                			<children>
                				<Label text="Choisir">
                					<HBox.margin>
                						<Insets right="10.0" />
                					</HBox.margin>
                				</Label>
                				<ChoiceBox fx:id="cbGoal" prefWidth="150.0" />
                			</children>
                			<VBox.margin>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</VBox.margin>
                			<padding>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</padding>
                		</HBox>
                		<HBox fillHeight="false" VBox.vgrow="ALWAYS">
                		   			<children>
                				<Label text="Choose input file">
                					<HBox.margin>
                						<Insets right="10.0" />
                					</HBox.margin>
                				</Label>
                		<Button fx:id="bChoose" alignment="CENTER" mnemonicParsing="false" onAction="#handleButtonAction" text="choose">
    			        </Button>
                			</children>
                			<VBox.margin>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</VBox.margin>
                			<padding>
                				<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                			</padding>
                		</HBox>
     
                	</children>
                	<padding>
                		<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
                	</padding>
                </VBox>
             </content>
          </ScrollPane>
     
                		<Button fx:id="bFinish" alignment="CENTER" mnemonicParsing="false" onAction="#handleButtonAction" text="suite">
             <VBox.margin>
                <Insets top="10.0" />
             </VBox.margin>
          </Button>
          <Label fx:id="eFinish" alignment="CENTER" textFill="#ee0404">
             <VBox.margin>
                <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
             </VBox.margin>
          </Label>
       </children>
    </VBox>

  2. #2
    Membre confirmé
    Avatar de provirus
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Février 2009
    Messages
    248
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Canada

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2009
    Messages : 248
    Points : 580
    Points
    580
    Par défaut
    Bonjour,

    pour générer des PDF, j'utilise Flying Saucer (https://github.com/flyingsaucerproject/flyingsaucer )

    Ce que vous devez faire:
    • Générer un fichier html avec son css (peut utiliser Freemarker)
    • Donner ce HTML à Flying Saucer pour qu'il le transforme en PDF


    Amusez-vous bien

  3. #3
    Membre à l'essai
    Homme Profil pro
    rs
    Inscrit en
    Novembre 2016
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : rs

    Informations forums :
    Inscription : Novembre 2016
    Messages : 15
    Points : 10
    Points
    10
    Par défaut
    Bonjour et merci pour votre réponse!

    J'ai du mal à visualiser comment générer le fichiers html + css attendus à partir de javafx

    Merci

    Martin

  4. #4
    Membre confirmé
    Avatar de provirus
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Février 2009
    Messages
    248
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Canada

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Février 2009
    Messages : 248
    Points : 580
    Points
    580
    Par défaut
    Bonjour,

    peu importe que tu sois dans javafx, dans un servlet ou autre, tant que tu peux générer du HTML tu es correct.

    Tu pourrais techniquement créer un String qui a tout le HTML dedans (incluant le style CSS dans le header).

    Si tu as un gros HTML, je te suggère de prendre un moteur de template (je ne sais pas vraiment comment on appelle ça en français...) comme Freemarker.
    Dans ce cas, tu:
    • Crée un fichier html avec les variables, les loops et autres outils de Freemarker comme resource.
    • Crée une Map avec les valeurs des variables que tu veux passer à ta resource Freemarker.
    • Tu demandes à Freemarker de te donner le String qui est le fichier resource une fois processé. Regarde cette petitte Classe pour un exemple https://github.com/foilen/java-libra...rkerTools.java


    Bonne journée

Discussions similaires

  1. [EZPDF] générer du pdf à partir d'un formulaire
    Par Shyboy dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 03/05/2007, 20h19
  2. Ouvrir un fichier pdf à partir d'un formulaire
    Par loul404 dans le forum IHM
    Réponses: 4
    Dernier message: 19/03/2007, 13h22
  3. [ezPDF] Générer du PDF à partir d'un formulaire HTML
    Par Shyboy dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 08/08/2006, 11h33
  4. créer un pdf à partir d'un formulaire
    Par PrinceMaster77 dans le forum ASP
    Réponses: 4
    Dernier message: 04/05/2006, 22h38
  5. Réponses: 27
    Dernier message: 16/09/2005, 17h40

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