如何使用环服务流pdf [英] How to serve the stream pdf with ring

查看:92
本文介绍了如何使用环服务流pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

此:

(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函数-我对其进行了一些微调).

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).

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

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):

(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

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

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

这篇关于如何使用环服务流pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆