Click HELP for text formatting instructions. Then edit this text and check the preview.
Hi Pigeret,
Here is my complete code :
1. Make a conversation bean and add this method into it :
//===========================================================
@Begin
public byte[] renderingPDFForBytes(){
byte[] pdfBytes=null;
EmptyFacesContext emptyFacesContext = new EmptyFacesContext();
byte[] bytes = null;
try {
Renderer renderer = Renderer.instance();
renderer.render("ApostillingTemplate.xhtml");
Context conversation = Contexts.getConversationContext();
for(String s : conversation.getNames()){
System.out.println("s : "+s);
}
String h=Manager.instance().getCurrentConversationId();
store=DocumentStore.instance();
//My Proble is this hard coded ID...I need dynamic ID
DocumentData data=store.getDocumentData("1);
pdfBytes=data.getData();
} finally {
emptyFacesContext.restore();
}
endConversation();
return pdfBytes;
}
/**
* End conversation.
*/
@End
public void endConversation(){
System.out.println("pdf conversation ended");
}
//=======================================================
2)Here is My EmptyFacesContext :
//========================================================
import javax.faces.context.FacesContext;
import org.jboss.seam.mock.MockExternalContext;
import org.jboss.seam.mock.MockFacesContext;
public class EmptyFacesContext extends MockFacesContext {
FacesContext originalFacesContext;
public EmptyFacesContext(){
super(new MockExternalContext());
originalFacesContext = FacesContext.getCurrentInstance();
createViewRoot();
System.out.println("inside empty faces context");
FacesContext.setCurrentInstance(this);
}
public void restore(){
System.out.println("inside restore");
setCurrentInstance(originalFacesContext);
}
}
//============================================================
3)Here is Templeate for Generating PDF .Save it as ApostillingTemplate.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<p:document
xmlns:p="http://jboss.com/products/seam/pdf"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
paperSize="A4"
creator="PIA"
title="Apostilling Template"
type="pdf"
exportKey="1"
>
<p:paragraph alignment="center">
<p:font size="18" style="underline bold" family="Arial, Verdana, sans-serif">
<p:text value="APOSTILLE"/>
</p:font>
</p:paragraph>
<p:paragraph><p:text value=" "/></p:paragraph>
<p:paragraph alignment="center">
<p:font size="10" style="bold" family="Arial, Verdana, sans-serif">
<p:text value="dfdf"/>
</p:font>
</p:paragraph>
<p:font size="9" family="Arial, Verdana, sans-serif">
<p:table columns="2" widths="1 25">
<f:facet name="defaultCell">
<p:cell horizontalAlignment="left" verticalAlignment="top" paddingTop="20" border="0">
</p:cell>
</f:facet>
<p:cell><p:paragraph><p:text value="1."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph><p:text value="Country: "/> <p:text value="fsdfr"/></p:paragraph>
</p:cell>
<p:cell></p:cell>
<p:cell><p:paragraph><p:text value="This public document"/></p:paragraph></p:cell>
<p:cell><p:paragraph><p:text value="2."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph><p:text value="Has been signed by "/> <p:text value="dsfdfd"/></p:paragraph>
</p:cell>
<p:cell><p:paragraph><p:text value="3."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph><p:text value="Acting in the capacity of "/> <p:text value="gfgreertre"/></p:paragraph>
</p:cell>
<p:cell><p:paragraph><p:text value="4."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph><p:text value="Bears the seal/stamp "/> <p:text value="gfgrftr"/></p:paragraph>
</p:cell>
<p:cell></p:cell>
<p:cell><p:paragraph><p:text value="Certified"/></p:paragraph></p:cell>
<p:cell><p:paragraph><p:text value="5."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph>
<p:text value="At "/> <p:text value="fdgrfr"/>
<p:text value=" "/>
<p:text value="6. On "/> <p:text value="fsfffgfsd"/>
</p:text>
</p:paragraph>
</p:cell>
<p:cell><p:paragraph><p:text value="7."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph>
</p:paragraph>
</p:cell>
<p:cell><p:paragraph><p:text value="8."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph>
<p:text value="Number:"/>
<p:text value=""/>
</p:paragraph>
</p:cell>
<p:cell><p:paragraph><p:text value="9."/></p:paragraph></p:cell>
<p:cell>
<p:paragraph><p:text value="Seal"/>
<p:text value=" "/>
<p:text value="10. Signature:"/></p:paragraph>
</p:cell>
</p:table>
<p:table columns="5" widths="2 1 2 2 1">
<f:facet name="defaultCell">
<p:cell horizontalAlignment="left" verticalAlignment="top" paddingTop="20" border="0"></p:cell>
</f:facet>
<p:cell>
</p:cell>
<p:cell></p:cell>
<p:cell>
</p:cell>
<p:cell horizontalAlignment="left" verticalAlignment="bottom" paddingTop="40" border="0">
<p:paragraph alignment="right></p:paragraph>
</p:cell>
<p:cell></p:cell>
<p:cell></p:cell>
</p:table>
</p:font>
<p:table columns="1">
<f:facet name="defaultCell">
<p:cell horizontalAlignment="justify" verticalAlignment="top" paddingTop="40" border="0">
</p:cell>
</f:facet>
<p:cell>
<p:paragraph>
<p:font size="7" family="Arial, Verdana, sans-serif" style="bold">
</p:font>
<p:font size="7" family="Arial, Verdana, sans-serif">
</p:font>
</p:paragraph>
</p:cell>
</p:table>
</p:document>
//========================================================
4:My web.xml.
//========================================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>ecanoc</display-name>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>#{themebean.skin}</param-value>
</context-param>
<context-param>
<param-name>org.ajax4jsf.RESOURCE_URI_PREFIX</param-name>
<param-value>a4jResources</param-value>
</context-param>
<!-- Filter for tomahawk -->
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<param-name>maxFileSize</param-name>
<param-value>20m</param-value>
</init-param>
</filter>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>org.ajax4jsf.SKIN</param-name>
<param-value>classic</param-value>
</context-param>
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/sandbox.taglib.xml; /WEB-INF/tomahawk.taglib.xml</param-value>
</context-param>
<filter>
<filter-name>Seam Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
</filter>
<filter>
<display-name>Ajax4jsf Filter</display-name>
<filter-name>ajax4jsf</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
</filter>
<!-- extension mapping for adding <script/>, <link/>, and other resource tags to JSF-pages -->
<!-- extension mapping for serving page-independent resources (javascript, stylesheets, images, etc.) -->
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<!-- servlet-name must match the name of your javax.faces.webapp.FacesServlet entry -->
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Seam Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>ajax4jsf</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter>
<filter-name>Seam Servlet Filter</filter-name>
<filter-class>org.jboss.seam.servlet.SeamServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Seam Servlet Filter</filter-name>
<url-pattern>*.pdf</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Seam Resource Servlet</servlet-name>
<servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Seam Resource Servlet</servlet-name>
<url-pattern>/seam/resource/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<!--<security-constraint>
<display-name>Restrict raw XHTML Documents</display-name>
<web-resource-collection>
<web-resource-name>XHTML</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
//==========================================================
5: Call Conversation bean method(renderingPDFForBytes).PDF is generated on the fly in one request cycle.In order to get PDF bytes we have to mention its unique ID..which is always 1(my observation).
Still i am not able to get ID by using some Seam API.
I hope this will Help.
regards,
Pulkit