我想捕获当前页面并将其发送到将其转换为pdf的应用程序。
这是我正在使用的:
FacesContext facesContext=FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse)
facesContext.getExternalContext().getResponse();
HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
// RequestPrinter.debugString();
response.reset();
// download a pdf file
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename="+new Date().toString()+".pdf");
prince.setVerbose(true);
prince.setLog(logFile);
try{
//getPath() to the page the user is currently on
URL pagePath=new URL(this.getPath());
URLConnection urlConnection = pagePath.openConnection();
urlConnection.setDoOutput(true);
int length = urlConnection.getContentLength();
//Lets use inputStream
BufferedInputStream bis=new BufferedInputStream(urlConnection.getInputStream());
response.setContentLength(length);
//this.getPageUsingJSoup().data().getBytes();
//call prince and pass params for inputstream outputStream
prince.convert(bis,response.getOutputStream());
urlConnection.getInputStream().close();
}catch(MalformedURLException mu){
mu.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
facesContext.responseComplete();
由于网站需要认证,所以生成的pdf是记录错误页面。
是否有一种方法可以捕获使用当前用户会话的页面内容?
谢谢你。
只需在与当前请求相同的HTTP会话中请求页面。如果您的Web应用程序支持URL重写(默认情况下),则只需将会话ID附加为jsessionid
路径片段:
String sessionId = ((HttpSession) externalContext.getSession()).getId();
InputStream input = new URL("http://localhost:8080/context/page.jsf;jsessionid=" + sessionId).openStream();
// ...
或者,如果您的Web应用程序不接受URL重写,而仅接受cookie,然后以通常的方式将其设置为请求cookie:
URLConnection connection = new URL("http://localhost:8080/context/page.jsf").openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
InputStream input = connection.getInputStream();
// ...
请注意,我删除了setDoOutput()
,因为您似乎对执行POST请求不感兴趣。
我不知道如何使用当前用户的会话来捕获页面的内容,但是我可以建议另一种方法-您可以在Selenium测试用例内部移动pdf转换逻辑,并使用测试用例进行导航和登录到需要身份验证的页面。自动tc登录后,您可以调用pdf转换逻辑...?
是的。您正在发送此内容,因此拥有它。您应该存储内容对象。如果没有,请检查您的字节流。内容应该在那里;)
[有几个网站,可让您将整个页面转换为pdf并将其另存为.pdf文件。试用网站http://pdfcrowd.com/,希望对您有所帮助。