hi,
I use the maven dependencies as shown at [1]. [2] shows you a short
version of my code generating a pdf from my tapestry page displaying an
invoice (PrintInvoice.class). Note that I am building it as we speak so
it might still be somewhat crude at places.
Hope it helps,
Joost
[1] maven dependencies:
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8pre2</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.0.8</version>
</dependency>
[2] my code:
@Component(id = "pdfInvoiceLink", parameters =
{"context=literal:fileNameOfPdfToDownload.pdf"})
private ActionLink pdfInvoiceLink;
//event methods
@OnEvent(component = "pdfInvoiceLink")
private Object handlePdfDownload() throws Exception {
Link invoiceLink =
pageRenderLinkSource.createPageRenderLinkWithContext(PrintInvoice.class,
getInvoice().getId());
String baseURL = baseURLSource.getBaseURL(true);
URL url = new URL(baseURL + invoiceLink.getBasePath());
String htmlContent = getHtmlContent(url,
requestGlobals.getHTTPServletRequest().getSession().getId());
Document document = generateDocumentFromHtml(htmlContent);
byte[] pdfBytes = convertDocumentToPdfByteArray(baseURL, document);
final ByteArrayInputStream responseStream = new
ByteArrayInputStream(pdfBytes);
final StreamResponse response = new StreamResponse() {
public String getContentType() {
return ContentTypeConstants.PDF_CONTENT_TYPE;
}
public InputStream getStream() throws IOException {
return responseStream;
}
public void prepareResponse(Response response) {
}
};
return response;
}
/**
* @param baseURL
* @param document
* @return
* @throws DocumentException
* @throws IOException
*/
private byte[] convertDocumentToPdfByteArray(String baseURL,
Document document) throws DocumentException, IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(document, baseURL);
renderer.layout();
renderer.createPDF(os);
byte[] pdfBytes = os.toByteArray();
os.close();
return pdfBytes;
}
/**
* @param htmlContent
* @return
* @throws JDOMException
* @throws IOException
*/
private Document generateDocumentFromHtml(String htmlContent)
throws JDOMException, IOException {
//using JDOM to generate the DOM from xhtml
SAXBuilder builder = new SAXBuilder();
builder.setValidation(false);
//Don't validate the DTD
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String
systemId)
throws SAXException, IOException {
if (systemId.contains(".dtd")) {
return new InputSource(new StringReader(""));
} else {
return null;
}
}
});
ByteArrayInputStream byteIS = new
ByteArrayInputStream(htmlContent.toString().getBytes());
org.jdom.Document jdomDocument = builder.build(byteIS);
DOMOutputter outputter = new DOMOutputter();
//convert JDOM Document to org.w3c.dom.Document
Document document = outputter.output(jsdomDocument);
return document;
}
public String getHtmlContent(URL url, String sessionId) throws
IOException, CertificateException {
HttpsURLConnection ucon = (HttpsURLConnection)
url.openConnection();
ucon.setUseCaches(false);
ucon.setDoOutput(true);
ucon.setDoInput(true);
//provide the JSESSIONID cookie to make sure the same session is
used as the current one. Needed for logon credentials
ucon.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
//to make the connection work for development localhost with a self
signed certificate, provide this HostnameVerifier
ucon.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
//on localhost, allow for faulty certificates as it
will always be dev
return hostname.equals("localhost") ? true : false;
}
});
InputStream inputStream = ucon.getInputStream();
BufferedReader in = new BufferedReader(new
InputStreamReader(inputStream));
StringBuilder htmlBuilder = new StringBuilder();
String pre = null;
while ((pre = in.readLine()) != null){
htmlBuilder.append(pre);
htmlBuilder.append("\n");
}
in.close();
String htmlContent = htmlBuilder.toString();
log.debug("The downloaded html file:\n\n" + htmlContent);
return htmlContent;
}
On 31/12/10 12:54 AM, Angelo C. wrote:
Hi Joost,
This is a good solution, is there any maven dependencies I can directly
specify to pull down the Flying Saucer? or some codes that I can directly
use in a T5 app?
Thanks,
Angelo
Joost Schouten (mailing lists) wrote:
Another option is to use Flying Saucer [1] to render the html in pure
java. I use it with iText [2] to create pdf's from any xhtml page.
This is a nice combination as it basically allows you to template your
pdf's with tapestry pages. So from within a Tapestry page you retreive
any html with the use of an URLConnection (in my case another page in
my own app) and return it in the form of an pdf.
I'm sure there are ways to render to images i n Flying Saucer as well.
Good luck,
Joost
[1] https://xhtmlrenderer.dev.java.net/
[2]
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
On Thu, Dec 30, 2010 at 5:19 PM, Donny Nadolny<donny.nado...@gmail.com>
wrote:
Kind of. You can run it using xvfb (assuming linux), so you don't need a
full x server.
Selenium is a remote control tool for browsers, mainly used for writing
tests for webapps. It starts up firefox (or ie, chrome, or safari) and
controls the browser, telling it to click on links / type things, and you
can make assertions on the output. Usually it's only run on developer/CI
machines, not on your production machine, although I've seen it done.
On Thu, Dec 30, 2010 at 11:12 AM, Angelo C.<angelochen...@gmail.com>
wrote:
Thanks for the quick answer, not so familiar with selenium, can it be
run
headless in the server?
--
View this message in context:
http://tapestry.1045711.n5.nabble.com/T5-archiving-a-page-tp3322541p3322591.html
Sent from the Tapestry - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org