Search code examples
lisppdf-generationcommon-lispoutputstream

cl-pdf output error


I'm trying to use cl-pdf for some fairly basic PDF generation, but I'm getting tripped up at the examples (which is embarassing to say the least).

When I run the first example included in the package

(defun example1 (&optional (file #P"/tmp/ex1.pdf"))
  (pdf:with-document ()
    (pdf:with-page ()
      (pdf:with-outline-level ("Example" (pdf:register-page-reference))
        (let ((helvetica (pdf:get-font "Helvetica")))
          (pdf:in-text-mode
           (pdf:set-font helvetica 36.0)
           (pdf:move-text 100 800)
           (pdf:draw-text "cl-pdf: Example 1"))
          (pdf:translate 230 500)
          (loop repeat 150
         for i = 0.67 then (* i 1.045)
         do (pdf:in-text-mode
             (pdf:set-font helvetica i)
             (pdf:set-rgb-fill (/ (random 255) 255.0)
                               (/ (random 255) 255.0)
                               (/ (random 255) 255.0))
             (pdf:move-text (* i 3) 0)
             (pdf:show-text "cl-typesetting"))
           (pdf:rotate 13)))))
    (pdf:write-document file)))

by running (example1 #P"/home/inaimathi/Desktop/ex1.pdf") it gives me this error

#<SB-SYS:FD-STREAM for "file /home/inaimathi/Desktop/test.pdf" 
{CF9D931}> is not a binary output stream. 
    [Condition of type SIMPLE-TYPE-ERROR]

Restarts:
 0: [ABORT] Exit debugger, returning to top level.

The same thing happens when I call (example1), or when I do

(with-open-file 
     (test-file #P"/home/inaimathi/Desktop/ex1.pdf" 
     :direction :output :if-does-not-exist :create) 
   (example1 test-file))

Finally, if I try

(with-open-file 
     (test-file #P"/home/inaimathi/Desktop/ex1.pdf" 
     :direction :output :if-does-not-exist :create 
     :element-type '(unsigned-byte 8)) 
   (example1 test-file))

I get the error

#<SB-SYS:FD-STREAM for "file /home/inaimathi/Desktop/test.pdf" 
{D197C99}> is not a character output stream.
   [Condition of type SIMPLE-TYPE-ERROR]

Restarts:
 0: [ABORT] Exit debugger, returning to top level.

Is there a way to declare a binary character stream? How do I get simple output out of cl-pdf? I'm using SBCL straight out of the debian repos (which is 1.0.29, I think), in case it matters.


Solution

  • (setf pdf:*compress-streams* nil) should help. It's trying to write binary data to a character stream, and while that works on LispWorks and some other systems, it doesn't work everywhere and particularly not on SBCL.