Api application conversion (java)

I want to convert pdf files to image files in appengine. Ideally, I would download pdf as a blob and save both the PDF and the pdf image. Conversion can also be performed at other times (taskqueue).

I did not find working samples or good documentation for this.
The official documentation is here . Here is my implementation on my boot 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: added byte [] = IOUtils.toByteArray (is); and still getting NPE ...

I’m also interested to know the quality of the conversion if anyone has experience.

0
source share
2 answers

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"); } 
0
source

To convert a document, you first need to create an asset. An asset is created by passing bytes to the constructor, as shown in the example. In your case, you will need to use the BlobstoreInputStream class 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 the bytes from this input stream.

After conversion, you can access the bytes of the converted image using asset.getData() , and then follow this document to write the image to Blobstore.

+2
source

Source: https://habr.com/ru/post/924303/


All Articles