java程序单独运行正确,用jsp调用就出错
本人编写了一段java类程序,功能是将html网页保存为pdf格式,这个java类单独运行结果是正确的。但是如果把这段代码加入jsp中,让jsp页面调用,结果就出错,请各位大侠帮忙看一下。。。谢谢啦
JSP代码为:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="format.conversion.HtmlToPdf" %>
<%
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 JSP 'index.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>
This is my JSP page. <br>
</body>
<%
HtmlToPdf htp=new HtmlToPdf();
htp.go();
%>
</html>
java类代码为:
package format.conversion;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont;
public class HtmlToPdf {
public static void main(String[] args){
try{
HtmlToPdf tt=new HtmlToPdf();
tt.go();
System.out.println("pdf保存成功");
}catch(Exception ex){
System.out.println("pdf保存失败");
ex.printStackTrace();
}
System.exit(0);
}
public void go()throws Exception{
String inputFile = "WebRoot/Report.html";
String url = new File(inputFile).toURI().toURL().toString();
JFileChooser fileSave=new JFileChooser(".");
fileSave.showSaveDialog(null);
File file=fileSave.getSelectedFile();
String outputFile =file.toString();
String url1=new File(outputFile).toURI().toURL().toString();
System.out.println(url);
System.out.println(url1);
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/SIMSUN.TTC", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);