0

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

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
sadique
  • 15
  • 4
  • 1
    I see "wiprologo.jpg". You do know that Wipro is a customer of iText Software, has a support contract, and has direct access to the iText Jira? Of course, Wipro is a big company with lots of projects and this may not apply to you. Check with the person in your company who is responsible for the purchase of iText. – Amedee Van Gasse Oct 03 '17 at 06:58
  • You're showing your project structure. What does the .adapter file structure look like? I'd make sure img/wiprologo.jpg is also in there, at the root of the classes folder. – dbreaux Oct 03 '17 at 17:07

1 Answers1

1

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