demonz demonz
demonz demonz

Reputation: 649

primefaces: how to show pdf AND load new form (separate windows)

this is the flow:


0 - user goes to http:\ form_url
1 - form appears
2 - user fills in fields
3 - user clicks "save and generate PDF"
4a - app opens a new browser tab and shows pdf
4b - app displays a brand new empty form in the original tab

up to now i've been able to do 4a or 4b. One or the other, not both of them at the same time.

Can you please help me, thanks

Upvotes: 1

Views: 939

Answers (1)

BalusC
BalusC

Reputation: 1108692

You can't send 2 HTTP responses to 1 HTTP request.

You need to let the client send 2 HTTP requests. In this particular case, easiest would be to store the data which is necessary for generating the PDF temporarily in session and use JavaScript's window.open() to fire a new request in a new window which in turn triggers the PDF generation. E.g.

id = UUID.randomUUID().toString();
Data data = collectDataWhichIsNecessaryForGeneratingPdf();
externalContext.getSessionMap().put(id, data);
return "brandNewEmptyform";

with in the brand new empty form

<h:outputScript rendered="#{not empty bean.id}">
    window.open('somePdfServlet?id=#{bean.id}');
</h:outputScript>

and then in the servlet which is mapped on /somePdfServlet:

String id = request.getParameter("id");
Data data = (Data) session.getAttribute(id);
session.removeAttribute(id);
// Now generate PDF based on data and write to response the usual way.

Upvotes: 1

Related Questions