Impression pdf avec Flying Saucer
Bonjour tout le monde,
j'ai suivi un tuto pour générer des pdf à partir de xhtml avec flying saucer et itex.
voici mes codes.
Classe RendererFilter.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package com.cedricom.post.webapp.beans.printer;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.lowagie.text.DocumentException;
/**
* Veuillez mettre la description de la classe ici.
*
* @version 22 nov. 2010
* @author mdjigo
*/
public class RendererFilter implements Filter {
FilterConfig config;
private DocumentBuilder documentBuilder;
public void destroy() {
}
public void doFilter(final ServletRequest req, final ServletResponse resp, final FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// V�rifier si le filtre doit �tre appliqu�
String renderType = request.getParameter("RenderOutputType");
if (renderType != null) {
// Capturer le content de cette requ�te
ContentCaptureServletResponse capContent = new ContentCaptureServletResponse(response);
filterChain.doFilter(request, capContent);
try {
// Parse the XHTML content to a document that is
// readable by the XHTML renderer.
StringReader contentReader = new StringReader(capContent.getContent());
InputSource source = new InputSource(contentReader);
Document xhtmlContent = documentBuilder.parse(source);
if (renderType.equals("pdf")) {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(xhtmlContent, "");
renderer.layout();
response.setContentType("application/pdf");
OutputStream browserStream = response.getOutputStream();
renderer.createPDF(browserStream);
return;
}
} catch (SAXException e) {
throw new ServletException(e);
} catch (DocumentException e) {
throw new ServletException(e);
}
} else {
// Normal processing
filterChain.doFilter(request, response);
}
}
public void init(final FilterConfig config) throws ServletException {
System.setProperty("xr.util-logging.loggingEnabled", "false");
try {
this.config = config;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
documentBuilder = factory.newDocumentBuilder();
documentBuilder.setEntityResolver(new LocalHostEntityResolver());
} catch (ParserConfigurationException e) {
throw new ServletException(e);
}
}
} |
Classe ContentCaptureServletResponse.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| package com.cedricom.post.webapp.beans.printer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* Veuillez mettre la description de la classe ici.
*
* @version 22 nov. 2010
* @author mdjigo
*/
public class ContentCaptureServletResponse extends HttpServletResponseWrapper {
private ByteArrayOutputStream contentBuffer;
private PrintWriter writer;
/**
* @param originalResponse
*/
public ContentCaptureServletResponse(final HttpServletResponse originalResponse) {
super(originalResponse);
}
/**
* Veuillez mettre une description générale de la méthode ici
*
* @return content
*/
public String getContent() {
try {
writer = getWriter();
} catch (IOException e) {
e.printStackTrace();
}
writer.flush();
String xhtmlContent = new String(contentBuffer.toByteArray());
return xhtmlContent;
}
/**
* @return contentBuffer
*/
public ByteArrayOutputStream getContentBuffer() {
return contentBuffer;
}
@Override
public PrintWriter getWriter() throws IOException {
if (writer == null) {
contentBuffer = new ByteArrayOutputStream();
writer = new PrintWriter(contentBuffer);
}
return writer;
}
/**
* @param contentBuffer
* the contentBuffer to set
*/
public void setContentBuffer(final ByteArrayOutputStream contentBuffer) {
this.contentBuffer = contentBuffer;
}
/**
* @param writer
* the writer to set
*/
public void setWriter(final PrintWriter writer) {
this.writer = writer;
}
} |
web.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| <?xml version="1.0" encoding="UTF-8"?>
<web-app 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>post-webapp</display-name>
<!-- CONFIGURATION POUR LES LOGS -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:configs-webapp/log4j.properties</param-value>
</context-param>
<!-- INTEGRATION AVEC LA COUCHE MVC -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:contexts-webapp/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>#{postBackingBean.skinType}</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>
<context-param>
<param-name>org.ajax4jsf.VIEW_HANDLERS</param-name>
<param-value>com.sun.facelets.FaceletViewHandler</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER</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>
<filter>
<filter-name>RendererFilter</filter-name>
<filter-class>com.cedricom.post.webapp.beans.printer.RendererFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RendererFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Filtre d'encodage en UTF-8 -->
<filter>
<filter-name>charsetFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Configuration du chargement à la demande. TODO faire marcher ça avec la servlet de JSF -->
<!-- <filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
-->
<!-- Defining and mapping the RichFaces filter -->
<filter>
<display-name>RichFaces Filter</display-name>
<filter-name>richfaces</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
<init-param>
<param-name>createTempFiles</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxRequestSize</param-name>
<param-value>200000</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>richfaces</filter-name>
<servlet-name>JSF</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- SPRING SECURITY -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- CONFIGURATION JSF -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<servlet>
<servlet-name>JSF</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JSF</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/secure/errors.xhtml</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/secure/errors.xhtml</location>
</error-page>
</web-app> |
Voici un extrait du xhtml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <table width="100%">
<tr>
<td class="zoneRepeteeTitrePage">#{msgs['title.gestion.utilisateurs']}</td>
<td>
<hr class="zoneRepeteeHeaderLine" />
</td>
<td align="right">
<h:commandLink action="#{gestionUtilisateurController.listeAP}"
id="print">
<img src="#{application.contextPath}/images/Elements_Zone_Travail/Logo_imprimante.png" style="border: 0px;"/>
<f:param value="pdf" name="RenderOutputType"/>
</h:commandLink>
<img src="#{application.contextPath}/images/help.png"
style="height: 22px; width: 22px;" /></td>
</tr>
</table> |
Voici maintenant mon exception
[Fatal Error] :-1:-1: Premature end of file.
22 nov. 2010 16:54:06 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet JSF threw exception
org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at com.cedricom.post.webapp.beans.printer.RendererFilter.doFilter(RendererFilter.java:58)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Le xhtmlContent dans la classe RendererFilter.java est toujours vide,alors qu'il devrait contenir le xhtml de la page.
je vous remercie d'avance.