PD4ML v4 Forums PD4ML v3 Archived Forums (Read Only) PD4ML Tips & Tricks A definition of custom resource loaders

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #26200

    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.

    [language=java:1r83hefy]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();
    }
    }[/language:1r83hefy]

    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:

    [language=java:1r83hefy]HashMap map = new HashMap();
    map.put( “pd4ml.extra.resource.loaders”, “CustomFileResourceProvider” );
    pd4ml.setDynamicParams(map);[/language:1r83hefy]

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

    #27260

    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

    #27261

    > 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:

    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.

    #27262

    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

    #27263

    Is CustomFileResourceProvider.class in the classpath?

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

    #27264

    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

    #27265

    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 tags.

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

    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).

Viewing 7 posts - 1 through 7 (of 7 total)

The forum ‘PD4ML Tips & Tricks’ is closed to new topics and replies.