6

I love the option in org-reveal (reveal_single_file:t) that allows me to export a presentation to my students with image support embedded in the html file.

I've searched for a way to do it in the normal org to html exporter with no success. Any clue how that can be done?

2
  • What exactly do you mean by image support embedded in the html file, just that the images are shown in the html? Sorry I have only a superficial idea of reveal. Commented Jan 22, 2017 at 0:18
  • thx @Dieter.Wilhelm. i mean a self contained html file with images embedded in the html file (as base64-encoded data-URLs)
    – zeltak
    Commented Jan 22, 2017 at 6:21

2 Answers 2

3

The following advice of org-html--format-image lets ox-html export images as embedded base64 data if the corresponding image file has a size not larger than org-html-image-base64-max-size.

You can install that code by copying it into your init file and restarting emacs.

The code is tested with GNU Emacs 25.3.1 (x86_64-unknown-cygwin, GTK+ Version 3.22.20) of 2017-09-11 and orgmode 9.1.6.

(require 'org)
(require 'ox-html)
(require 'base64)

(defcustom org-html-image-base64-max-size #x40000
  "Export embedded base64 encoded images up to this size."
  :type 'number
  :group 'org-export-html)

(defun file-to-base64-string (file &optional image prefix postfix)
  "Transform binary file FILE into a base64-string prepending PREFIX and appending POSTFIX.
Puts \"data:image/%s;base64,\" with %s replaced by the image type before the actual image data if IMAGE is non-nil."
  (concat prefix
      (with-temp-buffer
        (set-buffer-multibyte nil)
        (insert-file-contents file nil nil nil t)
        (base64-encode-region (point-min) (point-max) 'no-line-break)
        (when image
          (goto-char (point-min))
          (insert (format "data:image/%s;base64," (image-type-from-file-name file))))
        (buffer-string))
      postfix))

(defun orgTZA-html-base64-encode-p (file)
  "Check whether FILE should be exported base64-encoded.
The return value is actually FILE with \"file://\" removed if it is a prefix of FILE."
  (when (and (stringp file)
             (string-match "\\`file://" file))
    (setq file (substring file (match-end 0))))
  (and
   (file-readable-p file)
   (let ((size (nth 7 (file-attributes file))))
     (<= size org-html-image-base64-max-size))
   file))

(defun orgTZA-html--format-image (source attributes info)
  "Return \"img\" tag with given SOURCE and ATTRIBUTES.
SOURCE is a string specifying the location of the image.
ATTRIBUTES is a plist, as returned by
`org-export-read-attribute'.  INFO is a plist used as
a communication channel."
  (if (string= "svg" (file-name-extension source))
      (org-html--svg-image source attributes info)
    (let* ((file (orgTZA-html-base64-encode-p source))
           (data (if file (file-to-base64-string file t)
                   source)))
      (org-html-close-tag
       "img"
       (org-html--make-attribute-string
        (org-combine-plists
         (list :src data
               :alt (if (string-match-p "^ltxpng/" source)
                        (org-html-encode-plain-text
                         (org-find-text-property-in-string 'org-latex-src source))
                      (file-name-nondirectory source)))
         attributes))
       info))))

(advice-add 'org-html--format-image :override #'orgTZA-html--format-image)
1
  • 1
    It works with emacs 26.1 on Windows 7 after evaluating the above code. Thanks!
    – Yu Shen
    Commented Nov 13, 2018 at 19:58
1

Pandoc supports inclusion of images via the --self-contained option:

pandoc -o outfile.html --standalone --self-contained --to html5 your-file.org

This will generate a single file and include all images as base64-encoded data-URLs.

3
  • thx. is there no way to do that from the exporter in orgmode nativly?
    – zeltak
    Commented Nov 26, 2016 at 6:16
  • 1
    also it seems pandoc cant find the images. i get an error pandoc: Could not fetch file:///home/zeltak/org/attach/bgu/courses/BGUC004.GIS.Planners/images/l4.Autocad.org/add_field.GIF InvalidUrlException file:///home/zeltak/org/attach/bgu/courses/BGUC004.GIS.Planners/images/l4.Autocad.org/add_field.GIF" "Invalid scheme" even though the file is there?
    – zeltak
    Commented Nov 26, 2016 at 6:33
  • This solution worked on Windows 7 for the first time. Then, it stopped working, only producing export without evaluating the code blocks which use PlantUML to generate the image. I haven't figured why it did work in the first time.
    – Yu Shen
    Commented Nov 13, 2018 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.