I'm testing Java lib to edit existing pdf but the issue is that a can't load my existing pdf. I have the same result with iText and pdfbox, I can load the file the data seems here(pdf weigh ko) but the pdf created is empty (nothing display).
I'm doing it on a app engine server, with the two lib I can create pdf and display it in my browser with servlet or webservice.
I'm totaly lost, try tons of code but always the same result!
iText with importedPage :
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter docWriter = PdfWriter.getInstance(document, baos);
document.open();
// Load existing PDF
PdfReader reader = new PdfReader("WEB-INF/pdf.pdf");
document.newPage();
PdfImportedPage page = docWriter.getImportedPage(reader, 1);
PdfPTable table = new PdfPTable(2);
table.addCell(Image.getInstance(page));
document.add(table);
document.close();
docWriter.close();
pdfbox :
PDDocument document = new PDDocument();
PDDocument sourceDocument = PDDocument.load("WEB-INF/pdf.pdf");
PDPage templatePdfPage = (PDPage)sourceDocument.getDocumentCatalog().getAllPages().get(0);
document.addPage(templatePdfPage);
document.save(output);
First of all Get the path Using ServletContext Servlet and using PDFBOx read the pdf file and save the pdf file in /WEB-INF/savedpdffiles/
folder.
Note: Create the folder savedpdffiles
under WEB-INF
folder.
Read and save PDF file in Google AppEngine.
Code:
PrintWriter printWriter = response.getWriter();
try {
ServletContext context = request.getSession().getServletContext();
String pdffiles = context.getRealPath("/WEB-INF/");
File readPath = new File(pdffiles);
if (readPath.exists()) {
String pdfFile = "04-Request-Headers.pdf"; // read this file to save in savedpdffiles folder
File savedPath = new File(readPath.getAbsolutePath() +"/savedpdffiles/"); // create savedpdffiles folder under WEB-INF folder
File readFullPath = new File(readPath.getAbsolutePath() + File.separatorChar + pdfFile);
if (readFullPath.isFile()) {
if(!savedPath.exists()) {
savedPath.createNewFile();// create new pdf file if not exits
printWriter.println( savedPath.getName() +" File created in -> "+ savedPath.getAbsolutePath());
}
PDDocument document = new PDDocument();
PDDocument sourceDocument = PDDocument.load(readFullPath.getAbsolutePath()); // read the pdf file by PDDocument
PDPage templatePdfPage = (PDPage) sourceDocument.getDocumentCatalog().getAllPages().get(0); // only first page is read out of 13 pages and save the first page.
document.addPage(templatePdfPage);
document.save(savedPath + "/" + pdfFile);
document.close();
sourceDocument.close();
printWriter.print(pdfFile + " File saved to this location-> "+ savedPath.getAbsolutePath() + File.separatorChar + pdfFile);
} else {
printWriter.println(readFullPath.getName() + " File not exits in -> "+ readFullPath.getAbsolutePath());
}
} else {
printWriter.println("Path not exists -> "+ readPath.getAbsolutePath());
}
} catch (Exception e) {
printWriter.print("Type of Error occured while saving the PDF file -> "+ e.getMessage());
e.printStackTrace();
}
You will get below error
Caused by:
java.lang.NoClassDefFoundError: java.awt.Color is a restricted class. Please see the Google App Engine developer's guide for more details.
Powered by Jetty://
I find the problem, I'm using maven to build my app. Maven corrumpt my pdf by encoding file (in UTF-8 I think). I found this because I got .p12 file and when I what to read my lib say it was corrumpt file.
The problem now is a try to avoid this but dosn't work. (For the moment I replace file after build)
I try to add this in my pom.xml :
<resources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.pdf</exclude>
</excludes>
</resource>
</resources>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
and something was ambigious in my pom.xml, I doesn't not what it do :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
in order to interpolate version from pom into appengine-web.xml
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.