Appengine conversion Api (java)

两盒软妹~` 提交于 2020-01-16 11:32:32

问题


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.


回答1:


To convert a document you first have to create an asset. An asset is created by passing the the bytes to the constructor, as shown in the example. In your case you will need to use class BlobstoreInputStream to read the bytes of your PDF.

BlobKey blobKey = new BlobKey("your-pdf-blobkey");
InputStream is = new BlobstoreInputStream(blobkey);

Then you need to read all bytes from this input stream.

After the conversion, you can access the bytes of the converted image with asset.getData() and then follow this doc to write the image to the blobstore.




回答2:


Here is the working code to receive an upload pdf and convert it to a png using the Conversion api. The upload is completed with a multi-part post to an upload url must be obtained through:

     String url=blobstoreService.createUploadUrl("/upload");

Just place this code in a servlet and map it to "upload" in your web.xml.

The Conversion is good quality, however I did notice just a little blurriness around text. In my case the png was about 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");

                }


来源:https://stackoverflow.com/questions/11513841/appengine-conversion-api-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!