PDF creation and displaying in Vaadin

I have created a blog post which includes a small sample application which demonstrates creating and displaying a PDF in Vaadin using iText and the Embedded component. The article can be found
here
.

Hope someone will find this useful.

Regards,
Jamie

Great article!

Would you consider adding the project that article is from to our
reference list
? Contact me at henri@vaadin.com if you want to list the app.

Can’t find the VaadinSamples on
http://code.google.com/p/jc-examples/

The Google code project jc-examples contains multiple sample projects. One of them is VaadinSamples. The specific url to the svn location of this project is:
http://code.google.com/p/jc-examples/source/browse/#svn/trunk/VaadinSamples
. This is an Eclipse project which you can directly checkout in Eclipse. All dependencies are contained within the project itself, for simplicity.

Hope this helps.

The project is scheduled to transition to production this month. I will contact my client if it is possible to list the application in the reference list.

Thanks.

I have been using your ideas and code to display PDF documents. It has been working great in all browsers with the exception of IE6 which I really don’t care about. However, the new Google Chrome 8 has a strange behavior. When I display the PDF, Chrome 8 takes over and begins zooming out all the way to 10%. Have you seen this?

Looks like it is bug with Google Chrome 8. Check out the following threads…

http://www.google.com/support/forum/p/Chrome/thread?tid=03b3ad0db82a254c&hl=en

http://www.google.com/support/forum/p/Chrome/thread?tid=6d31db6f76de3478&hl=en

Solution is to disable the built in PDF viewer from google and enable Acrobat’s PDF plugin.

Has anyone got a sample of how best to include an image (say using a graphic in the theme) in the PDF output? I have no problem when we know the full physical path, but we want to fetch the file from the EAR that our application [& theme]
is packaged in… so finding the image via an application-context base URL is fairly essential. I know there are a number of options that might work, just hoping someone has already done this and has suggestions/samples on what they found works well in the Vaadin space.

Was really hoping/expecting that I would find there was some Vaadin method/utility that would take most any Vaadin Resource (ExternalResource, ThemeResource, FileResource, etc.) and return a stream (or better yet, a byte array). I did not find one, but did find a viable solution for my case (using a resource from the theme). My current solution (for what it is worth) follows.

I created some “helper” routines that do all the work, placing them into a helper class (or the super class used for all my iText PDF work) that has access to the Vaadin Application.:
[font=Courier New]

    [color=#be4444]

import
[/color]org.apache.commons.io.IOUtils;

[color=#be4444]

private
[/color] Application
vaadinApp
;

. . .

[color=#be4444]

private byte
[/color] getBytesFromContextResource(String url)
throws
IOException {

	ApplicationContext context = [color=#0029ff]

vaadinApp
[/color].getContext();

if
(context
instanceof
WebApplicationContext) {

		ServletContext ctx = ((WebApplicationContext)context).getHttpSession().getServletContext();
		[color=#00a590]

// System.out.println("CONTEXT RESOURCE URL: " + url + "\nREAL PATH: " + ctx.getRealPath(url));
[/color]
InputStream stream = ctx.getResourceAsStream(url);

if
(stream !=
null
)

			[color=#be4444]

return
[/color] IOUtils.toByteArray(stream);

	}
	[color=#be4444]

return null
[/color];

[/indent] }

[color=#be4444]

private byte
[/color] getBytesFromFolder(String folder, String url)
throws
IOException {
[indent]
String pathSep = (folder.endsWith(
“/”
) || folder.endsWith(
“\”
)) ?
“”
:
“/”
;
String path =
new
StringBuffer(folder).append(pathSep).append(url).toString();

byte
bytes = getBytesFromContextResource(path.toString());

return
bytes;

}

[color=#be4444]

public byte
[/color] getBytesFromThemesFolder(String theme, String url)
throws
IOException {
[indent]
String folder =
“/VAADIN/themes/”
+ theme;

return
getBytesFromFolder(folder, url);
[/indent]
}

[color=#be4444]

public byte
[/color] getBytesFromThemesFolder(String url)
throws
IOException {
[indent]

return
getBytesFromThemesFolder(
vaadinApp
.getTheme(), url);
[/indent]
}

[color=#be4444]

public byte
[/color] getBytesFromWebInfFolder(String url)
throws
IOException {
[indent]

return
getBytesFromFolder(
“/WEB-INF/”
, url);
[/indent]
}

[/font]

With that code in place, I can now use any of the the [public]
getByte* calls to fetch the bytes for the resource, and then use that to get the iText Image for use in the PDF:

[font=Courier New]

PdfPCell logo() {
	[indent]

PdfPCell cell =
new
PdfPCell();

try
{
[indent]
Image logo = Image.getInstance(getBytesFromThemesFolder(
“images/Logo_Color.jpg”
));
logo.scaleToFit(180f, 100000f);
cell.addElement(logo);
[/indent]
}
catch
(Exception e) {
[indent]

// e.printStackTrace();

Chunk logoMarker =
new
Chunk(
“(LOGO SHOULD BE HERE)\n”
+ e);
cell.setFixedHeight(36f);
cell.addElement(logoMarker);
[/indent]
}

return
cell;
[/indent]
}

[/font]

Very good post Jamie, thanks a lot…Do you know how can I print a complete layout (and all of its components) in pdf??..some sort of picture of the layout…

regards,

Hugo