我想捕获当前页面并将其发送到将其转换为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是登录错误页面。
有没有办法捕获使用当前用户会话的页面内容?
提前谢谢。
答案 0 :(得分:3)
只需在与当前请求相同的HTTP会话中请求该页面。如果您的webapp支持URL重写(默认情况下),那么只需将会话ID附加为jsessionid
路径片段:
String sessionId = ((HttpSession) externalContext.getSession()).getId();
InputStream input = new URL("http://localhost:8080/context/page.jsf;jsessionid=" + sessionId).openStream();
// ...
或者,如果您的网络应用不接受网址重写,但只接受网址,请按常规方式将其设置为请求Cookie:
URLConnection connection = new URL("http://localhost:8080/context/page.jsf").openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
InputStream input = connection.getInputStream();
// ...
请注意,我删除了setDoOutput()
,因为您似乎对执行POST请求不感兴趣。
答案 1 :(得分:0)
我不知道如何使用当前用户的会话捕获页面内容,但我可以建议另一种方法 - 您可以在Selenium测试用例中移动pdf转换逻辑并使用测试用例进行导航并登录到需要身份验证的页面。自动tc登录后,您可以调用您的pdf转换逻辑......?
答案 2 :(得分:0)
是的当然有。您正在发送此内容,因此您拥有它。您应该存储内容对象。如果你没有它,请检查你的字节流。内容应该在那里;)
答案 3 :(得分:0)
有几个网站允许您将整个页面转换为pdf并将其另存为.pdf文件。试试网站http://pdfcrowd.com/希望这可以帮到你。