Here is the working code to get a PDF file to download and convert it to png using Api Conversion. The download is completed by multi-user mail to the download URL, which must be obtained via:
String url=blobstoreService.createUploadUrl("/upload");
Just put this code in the servlet and map it to the "upload" in your web.xml.
Conversion is good quality, but I noticed a little blur around the text. In my case, png was 25% larger.
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"); if (blobKey != null) { resp.getWriter().println(blobKey.getKeyString()); BlobstoreInputStream in=new BlobstoreInputStream(blobKey); byte[] b = IOUtils.toByteArray(in); if(b!=null){ log.log(Level.WARNING,"blobsize: "+b.length); }else{ log.log(Level.WARNING,"b is null"); } 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", "test3file.png"); FileWriteChannel writeChannel=fileService.openWriteChannel(file, true); writeChannel.write(ByteBuffer.wrap(ass.getData())); writeChannel.closeFinally(); } } else { log.log(Level.WARNING,"error"); }
source share