Thursday, July 30, 2009

Set,Specify PDF font type in Flying Soucer & iText

The following Java code shows you how to provide the font face for the created PDF output as same as in the input HTML or XHTML.

public static void createPDF(String url, String pdf)
throws IOException, DocumentException {
OutputStream os = null;
try {
os = new FileOutputStream(pdf);

ITextRenderer renderer = new ITextRenderer();
ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
callback.setSharedContext(renderer.getSharedContext());
renderer.getSharedContext ().setUserAgentCallback(callback);

Document doc = XMLResource.load(new InputSource(url)).getDocument();


renderer.getFontResolver().addFont("C:\\Windows\\Fonts\\Verdana.TTF","UTF-8", BaseFont.NOT_EMBEDDED);
renderer.setDocument(doc, url);
renderer.layout();
renderer.createPDF(os);

os.close();
os = null;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
}
}


"renderer.getFontResolver().addFont()" is the key method used the set the font face information of the PDF generated.

"BaseFont.NOT_EMBEDDED" specify not to embed the font in generated PDF.

"url" is the input HTML/XHTML file path.

"pdf" is the generated PDF file path.

Please find the input XHTML/HTML below:

<html>
<head>
<style type="text/css">
data
{
font-family: "Verdana";
}
</style>
</head>
<body style="font-size:16px">
<data>
<center style="font-size:24px">
<b>Hi Prasad...</b></center>
</data>
</body>
</html>


You can similarly embed the font in the generated PDF by changing parameters passed to "renderer.getFontResolver().addFont"

Thanks, have a nice day!

Tuesday, July 28, 2009

Do you like to test your application free?




































Java Application ToolsjDiffChaser
Web Application ToolsWebInjectSolex
SOAP Application ToolssoapUIiTKO LISAMActor
AJAX Application ToolsGITAKSelenium
Database ToolsLReportDBFireDTM Data Generator
.Net ToolsSharpRoboWatiN
Linux ToolsBugnuX



Use Open Source Software Testing Tools …..

Many test teams are paying over the odds for highly priced software test automation tools, in a market dominated by a small handful of commercial vendors. But many freeware and shareware alternatives are available today. In the above table, free testing tools are listed with the suitable application type.

Have a nice day!

Monday, July 27, 2009

Bring HTML, CSS power into PDF using Java

Today web technology has become a very powerful tool in communication. The technology of HTML, XHTML and CSS are very rich and powerful.

If you can convert HTML into PDF with CSS styles, the power of HTML/CSS will creep into PDFs. Yes it is possible,

You will need “Flying Saucer” & “iText” Libraries, which is are free Java libraries to convert XTML, CSS to PDF.

"Flying Saucer" bundles iText library in its binary distribution. The source distribution also includes set of demo and sample codes making it easy to understand the API.

iText : http://www.lowagie.com/iText/
Flying Saucer : https://xhtmlrenderer.dev.java.net/

Friday, July 24, 2009

Converting XHTML, HTML to PDF with CSS styles

I was wondering to convert HTML with CSS styles 2 PDF. Though googling, I could not find a good guidance on this.
So, I thought of publishing some information I came across.

There are actually several options to convert a HTML or a Web page into a PDF.

1. Adobe Acrobat

Yes off cause, Adobe Acrobat is one of the best options to convert a HTML into a PDF,
it will most of the time preserve the WYSIWYG.
The problem is that you need to pay for it. Adobe Acrobat is not free.

2. iText Library

“iText” Library is free and open-source. This project is mainly in Java, but now you have a C# iText library (iTextSharp) too.

The library is a very rich one doing many things on PDF files: create, manipulate etc.., but here I am considering only the HTML to PDF conversion. Actually you can convert a HTML into PDF easily with iText

"HtmlParser" class supports limited XHTML to PDF conversion. To apply CSS styles you need to use "HTMLWorker" class. Sample codes of these classes are available in Chapter 14 of iText in Action book. You can freely get the sample code form iText website.

However even this will not be a perfect XHTML to PDF conversion when it comes to CSS styles applied in HTMLs.

3. Flying Saucer & iText Library

The third option "Flying Saucer" with "iText" is much better, converting most of the CSS styles applied XHTML into PDFs nicely. This also free and open-source library given under LGPL. "Flying Saucer" bundles iText library in its binary distribution. The souce distribution also include set of demo and sample codes making it easy to understand the API.

Very simple set of lines to convert the XHTML into PDF,

Input xhtml:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My First Document</title>
<style type="text/css"> b { color: green; } </style>
</head>

<body>
<p>
<b>Greetings Earthlings!</b>
We've come for your Java.
</p>
</body>

</html>



Java code to convert XHTML into PDF:

package flyingsaucerpdf;

import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class FirstDoc {

public static void main(String[] args)
throws IOException, DocumentException {
String inputFile = "samples/firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);

os.close();
}
}

I believe even SAAS clouds in www.salesforce.com also render PDFs using these packages.

4. Online HTML to PDF conversion

Apart from these options there are list of web sites those provide online HTML to PDF conversion which I will not discuss here.

Here are the useful weblinks:

Flying Saucer : https://xhtmlrenderer.dev.java.net/
iText : http://www.lowagie.com/iText/
Adobe Acrobat : http://www.adobe.com/products/acrobat/?promoid=BPDDU

Thank you, Have a nice Day..!

Wednesday, July 8, 2009

Google Introducing the Google Chrome OS


After launching Google Chrome Browser Google reveals that the new open source OS Chrome will be released in mid 2010. For further detail visit official Google blog http://googleblog.blogspot.com/2009/07/introducing-google-chrome-os.html

Search