当前位置: 首页 > 知识库问答 >
问题:

应用部署后如何打印pdf格式

屈畅
2023-03-14

我是编程的初学者,我正在使用eclipse kepler(包括WindowBuilder)。我有一个问题,在我的应用程序中,在部署之前,我从数据库(MySQl)打印pdf格式的数据,它运行良好,但部署之后(使用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);}

                }







        }
    });

共有1个答案

扶隐水
2023-03-14

路径才是问题所在。

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);
 类似资料:
  • 我必须使用java应用程序打印PDF文件。我尝试过这样的方法: 当我在假打印机上测试时(我使用PDFCreator作为打印机),一切正常,但当我尝试在物理打印机上打印时,什么都没有发生。 然后我用了PDFBox。文件是打印出来的,但文字之间有奇怪的点,在不应该的地方。 那么,也许有人有从java应用程序打印PDF的经验,可以分享这些信息?

  • 问题内容: 我想使用PDFBox打印 由iText创建的 PDF文件 。我已经使用PDDocument类及其方法print()成功尝试了此操作。您可以在此处找到文档: http //pdfbox.apache.org/apidocs/。 (我正在使用此代码:) 方法print()很好用,但是 有一个问题:当我需要打印多个文件时,该方法要求我为每个文档选择打印机。 有什么办法只能设置一次打印机吗?

  • 我已经用所有3种可用的字体试过了;TimesNewRoman、Courier和Helvitica三家公司都出现了相同的例外情况。 但是当您查看apcahe pdfBox源代码中的WIN_ANSI_ENCODING_TABLE时, 我们可以看到定义了下面的非破缺空间。 在pdf规范文档中也引用了以下内容: 空格字符在MacRomanEncoding中也应编码为312,在WinAnsiEncoding

  • 我有一个连接到CUPS的打印机,它支持双面打印,如何通过java例程将其设置为单面打印或双面打印? 我曾尝试使用它的库使用ASET添加和addViewer首选项没有任何运气。 有人能提供一些建议吗?

  • 我目前的工作是创建机械图纸,用于发送给客户和作为施工图。当我的绘图完成后,我导出一个. pdf文件,并将其发送给客户端。 我们的客户非常喜欢黑白画,所以我试着提供他们。但是我用来画画的软件效果不好。它只有一个选项“所有颜色都是黑色”,我的画上有一些白色的“隐藏线”。当然,这些显示使用所有颜色作为黑色选项。 我找到了一个解决方案,那就是使用pdf打印机。效果很好,效果也很好。 现在我想打印这个。pd

  • 使用飞碟以PDF格式打印图像。 超文本标记语言代码: 当我使用飞碟将HTML转换为PDF时。生成的PDF不会打印图像。 我需要使用任何特定的软件包来打印PDF格式的图像吗。如果你有任何问题,请告诉我。