Python Forum
wkhtmltoimage generated jpeg save to Database using mongoengine
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wkhtmltoimage generated jpeg save to Database using mongoengine
#1
Question 
I want to generated image from html to jpeg. So I'm using imgkit and try to generated image. Image is generated correctly. According to imgkit documentation I tried to read image to variable and save it to mongodb collection using mongoengine. This code I used to read it to variable
# Use False instead of output path to save pdf to a variable
img = imgkit.from_url('http://example.com', False)
And this is the way that I tried to store it in db collection.
class Page(Document):
    image = ImageField()
        path_wkthmltoimage = r'C:/Program Files/wkhtmltopdf/bin/wkhtmltoimage.exe'
        config = imgkit.config(wkhtmltoimage=path_wkthmltoimage)
        options = {
            'format': 'jpeg',
            'quality': '40'
        }
        img = imgkit.from_url('http://example.com', False, config=config, options=options)
        myimage = open(img, 'rb')
            
Page.image.put(myimage)
Page.save()
But It gives this error
Error:
myimage = open(img, 'rb') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
How I store that image in db collection?
Reply
#2
make sure to install https://wkhtmltopdf.org/downloads.html

and check if the path_imgkit is working

import imgkit

path_imgkit = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.exe'
config = imgkit.config(wkhtmltoimage=path_imgkit)
imgkit.from_url('http://google.com', 'out.jpg', config=config)
the image will save itself in the root directory
Reply
#3
I already installed https://wkhtmltopdf.org/downloads.html. When I try to generated image save to local using this code it generated image and store it in parent folder.
imgkit.from_url('http://google.com', 'out.jpg', config=config)

But I want save it in db collection
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Save JSON data to sqlite database on Django Quin 0 2,804 Mar-26-2022, 06:22 PM
Last Post: Quin
  how retrieve database save multiple data in web Django 2.1 taomihiranga 0 2,763 Jul-30-2019, 04:58 PM
Last Post: taomihiranga
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 7,113 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  how i save the html form to flask database mebaysan 1 7,244 Feb-07-2019, 12:56 AM
Last Post: snippsat
  How to save uploaded image url in database from Django? PrateekG 14 14,701 Jul-04-2018, 05:18 PM
Last Post: PrateekG
  How to scrape only unique values and save it into database Prince_Bhatia 0 2,950 Jun-08-2018, 06:59 AM
Last Post: Prince_Bhatia
  Execute using Html, Save data into Database and Download in CSV in Django --Part 1 Prince_Bhatia 0 3,799 Jan-19-2018, 06:05 AM
Last Post: Prince_Bhatia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020