从GAE应用程序中写入PDF [英] Writing to a PDF from inside a GAE app

查看:188
本文介绍了从GAE应用程序中写入PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的GAE数据存储中读取数兆字节(原始文本字符串),然后将它们全部写入到一个新的PDF文件,然后使PDF文件可供用户下载。



我非常了解防止您写入文件系统的沙箱限制。我想知道是否有一个狡猾的方式创建一个PDF内存(或内存和blobstore组合),然后存储它,使客户端(浏览器)可以实际上拉下来作为一个文件,并保存它本地的。

这可能是一个巨大的延伸,但我唯一的选择是把这个任务放到一个非GAE服务器,我想避免所有的成本,即使我需要大量额外的开发。提前致谢。

解决方案

使用GAE本身,您绝对可以实现您的使用案例。以下是您应该遵循的高级步骤: 下载优秀的 iText库,这是一个使用PDF的Java库。首先构建您的Java代码来生成PDF内容。查看各种示例: http://itextpdf.com/book/toc.php 由于您不能直接写入文件,因此您需要以字节为单位生成PDF内容,然后编写一个充当Download Servlet的Servlet。 Servlet将使用Response对象来打开一个流,操作Mime Headers(文件名,文件类型)并将PDF内容写入流。当你这样做的时候,浏览器会自动显示下载选项。 你的下载Servlet的高级代码如下所示:

  public class DownloadPDF extends HttpServlet {
$ b $ public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException {

//提取一些请求参数,获取数据并生成文档
$ b $ String fileName =< SomeFileName> .pdf;
res.setContentType(application / pdf);
res.setHeader(Content-Disposition,attachment; filename = \+ fileName +\);
writePDF(< SomeObjectData>,res.getOutputStream());




$ b $ p


$ b $ $ b
  • 请记住,上面的writePDF方法是您自己的方法,您可以在其中使用iText库Document和其他类来生成数据,并将其写入到您在第二个参数中传递的输出流中。



  • 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.

    解决方案

    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.

    这篇关于从GAE应用程序中写入PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆