Skip to main content

Accesd a BFO Publisher web service

Project description

bfopublisher

This is a Python module that allows you to easily connect to a BFO Publisher web service.

Example

pip install bfopublisher
from bfopublisher import Publisher

html = '''<!doctype html>
<html>
<head>
    <title>Example Document</title>
    <style type="text/css">
    body {
        background-color: #f0f0f2;
    }
    </style>
    <link rel="stylesheet" href="my-stylesheet.css" type="text/css">
</head>
<body>
<div class="box">
    <h1>Example Document</h1>
    <p>This is an example HTML document that will be converted to PDF.</p>
    <p><a href="https://publisher.bfo.com">BFO Publisher</a></p>
</div>
</body>
</html>'''

my_stylesheet = '''
    .box {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
'''

action = 'convert'
message = {
    'put': [
        {
            'content': html,
            'content_type': 'text/html',
            'path': 'test.html'
        },
        {
            'content': my_stylesheet,
            'content-type': 'text/css',
            'path': 'my-stylesheet.css'
        }
    ]
}

def callback(response):
    if 'content' in response:
        print('Retrieved content of type %s' % response['content_type'])
        content = response['content']
        output_stream = open('out.pdf', 'wb')
        output_stream.write(content)
        output_stream.close()

publisher = Publisher('ws://localhost:8080/ws')
task = publisher.build(action, message)
task.set_callback(callback)
task.send()
publisher.disconnect()

The module depends on the cbor2 and websocket-client external Python modules which should be installed by pip.

Detail

The bfopublisher module is a simple Python wrapper around the BFO Publisher Web Service, which can be downloaded and run (locally or on another server; all the examples on this page assume it's accessible at http://localhost:8080/). Note the ws: scheme on the URL to access the web service in the code as well as the final "ws" in the path, to access it via the websockets protocol.

To use the API:

  1. Create a new Publisher object
  2. call build(action, message), where action is one of the actions described in the web service documentation. Usually this will be convert, you can also use status. This returns a task object associated with that message.
  3. set the callback method for that task. It takes a response object which will be a dictionary. Different tasks can be assigned different callbacks to handle their results.
  4. call send() on the task to send the message and process the responses. A convert action will produce numerous responses. One of them will be of type convert-response and contain the content output, others may be progress log messages. You can run many tasks in parallel over the same websocket connection for lengthy conversions, and the responses will be multiplexed back to the callbacks you specify.
  5. Ensure that disconnect() is called on the Publisher object when you're done, to close the connection.

The Publisher class takes a websockets URL in its constructor. You can also specify an authorization parameter, which is an authorization key for access control.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bfopublisher-0.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file bfopublisher-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: bfopublisher-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for bfopublisher-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ff097f9f678806766296f2895032dd810cacc4cb47db5240d47b167dd4a98498
MD5 f3baab49a3e51bd4d0a5ee04d6bc180c
BLAKE2b-256 8d83ba2b0c2bb6747116c2b27f2998d114c4791325bfe33f8e97c0a3f00edb95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page