PDF Generating Tool Support Forum

HOME   Login   Register    Search




  Subject: A definition of custom resource loaders
   PostPosted: 02 Jun 2009, 12:03 
In most of the situations PD4ML is able to resolve relative or absolute resource references (to images, to CSS etc) and load them. However there are special cases, when the default mechanisms are not sufficient.

For example:

  • The resources are stored in an unusual place, i.e. in a database.
  • The resources are referenced by an "exotic" or non-standard protocol. For instance, Weblogic and WebSphere SSL implementations are not derived from the standard JDK SSL classes PD4ML relies on, so it causes ClassCastException.

As a workaround PD4ML allows you to create your own resource loader, which takes an URL as a parameter, and should return the loaded resource as an array of bytes.

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.zefer.cache.ResourceProvider;

public class CustomFileResourceProvider extends ResourceProvider {

	public byte[] getResourceAsBytes(String resource, boolean debugOn) throws IOException {
		ByteArrayOutputStream fos = new ByteArrayOutputStream();
		byte buffer[] = new byte[2048];

		InputStream is = null;
		
		resource = "http://pd4ml.com/i/" + resource;
		
		URL src = new URL(resource);
		URLConnection urlConnect = src.openConnection();
		try {
			urlConnect.connect();
		} catch (Throwable e) {
			return new byte[0];
		}
		is = urlConnect.getInputStream();
		BufferedInputStream bis = new BufferedInputStream(is);

		int read;
		do {
			read = is.read(buffer, 0, buffer.length);
			if (read > 0) { // something to put down
				fos.write(buffer, 0, read);
			}
		} while (read > -1);

		fos.close();
		bis.close();
		is.close();

		return fos.toByteArray();
	}
}



In order to enable the loader, you may either pass to PD4ML the class name with an environment variable (add the following to JVM command line):

-Dpd4ml.extra.resource.loaders=CustomFileResourceProvider

or via API call:

HashMap map = new HashMap();
map.put( "pd4ml.extra.resource.loaders", "CustomFileResourceProvider" );
pd4ml.setDynamicParams(map);


It may also be a coma-separated list of multiple resource loaders.


  Subject: Re: A definition of custom resource loaders
   PostPosted: 29 Aug 2011, 23:04 
Hi,
I need to extract multiple images from database and put them on the PDF.
Is there a way I can pass the InputStream of the image as an arguement to the ResourceProvider.

Thanks


  Subject: Re: A definition of custom resource loaders
   PostPosted: 30 Aug 2011, 19:21 
> Is there a way I can pass the InputStream of the image as an arguement to the ResourceProvider.

You need to reference somehow the image in the database from an HTML source. It is up to you: you may even implement a repository of InputStreams (which would be a quite strange architectural decision) and refer to them by a unique input stream ID:

<img src="file:inputstream#4711">

In the resource loader you'll receive "file:inputstream#4711" image URI to be parsed, to select the corresponding input stream and to read the referred image.

But of course a better approach is to specify in the image URI some unique key, sufficient to lookup and to read database image with your custom resource loader.


  Subject: Re: A definition of custom resource loaders
   PostPosted: 05 Sep 2011, 21:15 
Hi,

Do you have any example code for using the Custom Resource loader using Java/JSP.

I have this code in my Filter :
map.put( "pd4ml.extra.resource.loaders", "CustomFileResourceProvider");
pd4ml.setDynamicParams(map);

But I don't see CustomFileResourceProvider getting called.


Any help would be really appreciated?

Thanks


  Subject: Re: A definition of custom resource loaders
   PostPosted: 06 Sep 2011, 12:11 
Is CustomFileResourceProvider.class in the classpath?

If so, try to enable debug (pd4ml.enableDebugInfo()) and inspect server's log.


  Subject: Re: A definition of custom resource loaders
   PostPosted: 23 Sep 2011, 00:58 
Hi,

Do you have an example JSP and ResoruceProvider Class for reading multiple images from the database?
I am looking for a way to place the images at a particular place on the PDF using the ResourceProvide method.

Thanks


  Subject: Re: A definition of custom resource loaders
   PostPosted: 05 Oct 2011, 16:19 
In general you cannot place images to particular places of document layout via PD4ML API or with resource loaders.

You define your document layout using HTML and the images positions are defined by a locations of <img> tags.

If you plan to load images from a database, you may define your own image loading protocol. For example:

<img src="database:table=images;key=4711">

Your custom resource loader should parse the string "database:table=images;key=4711" and perform a corresponding request to the database (using the key value to locate a proper image) via JDBC (or similar).



[Reply]     [ 7 posts ] 

cron
Copyright ©2004-10 zefer|org. All rights reserved. Bookmark and Share