[cl-pdf-devel] Concatenating PDF files

Marc Battyani marc.battyani at fractalconcept.com
Mon Nov 14 23:17:59 UTC 2005


Tim Daly Jr. <tim at tenkan.org> wrote:
> On Mon, 2005-11-14 at 21:08 +0100, Edi Weitz wrote:
> ...
> > Nevertheless, at the moment I don't even know enough of CL-PDF to be
> > able to concatenate the files without wading through the source code
> > for a couple of hours.  So, if someone could provide me with some
> > lines of example code to get me started I'd be /very/ thankful.
>
> Something like this would be a start, I think:
>
>     (require :cl-pdf)
>     (require :cl-pdf-parser)
>     (in-package :pdf)
>
>     (defun concat-pdfs (out-file &rest in-files)
>       (with-document ()
>         (dolist (in-file in-files)
>           (let* ((doc (read-pdf-file in-file))
>                  (root (root-page doc)))
>             (loop for page across (pages root) do
>                   (with-page ()
>                     (let ((template (make-template-from-page page)))
>                       (add-templates-to-page template)
>                       (draw-template template))))))
>         (write-document out-file)))

Cool!

> > but I'd rather do it all in Lisp if possible.

Here is the same one with the page numbers added:

(defun concat-pdfs (out-file &rest in-files)
  (with-document ()
    (let ((page-number 0))
      (dolist (in-file in-files)
        (let* ((doc (read-pdf-file in-file))
               (root (root-page doc)))
          (loop for page across (pages root) do
               (with-page ()
                 (let ((template (make-template-from-page page))
                       (helvetica (pdf:get-font "Helvetica")))
                   (add-templates-to-page template)
                   (draw-template template)
                   (pdf:in-text-mode
                    (pdf:set-font helvetica 14.0)
                    (pdf:move-text 500 10)
                    (pdf:set-gray-fill 0.0)
                    (pdf:draw-text (format nil "Page ~d" (incf
page-number)))))))))
      (write-document out-file))))

I will add this to the cl-pdf examples. :)

> Amen, brother Maynard!

Yep. Lisp rulez!

Marc





More information about the cl-pdf-devel mailing list