Question

here i am again :D in my j2ee web project,i want to generate my data from a jsp page in a pdf file .. i'm using Myeclipse so i'm usign the itexte library :D in a simple java project it works but in my java web project it doesn't .. i don't have any error everything seems fine but the file is not created !!

so here is the class generating the file pdf:

package mesClasses;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.List;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;


public class Genererpdf {

public Genererpdf() {   
    //constructeur
}

public void generer(String cc, String c, String p, String q, String px) {


    try {
        File file = new File("facture.pdf");
        FileOutputStream fileout = new FileOutputStream(file);
        Document document = new Document();
        PdfWriter.getInstance(document, fileout);
        document.addAuthor("Asuce Tech");
        document.addTitle("Facture de la commande");

        document.open();

        Chunk chunk = new Chunk("iText Test");
        Font font = new Font(Font.COURIER);
        font.setStyle(Font.UNDERLINE);
        font.setStyle(Font.ITALIC);
        chunk.setFont(font);
        chunk.setBackground(Color.CYAN);
        document.add(chunk);

        Paragraph paragraph = new Paragraph();
        paragraph.add("Hello World");
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);            

        List list = new List(true, 15);
        list.add(cc);
        list.add(c);
        list.add(p);
        list.add(q);
        list.add(px);
        document.add(list);

        document.close();
        } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}


}

and in my jsp page that called that methode i have:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ page import="mesClasses.Genererpdf" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSF 'generePdf.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
String codeCommande=request.getParameter("cc");
String client=request.getParameter("clt");
String produit=request.getParameter("pdt");
String quantiteC=request.getParameter("qc");
String prixT=request.getParameter("px");    

Genererpdf gp = new Genererpdf();
gp.generer(codeCommande,client,produit,quantiteC,prixT);
 %>

</body>
</html>

any idea plzzzzz ????

Was it helpful?

Solution 2

okkkk guys after some researches i found the solution .. its' impossible to create a PDF file from a code in a jsp page i should use servlet with some modifications here is the link i used and it works now http://www.onjava.com/lpt/a/3924 thank you all

and here is the code i used in my servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    //recuperer les parametrse envoyés
    String code=request.getParameter("cc");
    String client=request.getParameter("clt");
    String produit=request.getParameter("pdt");
    String quantite=request.getParameter("qc");
    String prixT=request.getParameter("px");

    response.setContentType("application/pdf");

    try{
    Document doc1 = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(doc1, baos);

    doc1.addAuthor("Asuce Tech");
    doc1.addTitle("Facture de la commande ajoutée");

    doc1.open();

    //ajouter du style au PDF
    Chunk chunk = new Chunk("Facture");
    Font font = new Font(Font.COURIER);
    font.setStyle(Font.UNDERLINE);
    font.setStyle(Font.ITALIC);
    chunk.setFont(font);
    chunk.setBackground(Color.CYAN);
    doc1.add(chunk);

    Paragraph paragraph = new Paragraph();
    paragraph.add("Gestion Commercial par point de vente");
    paragraph.setAlignment(Element.ALIGN_CENTER);
    paragraph.setAlignment(Element.ALIGN_CENTER);
    doc1.add(paragraph);
    doc1.add(new Paragraph("Facture générée Le "+ new java.util.Date()));
    List list = new List(true, 15);
    list.add("Code Commande : "+code);
    list.add("Nom Client    : "+client);
    list.add("Nom Produit   : "+produit);
    list.add("Quantité commandée : "+quantite);
    list.add("Prix Total    : "+prixT);
    doc1.add(list);

        doc1.close();

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control",
                "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Content-disposition",
                "inline; filename=factureCommande.pdf" );
        response.setHeader("Pragma", "public");
            // setting the content type


    response.setContentLength(baos.size());

    ServletOutputStream sos;
    sos = response.getOutputStream();
    baos.writeTo(sos);
    sos.flush();
    }
    catch(DocumentException e) {
        throw new IOException(e.getMessage());
    }
}

and to call that sevlet i used just a simple href in my jsp page where i passed my parameters :

 <a href="generateurPdf?cc=<%=c1%>&clt=<%=clt%>&pdt=<%=nomP%>&qc=<%=q1%>&px=<%=p1%>">Facture en PDF</a>

i hope i helped someone who needs to do the same ... and Thank you

OTHER TIPS

If there are no errors, then your file has probably been created. As you specified a relative file path, it would have been created in the folder from which the server is launched. For example, if you're running MyEclipse 2014 and using the in-built Tomcat 7 server, the file would likely be in the plugins\com.genuitec.eclipse.easie.tomcat7.myeclipse_11.5.0.me201310302042\tomcat\bin folder under the folder in which MyEclipe 2014 is installed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top