【问题标题】:How to print in pdf after deployed application部署应用程序后如何以pdf格式打印
【发布时间】:2013-07-13 03:11:24
【问题描述】:

我是编程的初学者,我正在使用包括 WindowBuilder 在内的 eclipse kepler 我有一个问题,在我的应用程序中,在部署之前我以 pdf 格式从数据库 (MySQl) 打印数据,它运行良好,但在部署之后(使用 Lauch4j 和 InnoSetup编译器)这是不可能的,当我点击打印按钮时没有反应!你能帮我吗? 这是我用来写pdf的功能代码

public void imprimerFacture() 抛出 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();

}

我在这个按钮中调用的函数

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);}

                }







        }
    });

【问题讨论】:

  • java.awt.Desktop.getDesktop().open(new File("src/images/facture.pdf"));在此处捕获异常并查看原因。
  • 你应该创建一些日志结构来了解发生了什么。
  • 我的意思是至少打印异常。如果您作为 EXE 文件运行,那么您将没有控制台,因此请尝试作为可运行的 jar 运行。

标签: java


【解决方案1】:
java.awt.Desktop.getDesktop().open(new File("src/images/facture.pdf"));

我认为问题就在这里。当您部署为 jar 文件时,您的资源无法像那样被访问。

您应该使用 getResource。 Reference

你可以试试

String path =Yourclass.class.getResource("/images/facture.pdf").getPath();
java.awt.Desktop.getDesktop().open(new File(path));

【讨论】:

  • 嗨 Makky,当我写这段代码时 java.awt.Desktop.getDesktop().open(path); 它是错误的 类型 Desktop 中的方法 open(File) 不适用于参数(字符串)请问我该怎么办?
  • 嗨,Makky,我正在使用从 Joop Eggen 获取的代码,现在问题已经解决了
【解决方案2】:

路径是问题。

PdfWriter.getInstance(document, new FileOutputStream("src/images/facture.pdf"));

最好保留您自己的目录用于读/写数据,通常在用户的家中。 您还可以创建一个临时文件(请参阅文件)。

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);

【讨论】:

  • 嗨乔普·埃根?我写了这段代码,但没有反应请问我该怎么办?
  • Vous n'oublez pas open(pdfFile)?我还添加了文件的用法。
  • HI Joop Eggen? 只是说声谢谢,现在它运行良好,但我想在我的应用程序中问你我可以使用此代码打印但我不想要一个对话框有任何代码做吗?我想,如果我点击打印按钮自动打印发票而不看到对话框
  • 这是我用来打印的代码public void actionPerformed(ActionEvent arg0) { if(!booleanScan) { PrintJob job=getToolkit().getPrintJob(fen,"Impression",null); if (job != null){ 图形 g = job.getGraphics(); if(g != null){ ecran.printAll(g); g.dispose(); } 工作.end(); }
  • 还有Desktop.getDesktop().print(pdfFile)。当然我会用在actionPerformed:SwingUtilities.invokeLater;搜索invokeLater了解详情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 2019-08-24
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
相关资源
最近更新 更多