Can't add dynamic base64 image in custom print format

Hello, everyone.
I added a text field to Sales Invoice with a generated base64 image of the barcode (the barcode is a requirement of Electronic Invoice in my country), then I created a Custom Print Format and added a section with the following HTML:

<img src="data:image/png;base64, {{ doc.barcode }}">

When tried to save, I get the following error:

Traceback (most recent call last):
  File "/home/erp/erpnext/apps/frappe/frappe/app.py", line 56, in application
    response = frappe.handler.handle()
  File "/home/erp/erpnext/apps/frappe/frappe/handler.py", line 21, in handle
    data = execute_cmd(cmd)
  File "/home/erp/erpnext/apps/frappe/frappe/handler.py", line 52, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/erp/erpnext/apps/frappe/frappe/__init__.py", line 922, in call
    return fn(*args, **newargs)
  File "/home/erp/erpnext/apps/frappe/frappe/client.py", line 107, in set_value
    doc.save()
  File "/home/erp/erpnext/apps/frappe/frappe/model/document.py", line 230, in save
    return self._save(*args, **kwargs)
  File "/home/erp/erpnext/apps/frappe/frappe/model/document.py", line 263, in _save
    self.run_before_save_methods()
  File "/home/erp/erpnext/apps/frappe/frappe/model/document.py", line 772, in run_before_save_methods
    self.run_method("validate")
  File "/home/erp/erpnext/apps/frappe/frappe/model/document.py", line 666, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/erp/erpnext/apps/frappe/frappe/model/document.py", line 887, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/erp/erpnext/apps/frappe/frappe/model/document.py", line 870, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/erp/erpnext/apps/frappe/frappe/model/document.py", line 660, in <lambda>
    fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
  File "/home/erp/erpnext/apps/frappe/frappe/printing/doctype/print_format/print_format.py", line 25, in validate
    self.extract_images()
  File "/home/erp/erpnext/apps/frappe/frappe/printing/doctype/print_format/print_format.py", line 39, in extract_images
    df['options'] = extract_images_from_html(self, df['options'])
  File "/home/erp/erpnext/apps/frappe/frappe/utils/file_manager.py", line 134, in extract_images_from_html
    content = re.sub('<img[^>]*src\s*=\s*["\'](?=data:)(.*?)["\']', _save_file, content)
  File "/home/erp/erpnext/env/lib/python2.7/re.py", line 155, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/home/erp/erpnext/apps/frappe/frappe/utils/file_manager.py", line 127, in _save_file
    file_url = save_file(filename, content, doctype, name, decode=True).get("file_url")
  File "/home/erp/erpnext/apps/frappe/frappe/utils/file_manager.py", line 155, in save_file
    content = base64.b64decode(content)
  File "/usr/lib/python2.7/base64.py", line 78, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

Watching the code it seems isn’t possibly add a dynamic image in this way, only works for static content.

Thanks

While saving a document, the system will automatically extract base64 encoded from HTML.

Maybe there needs to be a way to flag html extraction script to ignore extraction of images if you pass a tag.

Maybe just catch that exception and pass it. Also send a Pull!

Thanks for the answer, you have confirmed what I was supposed.
Knowing this I’ll try to find a way and will send a pull request.

Regards

I have created two PR whit two alternatives:

Thanks

1 Like

6 years later, unfortunately, this seems to be not done. I’m still experiencing issues. The src attribute just gets removed.