0

我想在 appengine 中将 pdf 转换为图像文件。理想情况下,我会将 pdf 作为 blob 上传并存储 pdf 和 pdf 的图像。转换也可以在不同的时间完成(任务队列)。

我还没有找到任何工作示例或这样做的良好文档。
官方文档在这里。这是我在上传 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");

                }

更新:已添加 byte[]=IOUtils.toByteArray(is); 并且仍然获得NPE ...

如果有人有经验,我也很好奇转换的质量。

4

2 回答 2

2

要转换文档,您首先必须创建资产。通过将字节传递给构造函数来创建资产,如示例中所示。在您的情况下,您将需要使用类BlobstoreInputStream来读取 PDF 的字节。

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

然后您需要从此输入流中读取所有字节。

转换后,您可以访问转换后图像的字节,asset.getData()然后按照此文档将图像写入 blobstore。

于 2012-07-16T23:39:19.883 回答
0

这是接收上传 pdf 并使用 Conversion api 将其转换为 png 的工作代码。上传完成后需要多部分post到上传url,必须通过以下方式获取:

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

只需将此代码放在 servlet 中并将其映射到 web.xml 中的“上传”。

转换质量很好,但是我确实注意到文本周围有点模糊。在我的情况下,png 大约大 25%。

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");

                }
于 2012-07-17T17:18:59.257 回答