def main(): """Generate a PDF using the async method.""" docraptor = DocRaptor() print("Create PDF") resp = docraptor.create( { "document_content": "<h1>python-docraptor</h1><p>Async Test</p>", "test": True, "async": True, } ) print("Status ID: {status_id}".format(status_id=resp["status_id"])) status_id = resp["status_id"] resp = docraptor.status(status_id) print(" {status}".format(status=resp["status"])) while resp["status"] != "completed": time.sleep(3) resp = docraptor.status(status_id) print(" {status}".format(status=resp["status"])) print("Download to test_async.pdf") with open("test_async.pdf", "wb") as pdf_file: pdf_file.write(docraptor.download(resp["download_key"]).content) print("[DONE]")
def test_status(self): stub_http_response_with("simple_status", "get") app = DocRaptor(self.test_key) resp = app.status("test-id") assert "completed" == resp["status"] key = app._get_download_key(resp["download_url"]) assert "ffffffffffd74b4a993fcae917699ed7" == key
def test_status(self): stub_http_response_with("simple_status", "get") app = DocRaptor(self.test_key) resp = app.status("test-id") assert "completed" == resp["status"] key = _get_download_key(resp["download_url"]) assert "ffffffffffd74b4a993fcae917699ed7" == key
def generate_pdf(request): #return HttpResponse(os.getcwd()) response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' docraptor = DocRaptor('yBXuHoCGqUDOjJ2MrWmv') userprof = UserProfile.objects.get(user=request.user) if request.POST .get('side') == "L": sg = StudyGuide.objects.get(title_id=userprof.current_guide) else: sg = StudyGuide.objects.get(title_id=userprof.current_guide2) images = PersonalImageNote.objects.filter(guide=sg) text = TextNote.objects.filter(guide=sg) html = "<html><body>" for t in text: if t.text is not None: html += str(t.text) for note in images: html += "<img src='http://ujungo.com/media/%s' STYLE='position:absolute; TOP:%spx; LEFT:%spx; WIDTH:%spx; HEIGHT:%spx' />" % (note.image.image,note.ycoord, note.xcoord, note.width, note.height) # STYLE='position:absolute; TOP:{{note.ycoord}}px; LEFT:{{note.xcoord}}px; WIDTH:{{note.width}}px; HEIGHT:{{note.height}}px' #html = html.replace(" ","") html += "</body></html>" print html with open("/usr/local/src/testsheets/media/%s.pdf" % sg.title_id, "wb") as f: f.write(docraptor.create({ 'document_content': html, 'test': True, 'api_key': 'yBXuHoCGqUDOjJ2MrWmv' }).content) return HttpResponse(sg.title_id)
def main(): """Generate a PDF with specified url.""" docraptor = DocRaptor() print("Create test_basic_url.pdf") with open("test_basic_url.pdf", "wb") as pdf_file: pdf_file.write( docraptor.create({ "document_url": "http://docraptor.com", "test": True }).content)
def main(): """Generate a PDF with specified url.""" docraptor = DocRaptor() print("Create test_basic_url.pdf") with open("test_basic_url.pdf", "wb") as pdf_file: pdf_file.write( docraptor.create( {"document_url": "http://docraptor.com", "test": True} ).content )
def main(): """Generate a PDF with specified content.""" docraptor = DocRaptor() print("Create test_basic_content.pdf") with open("test_basic_content.pdf", "wb") as pdf_file: pdf_file.write( docraptor.create({ "document_content": "<h1>python-docraptor</h1><p>Basic Test</p>", "test": True, }).content)
def main(): """Generate a PDF with specified content.""" docraptor = DocRaptor() print("Create test_basic_content.pdf") with open("test_basic_content.pdf", "wb") as pdf_file: pdf_file.write( docraptor.create( { "document_content": "<h1>python-docraptor</h1><p>Basic Test</p>", "test": True, } ).content )
def main(): """Generate an XLS with specified content.""" table = """<table> <thead> <tr><th>First Name</th><th>Last Name</th></tr> </thead> <tbody> <tr><td>Paul</td><td>McGrath</td></tr> <tr><td>Liam</td><td>Brady</td></tr> <tr><td>John</td><td>Giles</td></tr> </tbody> </table>""" docraptor = DocRaptor() print("Create test_basic.xls") with open("test_basic.xls", "wb") as pdf_file: pdf_file.write( docraptor.create( {"document_content": table, "document_type": "xls", "test": True} ).content )
def main(): """Generate a PDF using the async method.""" docraptor = DocRaptor() print("Create PDF") resp = docraptor.create({ "document_content": "<h1>python-docraptor</h1><p>Async Test</p>", "test": True, "async": True, }) print("Status ID: {status_id}".format(status_id=resp["status_id"])) status_id = resp["status_id"] resp = docraptor.status(status_id) print(" {status}".format(status=resp["status"])) while resp["status"] != "completed": time.sleep(3) resp = docraptor.status(status_id) print(" {status}".format(status=resp["status"])) print("Download to test_async.pdf") with open("test_async.pdf", "wb") as pdf_file: pdf_file.write(docraptor.download(resp["download_key"]).content) print("[DONE]")
def test_bogus_arguments_none(self): DocRaptor(self.test_key).create(None)
def test_bogus_arguments_bool(self): DocRaptor(self.test_key).create(True)
def test_param_api_keys(self): docraptor = DocRaptor(self.test_key) assert docraptor.api_key == self.test_key
def test_invalid_list_docs_raises(self): stub_http_response_with("invalid_list_docs", "get", 400) resp = DocRaptor(self.test_key).list_docs({"raise_exception_on_failure": True})
def test_blank_content(self): DocRaptor(self.test_key).create({"content": ""})
def test_blank_url(self): DocRaptor(self.test_key).create({"url": ""})
def test_bogus_arguments_bool(self): DocRaptor(self.test_key).list_docs(True)
def test_invalid_download_raises(self): stub_http_response_with("invalid_download", "get", 400) resp = DocRaptor(self.test_key).download("test-id", True)
def test_invalid_download(self): stub_http_response_with("invalid_download", "get", 400) resp = DocRaptor(self.test_key).download("test-id") assert FIXTURES["invalid_download"] == resp.content assert 400 == resp.status_code
def test_timeout(self): os.environ["DOCRAPTOR_TIMEOUT"] = "0.0001" resp = DocRaptor(self.test_key).create( {"document_content": "<html><body>Hey</body></html>"} )
def test_invalid_status_raises(self): stub_http_response_with("invalid_status", "get", 400) resp = DocRaptor(self.test_key).status("test-id", True)
def test_invalid_status(self): stub_http_response_with("invalid_status", "get", 403) resp = DocRaptor(self.test_key).status("test-id") assert FIXTURES["invalid_status"] == resp.content assert 403 == resp.status_code
def test_list_docs(self): stub_http_response_with("simple_list_docs", "get") resp = DocRaptor(self.test_key).list_docs({}) assert FIXTURES["simple_list_docs"] == resp.content
def test_no_content_empty(self): DocRaptor(self.test_key).create({})
import time from docraptor import DocRaptor docraptor = DocRaptor() print "Create test_basic_url.pdf" with open("test_basic_url.pdf", "wb") as f: f.write(docraptor.create({ 'document_url': 'http://docraptor.com', 'test': True }).content)
def test_download(self): stub_http_response_with("simple_download", "get") resp = DocRaptor(self.test_key).download("test-id") assert FIXTURES["simple_download"] == resp.content
def test_create_with_valid_content(self): stub_http_response_with("simple_pdf", "post") resp = DocRaptor(self.test_key).create({"document_content": self.html_content}) assert FIXTURES["simple_pdf"] == resp.content assert 200 == resp.status_code
import time from docraptor import DocRaptor docraptor = DocRaptor() print "Create test_basic_content.pdf" with open("test_basic_content.pdf", "wb") as f: f.write(docraptor.create({ 'document_content': '<h1>python-docraptor</h1><p>Basic Test</p>', 'test': True }).content)
import time from docraptor import DocRaptor table = """<table> <thead> <tr><th>First Name</th><th>Last Name</th></tr> </thead> <tbody> <tr><td>Paul</td><td>McGrath</td></tr> <tr><td>Liam</td><td>Brady</td></tr> <tr><td>John</td><td>Giles</td></tr> </tbody> </table>""" docraptor = DocRaptor() print "Create test_basic.xls" with open("test_basic.xls", "wb") as f: f.write(docraptor.create({ 'document_content': table, 'document_type': 'xls', 'test': True }).content)
import time from docraptor import DocRaptor docraptor = DocRaptor() print "Create PDF" resp = docraptor.create({ 'document_content': '<h1>python-docraptor</h1><p>Async Test</p>', 'test': True, 'async': True }) print "Status ID: %s" % (resp['status_id']) status_id = resp['status_id'] resp = docraptor.status(status_id) print " %s" % (resp['status']) while resp['status'] != 'completed': time.sleep(3) resp = docraptor.status(status_id) print " %s" % (resp['status']) print "Download to test_async.pdf", with open("test_async.pdf", "wb") as f: f.write(docraptor.download(resp['download_key']).content) print "[DONE]"
def test_create_with_invalid_content_raises(self): invalid_html = "<herp" stub_http_response_with("invalid_pdf", "post", 422) resp = DocRaptor(self.test_key).create( {"document_content": invalid_html, "raise_exception_on_failure": True} )
def test_bogus_arguments_None(self): DocRaptor(self.test_key).list_docs(None)
def test_no_content_attr(self): DocRaptor(self.test_key).create({"herped": "the_derp"})
def test_no_overwrite_env_api_key(self): os.environ["DOCRAPTOR_API_KEY"] = self.test_key docraptor = DocRaptor() os.environ["DOCRAPTOR_API_KEY"] = "blah" assert docraptor.api_key == self.test_key
def test_api_keys(self): docraptor = DocRaptor()
def test_create_with_invalid_content(self): invalid_html = "<herp" stub_http_response_with("invalid_pdf", "post", 422) resp = DocRaptor(self.test_key).create({"document_content": invalid_html}) assert FIXTURES["invalid_pdf"] == resp.content assert 422 == resp.status_code
def test_env_api_key(self): os.environ["DOCRAPTOR_API_KEY"] = self.test_key docraptor = DocRaptor() assert docraptor.api_key == self.test_key
</style> </head> <body> <div class="page"> <h1>Hello, world!</h1> </div> <div class="page"> <h1>Hello, from page 2!</h1> </div> </body> </html> """ docraptor = DocRaptor(os.environ['DOCRAPTOR_KEY']) results = docraptor.create({ 'document_content': content, 'test': True, 'javascript': True, }) with open("docraptor_sample.pdf", "wb") as f: f.write(results.content)