Skip to content

Instantly share code, notes, and snippets.

@xenodium
Last active September 15, 2023 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenodium/bda0a8d09d67ecad46d707b77b75e902 to your computer and use it in GitHub Desktop.
Save xenodium/bda0a8d09d67ecad46d707b77b75e902 to your computer and use it in GitHub Desktop.
Select some text and invoke via M-x send-to-kindle-as-pdf
(defcustom send-to-kindle-from-email
nil
"Your own email address to send from via mu4e."
:type 'string
:group 'send-to-kindle)
(defcustom send-to-kindle-to-email
nil
"Your Kindle email address to send pdf to."
:type 'string
:group 'send-to-kindle)
(defun send-to-kindle-as-pdf ()
(interactive)
(unless send-to-kindle-from-email
(setq send-to-kindle-from-email
(read-string "From email address: ")))
(unless send-to-kindle-to-email
(setq send-to-kindle-to-email
(read-string "To email address: ")))
(let* ((content (if (region-active-p)
(buffer-substring (region-beginning) (region-end))
(buffer-string)))
(timestamp (format-time-string "%Y-%m-%dT%H:%M:%S" (current-time)))
(path (concat (temporary-file-directory) timestamp))
(txt (concat path ".txt"))
(pdf (concat path ".pdf"))
(buffer (get-buffer-create (generate-new-buffer-name "*Send as pdf*"))))
(with-temp-buffer
(insert content)
(write-file txt))
(with-temp-buffer
(let ((exit-code (call-process "pandoc" nil (current-buffer) nil
"-f" "markdown_strict+hard_line_breaks"
"-V" "fontsize:12pt"
"-V" "geometry:margin=0.5in"
txt "-o" pdf)))
(unless (zerop exit-code)
(error (buffer-string)))))
(with-current-buffer buffer
(erase-buffer)
(message-mode)
(insert
(format
"From: %s
To: %s
Subject: Captured at %s
--text follows this line--
<#multipart type=mixed>
<#part type=\"application/pdf\" filename=\"%s\" disposition=attachment>
<#/part>
<#/multipart>"
send-to-kindle-from-email
send-to-kindle-to-email
timestamp pdf)))
(switch-to-buffer buffer)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment