Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export function should include the image - must send from client as base64 as well #21

Open
dreaminglucid opened this issue Aug 27, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@dreaminglucid
Copy link
Owner

dreaminglucid commented Aug 27, 2023

Dynamic Image Encoding and Decoding

Since you're already able to fetch the dream data from the server to the client, one approach would be to dynamically encode the image to a Base64 string on the client-side just before making the API call. Then, send this Base64 string as a field in the dream object. On the server-side, you can decode this Base64 string back into an image and embed it in the PDF.

Here's a high-level overview of what this could look like:

Client-Side (JavaScript)

  • Convert the local image to a Base64 string.
  • Add this Base64 string to the dream object.
  • Send the dream object to the server via an API call.

Here's a small snippet to convert an image to Base64 in JavaScript:

async function toBase64(url) {
    const response = await fetch(url);
    const blob = await response.blob();
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onloadend = () => resolve(reader.result);
        reader.onerror = reject;
        reader.readAsDataURL(blob);
    });
}

// Usage: Add this before sending the dream object to the server
const imageBase64 = await toBase64('path/to/local/image.jpg');
dream['imageBase64'] = imageBase64;

Server-Side (Python)

  • Retrieve the dream object from the API call.
  • Decode the Base64 string to an image.
  • Embed this image in the PDF.

Here's how you would modify your export_dreams_to_pdf Python function:

from reportlab.lib.pagesizes import letter
from reportlab.platypus import Image as ReportLabImage
from base64 import b64decode
from io import BytesIO

# ... (existing code)

# Inside export_dreams_to_pdf function
image_base64 = dream.get('imageBase64', None)
if image_base64:
    # Decode the base64 string
    image_data = b64decode(image_base64.split(',')[1])
    
    # Create a BytesIO object
    image_io = BytesIO(image_data)

    # Create a ReportLab Image object and add it to the Story
    Story.append(ReportLabImage(image_io, 200, 200))

This should solve the problem without any changes to the frontend logic, other than the Base64 encoding which will be invisible to the end-user.

@dreaminglucid dreaminglucid added the enhancement New feature or request label Aug 27, 2023
@dreaminglucid dreaminglucid self-assigned this Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant