Tuesday, July 06, 2010

Creating PDF files from Common Lisp

The CL-PDF library allows you to create PDF files directly from common lisp. The easiest way to use cl-pdf (or for that matter many other libraries) is to download and install LibCL. CL-PDF comes with LibCL and makes the installation a snap.

I have a hello world pdf program written.
;; CL-PDF Hello World
;;
;; Use ASDF to load the CL-PDF library
;;
(asdf:operate 'asdf:load-op 'cl-pdf)
;;
;; Function to generate a Hello World pdf file.
;; On calling this function, you should see a pdf file
;; with a big Hello World in the center!
;;
(defun hello-world (file author title greeting)
"Create a Hello World PDF file"
(pdf:with-document (:title title :author author)
(pdf:with-page ()
(pdf:register-page-reference "page 1")
(pdf:with-outline-level ("Page 1" "page 1")
(let ((helvetica (make-instance 'pdf:font)))
(pdf:in-text-mode
(pdf:set-font helvetica 36.0)
(pdf:move-text 200 400)
(pdf:draw-text greeting)))))
(pdf:write-document file)))
(hello-world #P"d:/tmp/hello_world.pdf" "Sid H" "Hello World" "Hello World")

Just run this and it will generate a PDF with a big Hello World!

No comments: