From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Right way to add special processing to images? Date: Thu, 12 Jan 2017 16:53:38 +0000 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=f403043615cab7c5fd0545e88f6d Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:53256) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cRidT-0008Kf-14 for emacs-orgmode@gnu.org; Thu, 12 Jan 2017 11:54:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cRidK-0006Yt-Sg for emacs-orgmode@gnu.org; Thu, 12 Jan 2017 11:53:59 -0500 Received: from mail-ua0-x22a.google.com ([2607:f8b0:400c:c08::22a]:35194) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cRidK-0006Y6-MF for emacs-orgmode@gnu.org; Thu, 12 Jan 2017 11:53:50 -0500 Received: by mail-ua0-x22a.google.com with SMTP id y9so18543430uae.2 for ; Thu, 12 Jan 2017 08:53:50 -0800 (PST) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-org list --f403043615cab7c5fd0545e88f6d Content-Type: text/plain; charset=UTF-8 Hello, I have this elisp function that I run in org-export-before-processing-hook that coverts a pdf to png where that png file is already linked in the org file. Just that the png file did not exist to begin with; it is converted from pdf at the time of exporting. [Code is at the end of this email.] But for that you work, need to add my special keyword "convertfrompdf t" and I chose "#+HEADER" just before the image link to do that. Here is my MWE: ===== #+TITLE: PDF Image #+NAME: fig:unicorn # Below HEADER is required where the pdf version of the referenced png file exists. #+HEADER: :convertfrompdf t # The below caption line is optional #+CAPTION: Org-mode Unicorn Logo [[./org-mode-unicorn-logo.png]] ===== So the question is: Is using #+HEADER for this a good idea? Would the org devs just a better way to do the same? I basically need to be able to control per image link if that linked image foo.png needs to be converted from a foo.pdf. Thanks. ===== (defun modi/org-include-img-from-pdf (&rest _) "Convert the pdf files to image files. Only looks at #+HEADER: lines that have \":convertfrompdf t\"." (interactive) (when (derived-mode-p 'org-mode) (save-excursion (goto-char (point-min)) (while (search-forward-regexp "^\\s-*#\\+HEADER:.*\\s-:convertfrompdf\\s-+t" nil 'noerror) (let* (filenoext imgext imgfile pdffile cmd) ;; Keep on going on to the next line till it finds a line with ;; `[[FILE]]' (while (progn (forward-line 1) (not (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]")))) (when (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]") (setq filenoext (match-string-no-properties 1)) (setq imgext (match-string-no-properties 2)) (setq imgfile (expand-file-name (concat filenoext "." imgext))) (setq pdffile (expand-file-name (concat filenoext "." "pdf"))) (setq cmd (concat "convert -density 96 -quality 85 " pdffile " " imgfile)) (when (file-newer-than-file-p pdffile imgfile) ;; This block is executed only if pdffile is newer than imgfile ;; or if imgfile does not exist ;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Testing-Accessibility.html (message "%s" cmd) (shell-command cmd)))))))) ===== If interested, here is the code and MWE on my github repo: https://github.com/kaushalmodi/.emacs.d/tree/master/elisp/org-include-img-from-pdf -- Kaushal Modi --f403043615cab7c5fd0545e88f6d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello,

I have this elisp function that = I run in org-export-before-processing-hook that coverts a pdf to png where = that png file is already linked in the org file. Just that the png file did= not exist to begin with; it is converted from pdf at the time of exporting= . [Code is at the end of this email.]

But for that= you work, need to add my special keyword "convertfrompdf t" and = I chose "#+HEADER" just before the image link to do that.

Here is my MWE:
=3D=3D=3D=3D=3D
= #+TITLE: PDF Image
#+NAME: fig:unicorn
# Below HEADER i= s required where the pdf version of the referenced png file exists.
#+HEADER: :convertfrompdf t
# The below caption line is option= al
#+CAPTION: Org-mode Unicorn Logo
[[./org-mode-unicor= n-logo.png]]
=3D=3D=3D=3D=3D

So th= e question is: Is using #+HEADER for this a good idea? Would the org devs j= ust a better way to do the same? I basically need to be able to control per= image link if that linked image foo.png needs to be converted from a foo.p= df.

Thanks.

=3D=3D=3D=3D= =3D

(defun modi/org-include-img-from-pdf (&am= p;rest _)
=C2=A0 "Convert the pdf files to image files.

Only looks at #+HEADER: lines that have \":conver= tfrompdf t\"."
=C2=A0 (interactive)
=C2=A0 (w= hen (derived-mode-p 'org-mode)
=C2=A0 =C2=A0 (save-excursion<= /div>
=C2=A0 =C2=A0 =C2=A0 (goto-char (point-min))
=C2=A0 =C2= =A0 =C2=A0 (while (search-forward-regexp
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 "^\\s-*#\\+HEADER:.*\\s-:convertfrompdf\\s-+t= "
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 nil '= noerror)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (let* (filenoext imgext imgf= ile pdffile cmd)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; Keep on go= ing on to the next line till it finds a line with
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 ;; `[[FILE]]'
=C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (while (progn
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(forward-line 1)
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(not (looking-at "= \\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]"))))
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 (when (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]"= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq filenoext (matc= h-string-no-properties 1))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (setq imgext (match-string-no-properties 2))
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq imgfile (expand-file-name (concat filenoe= xt "." imgext)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 (setq pdffile (expand-file-name (concat filenoext "." "p= df")))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq cmd (= concat "convert -density 96 -quality 85 "
=C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 pdffile " " imgfile))
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 (when (file-newer-than-file-p pdffile imgfile)<= /div>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; This block is= executed only if pdffile is newer than imgfile
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;; or if imgfile does not exist
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (me= ssage "%s" cmd)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (shell-command cmd))))))))
=3D=3D=3D=3D=3D
=

If interested, here is the code and MWE on my github re= po:=C2=A0https://github.com/kaushalmodi/.emacs.d/tree/m= aster/elisp/org-include-img-from-pdf

--

Kaushal Modi

--f403043615cab7c5fd0545e88f6d--