pdf2png.el 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. ;; From http://emacs.stackexchange.com/questions/390/display-pdf-images-in-org-mode
  2. ; Example setup
  3. ;
  4. ; In this example setup I have the following files:
  5. ;
  6. ; An org file like below that includes an image file.
  7. ; A pdf file myimage.pdf. As shown in the below org example, you include an image file like .png that you would like to convert the pdf to.
  8. ; #+TITLE: PDF Image
  9. ; #+STARTUP: inlineimages
  10. ; #+NAME: fig:myimage
  11. ; #+HEADER: :convertfrompdf t
  12. ; # The below caption line is optional
  13. ; #+CAPTION: My image
  14. ; [[./myimage.png]]
  15. ;; Execute the `modi/org-include-img-from-pdf' function just before saving the file
  16. (add-hook 'before-save-hook #'modi/org-include-img-from-pdf)
  17. ;; Execute the `modi/org-include-img-from-pdf' function before processing the
  18. ;; file for export
  19. (add-hook 'org-export-before-processing-hook #'modi/org-include-img-from-pdf)
  20. (defun modi/org-include-img-from-pdf (&rest ignore)
  21. "Convert the pdf files to image files.
  22. Only looks at #HEADER: lines that have \":convertfrompdf t\".
  23. This function does nothing if not in org-mode, so you can safely
  24. add it to `before-save-hook'."
  25. (interactive)
  26. (when (derived-mode-p 'org-mode)
  27. (save-excursion
  28. (goto-char (point-min))
  29. (while (search-forward-regexp
  30. "^\\s-*#\\+HEADER:.*\\s-:convertfrompdf\\s-+t"
  31. nil 'noerror)
  32. (let* (filenoext imgext imgfile pdffile cmd)
  33. ;; Keep on going on to the next line till it finds a line with
  34. ;; `[[FILE]]'
  35. (while (progn
  36. (forward-line 1)
  37. (not (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]"))))
  38. (when (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]")
  39. (setq filenoext (match-string-no-properties 1))
  40. (setq imgext (match-string-no-properties 2))
  41. (setq imgfile (expand-file-name (concat filenoext "." imgext)))
  42. (setq pdffile (expand-file-name (concat filenoext "." "pdf")))
  43. (setq cmd (concat "convert -density 96 -quality 85 "
  44. pdffile " " imgfile))
  45. (when (file-newer-than-file-p pdffile imgfile)
  46. ;; This block is executed only if pdffile is newer than imgfile
  47. ;; or if imgfile does not exist
  48. ;; Source: https://www.gnu.org/software/emacs/manual/html_node/elisp/Testing-Accessibility.html
  49. (message "%s" cmd)
  50. (shell-command cmd))))))))