[racket] pdf-dc%: Generating a pdf with image embedded in jpeg format?

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Mon Dec 8 08:48:34 EST 2014

What's the final destination for these images? A pdf file, I guess? Is
it combined with other stuff in the pdf file, or does the pdf file
have just the image in it?

Robby


On Sun, Dec 7, 2014 at 9:53 PM, Daniel Prager <daniel.a.prager at gmail.com> wrote:
> I have some code that writes individual images to pdfs (see below).
>
> If there's a lot of "texture" in the images, these files can be very large
> indeed.
>
> I assume that by default the image is encoded as a kind of embedded png.
>
> I would like to be able to change the compression -- e.g. specify jpeg.
>
> My currently workaround is to save from Racket as a png, and then convert
> externally via ImageMagick: first to jpeg, then to pdf.
>
> (How) can I achieve the same result directly from Racket?
>
>
> Many thanks
>
> Dan
>
> ----
>
> #lang racket
>
> (require racket/draw
>          (only-in 2htdp/image
>                   save-image
>                   image-width
>                   image-height
>                   scale))
>
> (define (image->bitmap image)
>   (let* ([width (image-width image)]
>          [height (image-height image)]
>          [bm (make-bitmap width height)]
>          [dc (send bm make-dc)])
>     (send dc clear)
>     (send image draw dc 0 0 0 0 width height 0 0 #f)
>     bm))
>
> (define (image->pdf image output-file [mag 1.0])
>   (define dc
>     (new pdf-dc%
>          [ interactive #f ]
>          [ use-paper-bbox #f ]
>          [ width (* 0.8 mag (image-width image))]   ; Default scale is 0.8
> 0.8
>          [ height (* 0.8 mag (image-height image))]
>          [ output output-file ]))
>
>   (send* dc
>     (scale mag mag)
>     (start-doc "useless string")
>     (start-page))
>
>   (send dc draw-bitmap (image->bitmap image) 0 0)
>
>   (send* dc
>     (end-page)
>     (end-doc)))
>
> (module+ test
>   (require (only-in 2htdp/image
>                     circle text square overlay above))
>
>   (define im
>     (overlay
>      (above
>       (circle 50 'solid 'red)
>       (text "This is a red circle" 12 'black))
>      (square 150 'solid 'lightblue)))
>
>   (image->pdf im "image-test.pdf")
>   (save-image im "image-test.png"))
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
>

Posted on the users mailing list.