使用移动优先 java 适配器创建 PDF
Creating PDF using mobile first java adapter
我可以使用 java 适配器和 "ITEXT" 库生成 PDF,但无法向生成的 pdf 添加徽标。尝试引用图像文件时,徽标出现在 java 适配器文件夹结构中,我收到文件未找到异常。下面是代码
@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;
}
我在该文件夹中创建了一个图像文件夹,我有我的图像文件。
您可以尝试的几件事:
确保您的 pom.xml 具有将图像资源复制到构建目标的规则。其次,我认为您的文件需要位于 java class 路径结构内,java 才能找到它。如果 /img 不在 class 路径中,我认为它不会找到它。
例如,我使用 getResourceAsSteam() 加载我的 iText 许可证文件。
InputStream keyFileIS = getClass().getClassLoader().getResourceAsStream(licenseFile);
LicenseKey.loadLicenseFile(keyFileIS); // LicenseKey version 2
我将许可证文件放在适配器的基础 java 目录 (src/main/java) 中,以确保它位于 class 路径中。我使用 getClassLoader() 因为它搜索相对于 class 路径根而不是当前 class。我也没有指定任何路径信息,只是文件名。 (参见 What is the difference between Class.getResource() and ClassLoader.getResource()?)
在 pom.xml 的构建部分,我添加了一个 resources 规则以确保它被复制到目标(在 plugins 之后 规则):
<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>
这会将不是源文件的所有内容复制到目标。
希望有所帮助
我可以使用 java 适配器和 "ITEXT" 库生成 PDF,但无法向生成的 pdf 添加徽标。尝试引用图像文件时,徽标出现在 java 适配器文件夹结构中,我收到文件未找到异常。下面是代码
@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;
}
我在该文件夹中创建了一个图像文件夹,我有我的图像文件。
您可以尝试的几件事:
确保您的 pom.xml 具有将图像资源复制到构建目标的规则。其次,我认为您的文件需要位于 java class 路径结构内,java 才能找到它。如果 /img 不在 class 路径中,我认为它不会找到它。
例如,我使用 getResourceAsSteam() 加载我的 iText 许可证文件。
InputStream keyFileIS = getClass().getClassLoader().getResourceAsStream(licenseFile);
LicenseKey.loadLicenseFile(keyFileIS); // LicenseKey version 2
我将许可证文件放在适配器的基础 java 目录 (src/main/java) 中,以确保它位于 class 路径中。我使用 getClassLoader() 因为它搜索相对于 class 路径根而不是当前 class。我也没有指定任何路径信息,只是文件名。 (参见 What is the difference between Class.getResource() and ClassLoader.getResource()?)
在 pom.xml 的构建部分,我添加了一个 resources 规则以确保它被复制到目标(在 plugins 之后 规则):
<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>
这会将不是源文件的所有内容复制到目标。
希望有所帮助