Exemple #1
0
 def generate(self, content, external_resource=False):
     doc_api = docraptor.DocApi()
     data = {'test': self.test_mode,
             'name': 'docraptor-python.pdf', # help you find a document later
             'document_type': 'pdf',
             'javascript': self.javascript}
     if external_resource:
         data['document_url'] = content
     else:
         data['document_content'] = content
     return doc_api.create_doc(data)
Exemple #2
0
# It is created asynchronously, which means DocRaptor will render it for up to
# 10 minutes. This is useful when creating many documents in parallel, or very
# large documents with lots of assets.
#
# DocRaptor supports many options for output customization, the full list is
# https://docraptor.com/documentation/api#api_general
#
# You can run this example with: python async.rb

import docraptor
import time
import shutil

docraptor.configuration.username = "******"  # this key works for test documents
# docraptor.configuration.debug = True
doc_api = docraptor.DocApi()

try:

    create_response = doc_api.create_async_doc({
        "test": True,  # test documents are free but watermarked
        "document_content":
        "<html><body>Hello World</body></html>",  # supply content directly
        # "document_url": "http://docraptor.com/examples/invoice.html", # or use a url
        "name": "docraptor-python.pdf",  # help you find a document later
        "document_type": "pdf",  # pdf or xls or xlsx
        # "javascript": True,                                           # enable JavaScript processing
        # "prince_options": {
        #   "media": "screen",                                          # use screen styles instead of print styles
        #   "baseurl": "http://hello.com",                              # pretend URL when using document_content
        # },
Exemple #3
0
def create_pdf(payload):
    docraptor.configuration.username = settings.DOCRAPTOR_API_KEY
    client = docraptor.DocApi()
    return client.create_doc(payload)
def generate_pdf(api_key, html, callback, **kwargs):
    # First get HTML string to use
    if html is None:
        callback(error='No HTML found.')
        return

    if api_key is None:
        callback(error='No API key found.')
        return

    filename = 'example'
    if 'filename' in kwargs:
        filename = kwargs['filename']

    if 'build_toc' in kwargs:
        html = build_toc(html)

    is_test = True
    if 'is_production' in kwargs:
        is_test = False

    if 'include_default_styles' in kwargs:
        html = insert_default_styles(html)

    # Save intermediate html output to outbox
    with open('outbox/{}.html'.format(filename), 'w') as fh:
        fh.write(html)

    # Init the docraptor
    docraptor.configuration.username = api_key
    doc_api = docraptor.DocApi()

    print("Converting file \"{}\"...".format(filename))

    # Variables for tracking generation time
    time_counter = 0
    sleep_increment = 0.1  # in seconds

    try:
        create_response = doc_api.create_async_doc({
            "test":
            is_test,  # test documents are free but watermarked
            "document_content":
            html,  # supply content directly
            "name":
            "{}.pdf".format(filename),  # help find document later
            "document_type":
            "pdf",  # pdf or xls or xlsx
            # Tell DocRaptor to parse the document including javascript, first,
            # then pass the result to Prince. This allows js to alter the page
            # and have the result appear in the pdf.
            "javascript":
            True,
            "prince_options": {
                # You can have Prince parse the document's javascript, but it's
                # pretty useless since it can't alter the page. It is used,
                # however, in templates/reports/_styles. This can probably be
                # refactored away.
                "javascript": True
                # 'baseurl': 'https://s3.amazonaws.com'
            }
        })

        while True:
            status_response = doc_api.get_async_doc_status(
                create_response.status_id)
            if status_response.status == "completed":
                doc_response = doc_api.get_async_doc(
                    status_response.download_id)
                duration = time_counter * sleep_increment * 1000
                kwargs = {}
                kwargs['filename'] = filename
                kwargs['doc_response'] = doc_response
                callback(duration=duration, **kwargs)
                break
            elif status_response.status == "failed":
                callback(error="failed")
                break
            else:
                time_counter += 1
                time.sleep(sleep_increment)

    except docraptor.rest.ApiException as error:
        callback(error=error)