注意:您需要在系统(convert
可执行文件)上安装ImageMagick ,此解决方案才能起作用。
如何实施此解决方案
设置示例
在此示例安装程序中,我具有以下文件:
- 如下所示的组织文件,其中包含图像文件。
- pdf文件
myimage.pdf
。
# ()convertfrompdf:t
[[./myimage.png]]
自动将pdf转换为图像文件的功能
(defun org-include-img-from-pdf (&rest _)
"Convert pdf files to image files in org-mode bracket links.
# ()convertfrompdf:t # This is a special comment; tells that the upcoming
# link points to the to-be-converted-to file.
# If you have a foo.pdf that you need to convert to foo.png, use the
# foo.png file name in the link.
[[./foo.png]]
"
(interactive)
(if (executable-find "convert")
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^[ \t]*#\\s-+()convertfrompdf\\s-*:\\s-*t"
nil :noerror)
;; Keep on going to the next line till it finds a line with bracketed
;; file link.
(while (progn
(forward-line 1)
(not (looking-at org-bracket-link-regexp))))
;; Get the sub-group 1 match, the link, from `org-bracket-link-regexp'
(let ((link (match-string-no-properties 1)))
(when (stringp link)
(let* ((imgfile (expand-file-name link))
(pdffile (expand-file-name
(concat (file-name-sans-extension imgfile)
"." "pdf")))
(cmd (concat "convert -density 96 -quality 85 "
pdffile " " imgfile)))
(when (and (file-readable-p pdffile)
(file-newer-than-file-p pdffile imgfile))
;; This block is executed only if pdffile is newer than
;; imgfile or if imgfile does not exist.
(shell-command cmd)
(message "%s" cmd)))))))
(user-error "`convert' executable (part of Imagemagick) is not found")))
挂钩设置以指定何时运行此功能
(defun my/org-include-img-from-pdf-before-save ()
"Execute `org-include-img-from-pdf' just before saving the file."
(add-hook 'before-save-hook #'org-include-img-from-pdf nil :local))
(add-hook 'org-mode-hook #'my/org-include-img-from-pdf-before-save)
;; If you want to attempt to auto-convert PDF to PNG only during exports, and not during each save.
;; (with-eval-after-load 'ox
;; (add-hook 'org-export-before-processing-hook #'org-include-img-from-pdf))
代码+ MWE
endless/update-includes
,如果在期间before-save-hook
,如果找到带有#+NAME
或#+CAPTION
带有标记:convertfrompdf
的线[[SOMEFILE.EXT]]
,然后找到带有的线,则可以执行Imagemagickconvert
函数转换SOMEFILE.pdf
为SOMEFILE.EXT
。