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/", line 56, in application
    response = frappe.handler.handle()
  File "/home/erp/erpnext/apps/frappe/frappe/", line 21, in handle
    data = execute_cmd(cmd)
  File "/home/erp/erpnext/apps/frappe/frappe/", line 52, in execute_cmd
    return, **frappe.form_dict)
  File "/home/erp/erpnext/apps/frappe/frappe/", line 922, in call
    return fn(*args, **newargs)
  File "/home/erp/erpnext/apps/frappe/frappe/", line 107, in set_value
  File "/home/erp/erpnext/apps/frappe/frappe/model/", line 230, in save
    return self._save(*args, **kwargs)
  File "/home/erp/erpnext/apps/frappe/frappe/model/", line 263, in _save
  File "/home/erp/erpnext/apps/frappe/frappe/model/", line 772, in run_before_save_methods
  File "/home/erp/erpnext/apps/frappe/frappe/model/", line 666, in run_method
    out = Document.hook(fn)(self, *args, **kwargs)
  File "/home/erp/erpnext/apps/frappe/frappe/model/", line 887, in composer
    return composed(self, method, *args, **kwargs)
  File "/home/erp/erpnext/apps/frappe/frappe/model/", line 870, in runner
    add_to_return_value(self, fn(self, *args, **kwargs))
  File "/home/erp/erpnext/apps/frappe/frappe/model/", 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/", line 25, in validate
  File "/home/erp/erpnext/apps/frappe/frappe/printing/doctype/print_format/", line 39, in extract_images
    df['options'] = extract_images_from_html(self, df['options'])
  File "/home/erp/erpnext/apps/frappe/frappe/utils/", 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/", line 155, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/home/erp/erpnext/apps/frappe/frappe/utils/", 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/", line 155, in save_file
    content = base64.b64decode(content)
  File "/usr/lib/python2.7/", 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.


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.


I have created two PR whit two alternatives:


1 Like

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