How to serve the stream pdf with ring

╄→尐↘猪︶ㄣ 提交于 2019-12-22 04:49:14

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/22581644/how-to-serve-the-stream-pdf-with-ring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!