1

現在のページをキャプチャし、それを 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();

Web サイトは認証を必要とするため、生成された pdf はログ エラー ページです。

現在のユーザーのセッションを使用するページのコンテンツをキャプチャする方法はありますか?

前もって感謝します。

4

4 に答える 4

3

現在のリクエストと同じ 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 リクエストの実行に関心がないように見えるため、削除したことに注意してください。

于 2013-07-29T16:41:38.583 に答える
0

ページ全体を pdf に変換して .pdf ファイルとして保存できる Web サイトがいくつかあります。サイトhttp://pdfcrowd.com/を試してみてください。

于 2013-07-29T16:02:59.340 に答える
0

はい、もちろんあります。あなたはこのコンテンツを送信しているので、それを持っています。コンテンツ オブジェクトを保存する必要があります。持っていない場合は、バイト ストリームを調べてください。コンテンツはそこにあるはずです;)

于 2013-07-29T14:05:00.030 に答える
0

現在のユーザーのセッションを使用してページのコンテンツをキャプチャする方法はわかりませんが、別の方法を提案できます.PDF変換ロジックをSeleniumテストケース内に移動し、テストケースを使用してナビゲートしてログインできます認証が必要なページ。自動化された tc がログインした後、pdf 変換ロジックを呼び出すことができますか?

于 2013-07-29T14:00:10.273 に答える