I want to convert pdfs to image files within appengine. Ideally I would upload the pdf as a blob and store both the pdf and an image of the pdf. The conversion could also be done at a different time (taskqueue).
I have not found any working samples or good documentation of doing this.
The official documentation is here. Here is my implementation on my upload servlet.
@SuppressWarnings("serial")
public class UploadBlobServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(UploadBlobServlet.class.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("data");
log.log(Level.WARNING,"blobKey: "+blobKey.getKeyString());
if (blobKey != null) {
resp.getWriter().println(blobKey.getKeyString());
BlobstoreInputStream in=new BlobstoreInputStream(blobKey);
byte[] b = IOUtils.toByteArray(is);
// try{
in.read(b);
Asset asset = new Asset(
"application/pdf", b, "testfile.pdf");
Document document = new Document(asset);
Conversion conversion = new Conversion(document, "image/png");
ConversionService service =
ConversionServiceFactory.getConversionService();
ConversionResult result = service.convert(conversion);
if (result.success()) {
// Note: in most cases, we will return data all in one asset,
// except that we return multiple assets for multi-page images.
FileService fileService=FileServiceFactory.getFileService();
for (Asset ass : result.getOutputDoc().getAssets()) {
AppEngineFile file=fileService.createNewBlobFile("image/png", "testfile.png");
FileWriteChannel writeChannel=fileService.openWriteChannel(file, false);
writeChannel.write(ByteBuffer.wrap(b));
writeChannel.closeFinally();
}
} else {
log.log(Level.WARNING,"error");
}
Update: Have added byte[]=IOUtils.toByteArray(is); and still getting a NPE...
I am also curious as to the quality of the conversion if anyone has experience.