Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve the stream pdf with ring

I'm trying to serve a clj-http generated document directly via ring/compojure.

I thought ring.util/piped-output-stream would work, but it seems I'm not understanding something here...

This:

(defn laminat-pdf-t
  [natno]
  (piped-input-stream
   (fn [output-stream])
   (pdf
    [ {:title (str "Omanimali-Kuscheltierpass" natno)
       :orientation :landscape
       :size :a6
       :author "Omanimali - Stefanie Tuschen"
       :register-system-fonts true
       }
      ;; [:svg {} (clojure.java.io/file
      ;;           (str "/einbuergern/" natno "/svg" ))]
      [:paragraph "Some Text"]      ]
    output-stream)))

(defn laminat-pdf
  "generate individualized cuddly toy passport page"
  [natno]
  {:headers {"Content-Type" "application/pdf"}
   :body (laminat-pdf-t natno)})

leads to an empty response...

What do I need to do differently?

Thanks,

Mathias

like image 806
mathiasp Avatar asked Mar 22 '14 18:03

mathiasp


1 Answers

I think you may have a bracket out of place in your code (look at the laminat-pdf-t function below - I tweaked it slightly).

Here's exactly what I did (first creating a project with leiningen 2.3.4 called pdf-play) and it displayed a PDF correctly in IE 11.0.9600.16521, Firefox 28.0 and Chrome 33.0.1750.154 (all on Windows - sorry these are the only browsers that I have installed and I don't have a Linux or Mac box but I don't think the browser makes any difference):

project.clj

(defproject pdf-play "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [compojure "1.1.6"]
                 [clj-pdf "1.11.15"]]
  :plugins [[lein-ring "0.8.10"]]
  :ring {:handler pdf-play.handler/app})

src/pdf_play/handler.clj

(ns pdf-play.handler
  (:use compojure.core
        ring.util.io
        clj-pdf.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]))

(defn laminat-pdf-t
  [natno]
  (piped-input-stream
   (fn [output-stream]
     (pdf
       [{:title (str "Omanimali-Kuscheltierpass" natno)
         :orientation :landscape
         :size :a6
         :author "Omanimali - Stefanie Tuschen"
         :register-system-fonts true
         }
         ;; [:svg {} (clojure.java.io/file
         ;;           (str "/einbuergern/" natno "/svg" ))]
         [:paragraph "Some Text"]]
       output-stream))))

(defn laminat-pdf
  "generate individualized cuddly toy passport page"
  [natno]
  {:headers {"Content-Type" "application/pdf"}
   :body (laminat-pdf-t natno)})

(defroutes app-routes
  (GET "/" [] (laminat-pdf 1234))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app (handler/site app-routes))

Then started it at the command prompt like so:

lein ring server

and had a look in the browser and there was a PDF with "Some Text" printed in it.

like image 104
kmp Avatar answered Oct 31 '22 11:10

kmp