Search code examples
itextibm-mobilefirst

Creating PDF using mobile first java adapter


I am able to generate PDF by using java Adapter and "ITEXT" library but not able to add an logo to the generated pdf. Logo is present inside java adapter folder structure while trying to refer the image file i am getting File not found exception. Below is the code

@GET
@OAuthSecurity(enabled=false)
@Produces("application/pdf")
@Path("/downloadfile")
public Response getResourceData() throws  IOException, DocumentException, URISyntaxException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Document doc = new Document();
    PdfWriter.getInstance(doc, baos);
    doc.open();
    Image img = Image.getInstance(Pdf55Resource.class.getResource("/img/wiprologo.jpg"));
    doc.add(img);
    doc.add(createFirstTable());
    doc.close();
    ResponseBuilder response = Response.ok(baos.toByteArray());
    response.header("Content-Type", "application/pdf");
    response.header("Content-disposition", "attachment; filename="+ "audit.pdf");
    response.header("Pragma", "private");
    response.header("Access-Control-Allow-Credentials", "true");
    response.header( "Content-Length", baos.size() );
    response.header("Access-Control-Allow-Origin", "*");
    response.header("Access-Control-Allow-Methods", "*");
    response.header("Access-Control-Allow-Headers", "*");
    Response result = response.build();
    return result;

}

I have created one image folder inside that folder i have my image file.

enter image description here


Solution

  • Several things you can try:

    Make sure your pom.xml has a rule to copy the image resource to the build target. Secondly, I think your file will need to be inside the java classpath structure for java to find it. If /img isn't in the classpath, I don't think it will find it.

    As an example, I load the my iText license file using getResourceAsSteam().

    InputStream keyFileIS = getClass().getClassLoader().getResourceAsStream(licenseFile);
    LicenseKey.loadLicenseFile(keyFileIS);  // LicenseKey version 2
    

    I put the license file in the base java directory (src/main/java) of the adapter to make sure it's in the classpath. I use the getClassLoader() since it searches relative to the classpath root instead of the current class. I don't specify any path info either, just the file name. (see What is the difference between Class.getResource() and ClassLoader.getResource()?)

    In the build section of the pom.xml, I added a resources rule to make sure it gets copied to the target (after the plugins rule):

    <build>
        <plugins>
            <plugin>
                <groupId>com.ibm.mfp</groupId>
                <artifactId>adapter-maven-plugin</artifactId>
                <extensions>true</extensions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes><exclude>**/*.java</exclude></excludes>
            </resource>
        </resources>
    </build>
    

    That copies everything that's not a source file to the target.

    Hope something helps