关于Clojure:如何使用Ring投放流pdf

How to serve the stream pdf with ring

我正尝试直接通过ring / compojure提供clj-http生成的文档。

我以为ring.util / piped-output-stream可以用,但似乎我对这里不了解...

此:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(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)})

导致响应为空...

我需要做些什么?

谢谢,

Mathias


我认为您的代码中可能没有括号(请查看下面的laminat-pdf-t函数-我对其进行了一些微调)。

这正是我所做的(首先使用leiningen 2.3.4创建一个名为pdf-play的项目),并且它在IE 11.0.9600.16521Firefox 28.0Chrome 33.0.1750.154中正确显示了PDF(均在Windows上-抱歉,这些是仅安装了我没有Linux或Mac的浏览器,但我认为浏览器没有任何不同):

project.clj

1
2
3
4
5
6
(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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(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))

然后在命令提示符下启动它,如下所示:

1
lein ring server

,并在浏览器中浏览了一下,其中有一个印有"某些文本"的PDF。