我又来了:D 在我的j2ee web项目中,我想从pdf文件中的jsp页面生成我的数据..我正在使用Myeclipse所以我使用了itexte库:D 在一个简单的java项目中,它可以工作,但在我的java web项目中它没有..我没有任何错误一切似乎都很好,但文件没有创建!!
所以这里是生成文件的类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();
}
}
}
并在我调用该方法的jsp页面中:
<%@ 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>
任何想法plzzzzz ????
答案 0 :(得分:0)
如果没有错误,则可能已创建了您的文件。在指定相对文件路径时,它将在启动服务器的文件夹中创建。例如,如果您正在运行MyEclipse 2014并使用内置的Tomcat 7服务器,则该文件可能位于plugins \ com.genuitec.eclipse.easie.tomcat7.myeclipse_11.5.0.me201310302042 \ tomcat \ bin文件夹下安装MyEclipe 2014的文件夹。
答案 1 :(得分:0)
这是我在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());
}
}
并且调用那个sevlet我在我的jsp页面中只使用了一个简单的href,我传递了我的参数:
<a href="generateurPdf?cc=<%=c1%>&clt=<%=clt%>&pdt=<%=nomP%>&qc=<%=q1%>&px=<%=p1%>">Facture en PDF</a>
我希望我能帮助那些需要做同样事情的人...... 谢谢你