Temp File Creation Error on GAE With PDFBox

7 views
Skip to first unread message

Doug via StackOverflow

unread,
Jan 29, 2015, 11:13:58 PM1/29/15
to google-appengin...@googlegroups.com

On GAE I am trying to update a PDF with a FDF file using PDFBox (and Jersey).

I have read on how you can't use PDFBox on GAE because certain classes aren't whitelisted. I was hoping that just populating the PDF from the FDF wouldn't cause an issue if none of these classes are called.

I haven't seen a whitelist issue yet but I am getting a "java.lang.SecurityException: Unable to create temporary file"

So I am wondering if there is someway to avoid the FDFDocument creating the temp file?

Code!

package com.somecompany.test.service;

import java.io.File;
import java.io.OutputStream;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.fdf.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;

@Path("/pdfboxtest")
public class PDFBoxTest {

    @GET
    @Produces("application/pdf")
    public Response getFile() throws Exception {

        File pdfFile = new File("resources/GenerateFDF.pdf");
        File fdfFile = new File("resources/fdftest.fdf");

        PDDocument pdfDoc = PDDocument.load(pdfFile);
        FDFDocument fdfDoc = FDFDocument.load(fdfFile);

        PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        acroForm.setCacheFields(true);
        acroForm.importFDF(fdfDoc);

        PDStream ps = new PDStream(pdfDoc);
        OutputStream outputStream = ps.createOutputStream();

        ResponseBuilder response = Response.ok((Object) outputStream);
        response.header("Content-Disposition",
                "attachment; filename=wellthatworked.pdf");
        return response.build();
    }
}

Full error log!

com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.SecurityException: Unable to create temporary file
	at java.io.File.checkAndCreate(File.java:1873)
	at java.io.File.createTempFile(File.java:1968)
	at java.io.File.createTempFile(File.java:2013)
	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.createTmpFile(NonSequentialPDFParser.java:298)
	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.<init>(NonSequentialPDFParser.java:278)
	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.<init>(NonSequentialPDFParser.java:264)
	at org.apache.pdfbox.pdmodel.fdf.FDFDocument.load(FDFDocument.java:200)
	at org.apache.pdfbox.pdmodel.fdf.FDFDocument.load(FDFDocument.java:172)
	at com.somecompany.test.service.PDFBoxTest.getFile(PDFBoxTest.java:31)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:45)
	at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
	at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
	at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
	at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
	at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
	at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
	at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
	at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
	at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
	at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
	at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:540)
	at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:715)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
	at org.mortbay.jetty.Server.handle(Server.java:326)
	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
	at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
	at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:438)
	at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:445)
	at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:220)
	at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:309)
	at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:301)
	at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:442)
	at java.lang.Thread.run(Thread.java:724)



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/28229085/temp-file-creation-error-on-gae-with-pdfbox

Tilman Hausherr via StackOverflow

unread,
Jan 30, 2015, 5:49:41 PM1/30/15
to google-appengin...@googlegroups.com

A fixed version will be available in 1.8.9. However you can already test it now by going here https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox/1.8.9-SNAPSHOT/ (get a jar file from January 30 21:54 or later).

If it works, press the green checkmark or just delete the question. If it doesn't work, please write a comment.

Note that you may get into more trouble, I heard that GAE has other restrictions, see the discussion here.



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/28229085/temp-file-creation-error-on-gae-with-pdfbox/28245881#28245881
Reply all
Reply to author
Forward
0 new messages