jorisschellekens / borb

borb is a library for reading, creating and manipulating PDF files in python.

Home Page:https://borbpdf.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Save PDF using Dropbox API

IAyala opened this issue · comments

commented

I am saving pdfs created programatically using borb in my machine (thank you for the library, it is great!!). But I would like to be able to save the pdf directly into my dropbox account

I tried the following:

dbx.files_upload(pdf_object.pdf, '/my_file_from_borb.pdf',dropbox.files.WriteMode.add)

But got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vscode/.local/lib/python3.10/site-packages/dropbox/base.py", line 3207, in files_upload
    r = self.request(
  File "/home/vscode/.local/lib/python3.10/site-packages/dropbox/dropbox_client.py", line 323, in request
    res = self.request_json_string_with_retry(host,
  File "/home/vscode/.local/lib/python3.10/site-packages/dropbox/dropbox_client.py", line 473, in request_json_string_with_retry
    return self.request_json_string(host,
  File "/home/vscode/.local/lib/python3.10/site-packages/dropbox/dropbox_client.py", line 535, in request_json_string
    raise TypeError('expected request_binary as binary type, got %s' %
TypeError: expected request_binary as binary type, got <class 'borb.pdf.document.Document'>

Is there a way to make this work?

Thank you!

Hi there,

I have a couple of remarks:

  • The GitHub issues tab is not meant for "how do I do X with borb?" but rather for bugs and feature requests.
  • There are templates (for bugs and feature requests) which you can use
  • Your question comes down to "how can I get the bytes of the PDF", which has been asked before:
    • It has been asked as a GitHub issue (see #58 and #54)
    • It has been asked on StackOverflow as a question, tagged with borb

So please, in future check whether your issue is a duplicate.

I'm going to add an example to the examples-repository, because this question keeps popping up.
Here is the code:

from borb.pdf.document.document import Document
from borb.pdf.page.page import Page
from borb.pdf.pdf import PDF
from borb.pdf.canvas.layout.page_layout.multi_column_layout import SingleColumnLayout
from borb.pdf.canvas.layout.page_layout.page_layout import PageLayout
from borb.pdf.canvas.layout.text.paragraph import Paragraph

import dropbox

from decimal import Decimal
from pathlib import Path

import io


def dropbox_connect() -> dropbox.dropbox_client.Dropbox:
    """
    This function connects to Dropbox and returns the API connection
    :return:    a connection to the Dropbox API
    """
    try:
        dbx = dropbox.Dropbox('<your access key>')
    except AuthError as e:
        print('Error connecting to Dropbox with access token: ' + str(e))
    return dbx


def create_pdf() -> Document:
    """
    This function creates a PDF document containing the "Hello World" text.
    :return:    a borb.pdf.Document
    """

    # create Document
    doc: Document = Document()

    # create Page
    page: Page = Page()
    doc.add_page(page)

    # PageLayout
    layout: PageLayout = SingleColumnLayout(page)

    # add Paragraph
    layout.add(Paragraph("Hello World"))

    # return
    return doc


def document_to_bytes(pdf: Document) -> bytes:
    """
    This function converts a borb.pdf.Document to bytes
    :param pdf:     the input borb.pdf.Document
    :return:        the output bytes
    """
    buffer = io.BytesIO()
    PDF.dumps(buffer, pdf)
    buffer.seek(0)
    return buffer.getvalue()

if __name__ == '__main__':

    # set up connection
    dbx:dropbox.dropbox_client.Dropbox = dropbox_connect()
    print(dbx)

    # create PDF
    pdf: Document = create_pdf()

    # upload
    dbx.files_upload(document_to_bytes(pdf),
                     '/pdfs/hello_world.pdf',
                     mode=dropbox.files.WriteMode("overwrite"))

Kind regards,
Joris

commented

Thank you for your help, I apologize for not having noticed this question as a duplicate. I think adding this example would be useful. Thank you and sorry again for my ignorance. Regards

@jorisschellekens Your answers are always detailed yet clean and useful in my all my Github issue I have ever seen.