Emacs打印缓冲区的自动换行到PDF

Wer*_*ner 4 emacs pdf-generation elisp emacs23

我使用此函数将缓冲区的内容打印到PDF

(来自我的.emacs文件:)

(defun print-to-pdf ()
  (interactive)
  (ps-spool-buffer-with-faces)
  (switch-to-buffer "*PostScript*")
  (write-file "/tmp/tmp.ps")
  (kill-buffer "tmp.ps")
  (setq cmd (concat "ps2pdf14 /tmp/tmp.ps /home/user/" (buffer-name) ".pdf"))
  (shell-command cmd)
  (shell-command "rm /tmp/tmp.ps")
  (message (concat "Saved to:  /home/user/" (buffer-name) ".pdf"))  
  )
Run Code Online (Sandbox Code Playgroud)

但是,在将其写入磁盘之前,我无法找到一种方法来启用或应用可视行次要模式到PostScript缓冲区,以便在输出中启用自动换行.

Rup*_*ick 5

获得视线模式的问题在于它插入了"软换行符"(PS渲染器会忽略它).解决方案是用硬换行替换这些.我想,下面的代码可以满足您的需求.请注意,我们在临时缓冲区中调用harden-newlines,以免弄乱当前文档.此外,我已将输出目的地更改为始终着陆/tmp/print.pdf.在/home没有任何警告的情况下覆盖你的文件似乎是不明智的!您随后可以随后移动PDF.

无论如何,你走了.这是你想要的吗?

(defun harden-newlines ()
  (interactive)
  "Make all the newlines in the buffer hard."
  (save-excursion
    (goto-char (point-min))
    (while (search-forward "\n" nil t)
      (backward-char)
      (put-text-property (point) (1+ (point)) 'hard t)
      (forward-char))))

(defun spool-buffer-given-name (name)
  (load "ps-print")
  (let ((tmp ps-left-header))
    (unwind-protect
        (progn
          (setq ps-left-header
                (list (lambda () name) 'ps-header-dirpart))
          (ps-spool-buffer-with-faces))
      (setf ps-left-header tmp))))

(defun print-to-pdf ()
  "Print the current file to /tmp/print.pdf"
  (interactive)
  (let ((wbuf (generate-new-buffer "*Wrapped*"))
        (sbuf (current-buffer)))
    (jit-lock-fontify-now)
    (save-current-buffer
      (set-buffer wbuf)
      (insert-buffer sbuf)
      (longlines-mode t)
      (harden-newlines)
      (spool-buffer-given-name (buffer-name sbuf))
      (kill-buffer wbuf)
      (switch-to-buffer "*PostScript*")
      (write-file "/tmp/print.ps")
      (kill-buffer (current-buffer)))
    (call-process "ps2pdf14" nil nil nil
                  "/tmp/print.ps" "/tmp/print.pdf")
    (delete-file "/tmp/print.ps")
    (message "PDF saved to /tmp/print.pdf")))
Run Code Online (Sandbox Code Playgroud)