chickadee » pdf

pdf

Description

A library for PDF generation.

Author

Marc Battyani and Bruce Butterfield

Repository

This egg is hosted on the CHICKEN Subversion repository:

https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/pdf

If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.

Requirements

srfi-1, regex, format

Documentation

Examples

(import scheme (chicken base) (chicken random) (prefix pdf pdf:))
(define random pseudo-random-integer)

(define-constant pi 3.14159) 

(define-syntax dotimes
  (syntax-rules ()
    ((_ (index maxval) body ...)
     (do ((index 0 (+ index 1)))
       ((= index maxval))
       body ...))))

(define (ex1)
  (pdf:with-document-to-file "ex1.pdf"
   (let ((helvetica (pdf:build-font "Helvetica"))
         (courier (pdf:build-font "Courier")))
     (pdf:with-page
      (pdf:in-text-mode
       (pdf:set-font (pdf:font-name helvetica) 36)
       (pdf:move-text 100 750)
       (pdf:draw-text "scm-pdf: Example 1"))
      (pdf:in-text-mode
       (dotimes (i 25)
                    (pdf:set-font (pdf:font-name helvetica) (* i 1.5))
                    (pdf:move-text (+ i 5) (+ i 10))
                    (pdf:draw-text "Helvetica")))
      (pdf:in-text-mode
       (dotimes (i 25)
                    (pdf:set-font (pdf:font-name courier) (* i 1.5))
                    (pdf:move-text (+ i 5) (- 50 (+ i 10)))
                    (pdf:draw-text "Courier")))))))

(define (ex2)
  (pdf:with-document-to-file "ex2.pdf"
   (let ((helvetica (pdf:build-font "Helvetica")))
     (pdf:with-page
      (pdf:in-text-mode
       (pdf:set-font (pdf:font-name helvetica) 36)
       (pdf:move-text 100 750)
       (pdf:draw-text "scm-pdf: Example 2"))
      (pdf:translate 230 500)
      (do ((j 0 (+ j 1))
           (i 0.67 (* i 1.045)))
        ((= j 101))
        (pdf:in-text-mode
         (pdf:set-font (pdf:font-name helvetica) i)
         (pdf:move-text (* i 3) 0)
         (pdf:draw-text "rotation"))
        (pdf:rotate 18)))
     (pdf:with-page
      (pdf:in-text-mode
       (pdf:set-font (pdf:font-name helvetica) 40)
       (pdf:move-text 230 500)
       (pdf:draw-text "That's All, Folks!"))))))

(define (ex3)
  (pdf:with-document-to-file "ex3.pdf"
   (let ((helvetica (pdf:build-font "Helvetica")))
     (pdf:with-page (800 1000)
      (pdf:in-text-mode
       (pdf:set-font (pdf:font-name helvetica) 36.0)
       (pdf:move-text 100 900)
       (pdf:draw-text "scm-pdf: Example 3"))
      (pdf:move-to (+ 10 (random 500))(+ 10 (random 400)))
      (pdf:set-gray-fill 0.5)
      (dotimes (i 50)
               (pdf:line-to (+ 50 (random 500)) (+ 50 (random 400))))
      (pdf:close-even-odd-fill-and-stroke)
      (pdf:move-to (+ 50 (random 500))(+ 400 (random 400)))
      (pdf:set-rgb-fill 0.5 0.5 0.8)
      (pdf:set-rgb-stroke 0.9 0.5 0.1)
      (dotimes (i 50)
               (pdf:bezier2-to (+ 50 (random 500)) (+ 400 (random 400))
                           (+ 50 (random 500)) (+ 400 (random 400))))
      (pdf:close-even-odd-fill-and-stroke)))))

(define (ex4)
  (pdf:with-document-to-file "ex4.pdf"
   (let ((helvetica (pdf:build-font "Helvetica")))
     (pdf:with-page
       (pdf:in-text-mode
        (pdf:set-font (pdf:font-name helvetica) 36.0)
        (pdf:move-text 100 750)
        (pdf:draw-text "scm-pdf: Example 4"))
       (pdf:set-rgb-stroke 0.1 0.1 0.1)
       (pdf:set-rgb-fill 0.8 0.8 0.8)
       (let ((x 50) (y 600))
         (dotimes (i 2)
                  (pdf:rectangle x y 500 140 10)
                  (pdf:close-fill-and-stroke)
                  (set! y (- y 180))))
       (pdf:translate 50 670)
       (let ((x 50) (y 0))
         (do ((i 0 (+ i 1))
              (j 8 (* j 1.05)))
           ((= i 4))
           (pdf:set-rgb-fill (* 0.1 j) (* 0.01 j) (* 0.02 j))
           (pdf:circle x y (* 4 j))
           (pdf:close-fill-and-stroke)
           (pdf:ellipse (+ x 250) y (* 5 j) (* 4 j))
           (pdf:close-fill-and-stroke)
           (set! x (+ x 50))))
       
       (pdf:translate 0 -180)
       (pdf:regular-polygon 150 0 50 7 8)
       (pdf:close-fill-and-stroke)
       (pdf:star 350 0 50 30 6 5)
       (pdf:close-fill-and-stroke)
       
       (pdf:set-rgb-fill 0.8 0.6 0.2)
       (pdf:regular-polygon 150 0 30 5 4)
       (pdf:close-fill-and-stroke)
       (pdf:star 350 0 40 20 4 6)
       (pdf:close-fill-and-stroke)
       
       (pdf:set-rgb-fill 0.4 0.8 0.7)
       (pdf:regular-polygon 150 0 15 3 3)
       (pdf:close-fill-and-stroke)
       (pdf:star 350 0 35 10 12 1)
       (pdf:close-fill-and-stroke)
       
       (pdf:set-line-width 0.5)
       (do ((r 2 (+ r 2)))
         ((= r 100))
         (pdf:set-rgb-stroke (* 0.01 (random 100))
                             (* 0.01 (random 100))
                             (* 0.01 (random 100)))
         (pdf:arc 250 -230 r (* pi 0.001 (random 2000))
                             (* pi 0.001 (random 2000)))
         (pdf:stroke))))))


(ex1)
(ex2)
(ex3)
(ex4)