From mboxrd@z Thu Jan 1 00:00:00 1970 From: Francesco Pizzolante Subject: Blorgit > Adding PDF export Date: Mon, 23 Nov 2009 13:58:54 +0100 Message-ID: <873a451jox.fsf_-_@missioncriticalit.com> References: <87r5s24ebl.fsf@missioncriticalit.com> <87fx8asu9s.fsf@missioncriticalit.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: In-Reply-To: (Eric Schulte's message of "Thu, 19 Nov 2009 08:17:01 -0700") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org-mXXj517/zsQ@public.gmane.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org-mXXj517/zsQ@public.gmane.org To: Eric Schulte Cc: mailing-list-org-mode Eric, > Also, rather than using a shell script, would it be possible to use the > built in `org-export-as-pdf' function. This may obviate the need for a > shell script at all. In this case you probably wouldn't need to remove > the leading "." in the file names, and if you did, it could be done in > elisp right around export with the `rename-file' function. Regarding the addition of an extra link in order to get the PDF export, I took your remarks into account and here's what I finally went to. First, add the ".pdf" link to the page: --8<---------------cut here---------------start------------->8--- diff --git a/blorgit.rb b/blorgit.rb index 077b5d9..93bea47 100644 --- a/blorgit.rb +++ b/blorgit.rb @@ -197,6 +197,8 @@ __END__ %a{ :href => path_for(@blog, :format => 'org'), :title => 'download as org-mode' } .org %li %a{ :href => path_for(@blog, :format => 'tex'), :title => 'download as LaTeX' } .tex + %li + %a{ :href => path_for(@blog, :format => 'pdf'), :title => 'download as PDF' } .pdf #title_separator @@ sidebar --8<---------------cut here---------------end--------------->8--- In "acts_as_org.rb", call a "org-file-to-pdf" emacs function: --8<---------------cut here---------------start------------->8--- diff --git a/lib/acts_as_org.rb b/lib/acts_as_org.rb index 458741d..8e027fc 100644 --- a/lib/acts_as_org.rb +++ b/lib/acts_as_org.rb @@ -80,6 +80,24 @@ PREAMBLE l_path = self.latex_path(path) File.exist?(l_path) and File.mtime(l_path) > File.mtime(path) end + + def pdf_path(path) + File.join(File.dirname(path), + ActiveFile::Acts::Org::EXP_PREFIX + File.basename(path) + ".pdf") + end + + def to_pdf(path, options = {}) + p_path = self.pdf_path(path) + options = {:postamble => false}.merge(options) + self.emacs_run("(org-file-to-pdf \"#{path}\")") unless self.clean_pdf?(path) + return nil unless File.exist?(p_path) + html = File.read(p_path) + end + + def clean_pdf?(path) + p_path = self.pdf_path(path) + File.exist?(p_path) and File.mtime(p_path) > File.mtime(path) + end end module InstanceMethods @@ -107,6 +125,19 @@ PREAMBLE self.class.to_latex(self.full_path, options) end alias :to_tex :to_latex + + def pdf_path + self.class.pdf_path(self.full_path) + end + + def clean_pdf? + self.class.clean_pdf?(self.full_path) + end + + def to_pdf(options = {}) + self.class.to_pdf(self.full_path, options) + end + end end end --8<---------------cut here---------------end--------------->8--- In "org-interaction.el", added the "org-file-to-pdf" function: --8<---------------cut here---------------start------------->8--- diff --git a/elisp/org-interaction.el b/elisp/org-interaction.el index 2311156..9b0d695 100644 --- a/elisp/org-interaction.el +++ b/elisp/org-interaction.el @@ -24,6 +24,7 @@ evaluating BODY." ,temp-result))) (defvar org-interaction-prefix ".exported_") +(defvar tmp-prefix "exported_") (defun org-file-to-html (file-path) "Open up an org file, publish it to html, and then return the @@ -65,6 +66,31 @@ latex as a string." (write-file latex-path) (kill-buffer (current-buffer))))))) +(defun org-file-to-pdf (file-path) + "Open up an org file and export it as pdf." + (let* ((file-name (file-name-nondirectory file-path)) + (file-dir (file-name-directory file-path)) + (org-tmp-path (expand-file-name (concat tmp-prefix file-name ".org") "/tmp/")) + (pdf-tmp-path (expand-file-name (concat tmp-prefix file-name ".pdf") "/tmp/")) + (tex-tmp-path (expand-file-name (concat tmp-prefix file-name ".tex") "/tmp/")) + (pdf-path (expand-file-name (concat org-interaction-prefix file-name ".pdf") file-dir))) + (if (and (file-exists-p pdf-path) + (< 0 (time-to-seconds + (time-subtract + (nth 5 (file-attributes pdf-path)) + (nth 5 (file-attributes file-path)))))) + pdf-path + (with-temp-filebuffer + file-path + (write-file org-tmp-path) + (org-mode) + (save-window-excursion + (org-export-as-pdf nil) + (rename-file pdf-tmp-path pdf-path) + (delete-file org-tmp-path) + (delete-file tex-tmp-path) + (kill-buffer (current-buffer))))))) + ;; customization (setq org-export-blocks-witheld '(hidden comment)) --8<---------------cut here---------------end--------------->8--- Note that I use the /tmp folder to store temporary files without the leading dot (the export do PDF does not work with a leading dot). What do you think? Thanks for your feedback. Regards, Francesco _______________________________________________ Emacs-orgmode mailing list Please use `Reply All' to send replies to the list. Emacs-orgmode-mXXj517/zsQ@public.gmane.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode