Reputation: 1
I'm beginner in programming, I'm using eclipse kepler including WindowBuilder I have a problem, in my application, before deployed I print data from database (MySQl) in pdf it's running well but after deployed(using Lauch4j and InnoSetup Compiler) it 's not possible, when I click on print button no reaction! can you help me please? this is function code I'm using to write in pdf
public void imprimerFacture() throws DocumentException {
Document document = new Document(PageSize.A4);
Date str = new Date();
SimpleDateFormat sdt = new SimpleDateFormat("dd/MM/yyyy");
String strdat = sdt.format(str);
Table tableau = new Table(8,2);
try {
PdfWriter.getInstance(document, new FileOutputStream("src/images/facture.pdf"));
document.open()
Paragraph phrase = new Paragraph(new Chunk("Le "+strdat,FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE)).setBackground(Color.yellow));
phrase.setAlignment(Element.ALIGN_TOP);
phrase.setAlignment(Element.ALIGN_RIGHT);
document.add(phrase);
Paragraph ftitre = new Paragraph("Magasin :...........",FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE));
//ftitre.setUnderline(2f, -5f);
ftitre.setAlignment(Element.ALIGN_TOP);
ftitre.setAlignment(Element.ALIGN_LEFT);
document.add(ftitre);
Paragraph ftitre1 = new Paragraph("Adresse :...........",FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE));
//ftitre.setUnderline(2f, -5f);
ftitre1.setAlignment(Element.ALIGN_TOP);
ftitre1.setAlignment(Element.ALIGN_LEFT);
document.add(ftitre1);
Paragraph ftitre2 = new Paragraph("Téléphone :...........",FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, Color.BLUE));
//ftitre.setUnderline(2f, -5f);
ftitre2.setAlignment(Element.ALIGN_TOP);
ftitre2.setAlignment(Element.ALIGN_LEFT);
document.add(ftitre2);
Chunk chunk = new Chunk("Commandes disponibles",FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, Color.BLUE));
chunk.setUnderline(0.2f,-2f);
//chunk.ALIGN_CENTER
document.add(chunk);
tableau.setAutoFillEmptyCells(true);
} catch (DocumentException de) {de.printStackTrace();}
catch (IOException ioe) {ioe.printStackTrace();}
try {
Statement state = Connection_bd.getInstance().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet res = state.executeQuery("select ReferenceCommande,Designation,Quantite,DateLivraison,Categorie,MontantCommande,Date,IdentifiantClient from Commande");
//ResultSetMetaData meta = res.getMetaData();
tableau.addCell("ReferenceCommande");
tableau.addCell("Designation");
tableau.addCell("Quantite");
tableau.addCell("DateLivraison");
tableau.addCell("Categorie");
tableau.addCell("MontantCommande");
tableau.addCell("Date");
tableau.addCell("IdentifiantClient");
tableau.setWidth(110);
while(res.next()){
tableau.addCell(res.getString("ReferenceCommande"));
tableau.addCell(res.getString("Designation"));
tableau.addCell(res.getString("Quantite"));
tableau.addCell(res.getString("DateLivraison"));
tableau.addCell(res.getString("Categorie"));
tableau.addCell(res.getString("MontantCommande"));
tableau.addCell(res.getString("Date"));
tableau.addCell(res.getString("IdentifiantClient"));
}
document.add(tableau);
//on ferme le tout
res.close();
state.close();
}catch(SQLException e){}
Paragraph ftitre = new Paragraph("Magasinier",FontFactory.getFont(FontFactory.COURIER,10,Font.BOLD,Color.BLUE));
ftitre.setAlignment(Element.ALIGN_RIGHT);
document.add(ftitre);
document.close();
}
My function I called in this buttton
btnImprimer_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
int result=JOptionPane.showConfirmDialog(null,"Voulez-vous réellement imprimer ce(s) commande(s) ?","GE-SHOP1.0 SOFTWARE_MESSAGE",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.OK_OPTION)
{
String str="select * from commande";
String strin=AfficherChaine(str);
if(strin.equals(""))
{ JOptionPane.showMessageDialog(null,"Cette action ne peut pas aboutir du fait qu'il n'y a rien à imprimer!!!\n Veuillez saisir la(les) nouvelle(es) commande(es) et puis proceder a l'impression !","GE-SHOP1.0 SOFTWARE_MESSAGE",JOptionPane.WARNING_MESSAGE);}
else {try {
//if (listachat.size() > 0){
imprimerFacture();
if(Desktop.isDesktopSupported()){
if(Desktop.getDesktop().isSupported(java.awt.Desktop.Action.OPEN)){
try {
java.awt.Desktop.getDesktop().open(new File("src/images/facture.pdf"));
} catch (IOException ex) {
//Traitement de l'exception
}
catch (IllegalArgumentException e){}
} else {
JOptionPane.showMessageDialog(null, "La fonction n'est pas supportée par votre système d'exploitation","GE-SHOP1.0 SOFTWARE_MESSAGE", JOptionPane.ERROR_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(null, "Desktop pas supportée par votre système d'exploitation", "GE-SHOP1.0 SOFTWARE_MESSAGE", JOptionPane.ERROR_MESSAGE);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
else{if(result==JOptionPane.NO_OPTION)
{JOptionPane.showMessageDialog(null, "L'operation d'impression vient d'etre annulée", "GE-SHOP1.0 SOFTWARE_MESSAGE", JOptionPane.INFORMATION_MESSAGE);}
}
}
});
Upvotes: 0
Views: 465
Reputation: 17463
java.awt.Desktop.getDesktop().open(new File("src/images/facture.pdf"));
I think the problem is here. When you deploy as jar file your resources can't be accessed like that.
You should use getResource. Reference
You can try like
String path =Yourclass.class.getResource("/images/facture.pdf").getPath();
java.awt.Desktop.getDesktop().open(new File(path));
Upvotes: 0
Reputation: 109547
The path is the problem.
PdfWriter.getInstance(document, new FileOutputStream("src/images/facture.pdf"));
Better reserve you own directory for read/write data, typically in the user's home. You could also create a temporary file (see File).
File applicationDir = new File(System.getProperty("user.home")
+ File.sepator + ".GE-Shop");
applicationDir.mkdirs();
File pdfFile = new File(applicationDir, "facture.pdf");
PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
...
Desktop.getDesktop().open(pdfFile);
Upvotes: 0