Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7852955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:32:50+00:00 2026-06-02T19:32:50+00:00

i’m working on a task that i want to convert text file into PDF

  • 0

i’m working on a task that i want to convert text file into PDF one using conversion API that is in GAE , i tried as: https://developers.google.com/appengine/docs/python/conversion/overview

This is the code that i used:

from __future__ import with_statement
from google.appengine.api import files
import cgi, cgitb ; cgitb.enable()
import StringIO
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import mimetypes
from google.appengine.ext import blobstore
from mimetypes import guess_type
from google.appengine.api import conversion




def mime_type(filename):
    return guess_type(filename)[0]
class get(webapp.RequestHandler):
    def post(self):

        form = cgi.FieldStorage() 
        file_upload = form['file']  
        name=file_upload.filename  

        m=mimetypes.guess_type(name)[0]

        data=file_upload.file.read()
        buf = StringIO.StringIO()
        asset = conversion.Asset("text/plain", data, file_upload.filename)
        conversion_obj = conversion.ConversionRequest(asset, "application/pdf")
        result = conversion.convert(conversion_obj)
        if result.assets:
             for asset in result.assets:
                buf.write(asset.data)

        else:
             print "ERROR" 


        u_file = files.blobstore.create(mime_type="application/pdf",_blobinfo_uploaded_filename="test.pdf")
        data=buf.getvalue()
        with files.open(u_file, 'a') as f:
            f.write(data)
        files.finalize(u_file)
        blob_key = files.blobstore.get_blob_key(u_file)
        blob_info = blobstore.get(blob_key)
        name2 =  blob_info.filename   
        self.response.out.write("""<html><br><body style="background-color:#CC9999"><b><font size="5" face="Batang" ><center> <li ><a href="download.py?blob_key=%s" style="color:black">%s

               </center></font><hr></body></html>            
                """ % (str(blob_key),str(name2)))




def main():

    application = webapp.WSGIApplication( [(r'/get.py', get)], debug=True)

    run_wsgi_app(application)


if __name__ == "__main__":
    main()

download.py:

from __future__ import with_statement
from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp.util import run_wsgi_app
from mimetypes import guess_type

def mime_type(filename):
    return guess_type(filename)[0]
class Thumbnailer(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self):
        blob_key = self.request.get("blob_key")
        if blob_key:
            blob_info = blobstore.get(blob_key)


            if blob_info:
                save_as1 =  blob_info.filename
                type2=mime_type(blob_info.filename)
                self.send_blob(blob_info,content_type=type2,save_as=save_as1)

                return


def main():


    application = webapp.WSGIApplication([
            (r'/download.*', Thumbnailer),
        ], debug=True)
    run_wsgi_app(application)



if __name__ == '__main__':
    main()

EDIT:i edited the code, and when i try to print buf.getvalue(),i get :failed to load pdf document.
But when i try to open this PDF,i can’t open it and the adobe reader gives error immediately that it can’t open the file. Sorry i’m still beginner,any help please? Any suggestions are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:32:52+00:00Added an answer on June 2, 2026 at 7:32 pm

    Here is my working code sample(it’s for Python2.7 runtime though). Good luck!

    
    import os
    import StringIO
    
    from google.appengine.api import conversion
    from google.appengine.api import files
    from google.appengine.ext import blobstore
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp import blobstore_handlers
    
    import jinja2
    
    template_dir = os.path.join(os.path.dirname(__file__), 'templates')
    
    jinja2_env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(template_dir))
    
    
    class MainHandler(webapp.RequestHandler):
        def get(self):
            tmpl = jinja2_env.get_template('index.jinja2')
            self.response.out.write(tmpl.render())
    
    
    class PostHandler(blobstore_handlers.BlobstoreDownloadHandler):
        def post(self):
            data = self.request.get('file')
            asset = conversion.Asset('text/plain', data, 'test.txt')
            conversion_obj = conversion.Conversion(asset, 'application/pdf')
            result = conversion.convert(conversion_obj)
            buf = StringIO.StringIO()
            if result.assets:
                for asset in result.assets:
                    buf.write(asset.data)
            else:
                raise Exception('Conversion failed.')
            u_file = files.blobstore.create(mime_type='application/pdf',
                                            _blobinfo_uploaded_filename='test.pdf')
            with files.open(u_file, 'a') as f:
                f.write(buf.getvalue())
            files.finalize(u_file)
            blob_key = files.blobstore.get_blob_key(u_file)
            blob_info = blobstore.get(blob_key)
            self.send_blob(blob_info)
    
    
    app = webapp.WSGIApplication([
        ('/', MainHandler),
        ('/post_file', PostHandler),
    ],
    debug=True)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.