与Convert a Jsp with css to pdf相关
使用飞碟,我可以很容易地将HTML/XHTML转换成PDF格式.
现在,我希望能够为HTML/XHTML设置参数,所以我决定使用JSP进行模板化,并使用核心JSTL。Tomcat将JSP解析为HTML,因此我希望获取结果,通过获取响应内容将其转换为PDF,并通过飞碟进行处理。
但是,我不知道如何拦截与上述问题相关的响应内容。
1- ServletResponse接口对于在处理JSP之前添加内容似乎很有用。
2- HttpServletResponse是修改响应的头。
如何在我的方法中获取响应的内容,用飞碟处理它,然后将它作为"application/pdf“发送回客户端?
注:我们正在使用Struts 1,我知道这是不好的,我们计划很快转移到Spring,但还没有这个应用程序。
不是最好的解决方案,而是有效的解决方案:
现在,我将使用请求参数通过URI设置Jsp的内容,然后将其作为"application/pdf“发送回客户端。
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Scanner;
/**
* PdfConverter
* Pdf methods to convert Jsp from URI, or Xhtml/Xml from URI
* or internal content to Pdf.
*/
public class PdfConverter {
private static String encoding = "UTF-8";
/**
* Convert JSP from local server to PDF
* @param jspUri usage :
* URL jspUri =
* new URL("http://localhost:8080/myApp/myJspServlet?param1=1¶m2=2");
* @param pdfFileName
* @return Html content as a String from generated Jsp.
* @throws DocumentException
* @throws IOException
*/
public static String JspToHtmlString(URL jspUri) throws DocumentException,
IOException {
BufferedReader in = new BufferedReader(
new InputStreamReader(jspUri.openStream())
);
CharArrayWriter caw = new CharArrayWriter();
int octet = 0;
while ((octet = in.read()) != -1) { caw.write(octet); }
String pdfContent = caw.toString();
caw.close();
return pdfContent;
}
/**
* Convert Xhtml from package to PDF to the folder where this class is
* located.
* @param xhtmlFileName name of the Xhtml file inside the application.
* @param pdfFileName name of the written Pdf file.
* @return Pdf file generated and written on disk.
* @throws DocumentException
* @throws IOException
*/
public static File XhtmlToPdfConverter( String xhtmlFileName,
String pdfFileName) throws
DocumentException,
IOException {
FileOutputStream pdf = new FileOutputStream(pdfFileName);
new ITextRenderer() {{
setDocumentFromString(
new Scanner(
this.getClass().getResourceAsStream(xhtmlFileName), encoding
).useDelimiter("\\A").next()
);
layout();
createPDF(pdf);
}};
pdf.close();
return new File(pdfFileName);
}
}发布于 2017-12-16 18:23:19
您可以手动调用请求分派程序来呈现视图。这是一个Spring实现,但是思想和大多数类应该是相同的。
要使其工作,您必须以某种方式获得ServletContext,然后使用假请求/响应呈现视图。
// the request url and method arent really relevant, they could be if we wanted to use that information but were not
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/arbitrary/url");
MockHttpServletResponse response = new MockHttpServletResponse();
addModelAsRequestAttributes(request, modelMap);
LocaleResolver localeResolver = new JspLocaleResolver();
localeResolver.setLocale(request, response, Locale.getDefault());
// Push our LocaleResolver into the request
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, localeResolver);
// Push our Locale into the request
LocalizationContext localizationContext = new LocalizationContext(null, Locale.getDefault());
request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT+".request", localizationContext);
request.setAttribute(Config.FMT_LOCALE, Locale.getDefault());
servletContext.getRequestDispatcher("/WEB-INF/pdf_views/mypage.jsp").include(request, response);
return response.getContentAsString();
/*
* Moves the items in the map to be request.attributes
*/
private void addModelAsRequestAttributes(ServletRequest request, Map<String,Object> modelMap) {
if(modelMap != null && request != null) {
for (Map.Entry<String, Object> entry : modelMap.entrySet()) {
String modelName = entry.getKey();
Object modelValue = entry.getValue();
if(modelValue != null) {
request.setAttribute(modelName, modelValue);
} else {
request.removeAttribute(modelName);
}
}
}
}https://stackoverflow.com/questions/47471619
复制相似问题