Writing to a PDF from inside a GAE app

2019-07-09 01:41发布

I need to read several megabytes (raw text strings) out of my GAE Datastore and then write them all to a new PDF file, and then make the PDF file available for the user to download.

I am well aware of the sandbox restrictions that prevent you from writing to the file system. I am wondering if there is a crafty way of creating a PDF in-memory (or a combo of memory and the blobstore) and then storing it somehow so that the client-side (browser) can actually pull it down as a file and save it locally.

This is probably a huge stretch, but my only other option is to farm this task out to a non-GAE server, which I would like to avoid at all cost, even if it takes a lot of extra development on my end. Thanks in advance.

3条回答
再贱就再见
2楼-- · 2019-07-09 02:09

I found a couple of solutions by googling. Please note that I have not actually tried these libraries, but hopefully they will be of help.

查看更多
孤傲高冷的网名
3楼-- · 2019-07-09 02:22

While I'm not aware of the PDF generation on Google App Engine and especially in Java, but once you have it you can definitely store it and later serve it.

I suppose the generation of the PDF will take more than 30 seconds so you will have to consider using Task Queue Java API for this process.

After you have the file in memory you can simply write it to the Blobstore and later serve it as a regular blob. In the overview you will find a fully functional example on how to upload, write and serve your binary data (blobs) on Google App Engine.

查看更多
在下西门庆
4楼-- · 2019-07-09 02:23

You can definitely achieve your use case using GAE itself. Here are the steps that you should follow at a high level:

  1. Download the excellent iText library, which is a Java library to work with PDFs. First build out your Java code to generate the PDF content. Check out various examples at : http://itextpdf.com/book/toc.php

  2. Since you cannot write to a file directly, you need to generate your PDF content in bytes and then write a Servlet which will act as a Download Servlet. The Servlet will use the Response object to open a stream, manipulate the Mime Headers (filename, filetype) and write the PDF contents to the stream. A browser will automatically present a download option when you do that.

  3. Your Download Servlet will have high level code that looks like this:

    public class DownloadPDF extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
    
            //Extract some request parameters, fetch your data and generate your document
    
            String fileName = "<SomeFileName>.pdf";
            res.setContentType("application/pdf");
            res.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
            writePDF(<SomeObjectData>, res.getOutputStream());
        }
    }
    

    }

  4. Remember the writePDF method above is your own method, where you use iText libraries Document and other classes to generate the data and write it ot the outputstream that you have passed in the second parameter.

查看更多
登录 后发表回答