模板clj-pdf

时间:2014-05-04 13:14:26

标签: clojure clj-pdf

我使用clj-pdf生成pdf文件。正如README文件所示,该库提供了一些基本的模板选项。

例如,给定一个地图矢量,例如:

(def employees
  [{:country "Germany",
    :place "Nuremberg",
    :occupation "Engineer",
    :name "Neil Chetty"}
   {:country "Germany",
    :place "Ulm",
    :occupation "Engineer",
    :name "Vera Ellison"}])

和模板

(def employee-template
  (template
    [:paragraph
     [:heading $name]
     [:chunk {:style :bold} "occupation: "] $occupation "\n"
     [:chunk {:style :bold} "place: "] $place "\n"
     [:chunk {:style :bold} "country: "] $country
     [:spacer]]))

将生成以下输出:

(employee-template employees)

([:paragraph [:heading "Neil Chetty"] 
  [:chunk {:style :bold} "occupation: "] "Engineer" "\n" 
  [:chunk {:style :bold} "place: "] "Nuremberg" "\n" 
  [:chunk {:style :bold} "country: "] "Germany" [:spacer]] 
 [:paragraph [:heading "Vera Ellison"] 
  [:chunk {:style :bold} "occupation: "] "Engineer" "\n" 
  [:chunk {:style :bold} "place: "] "Ulm" "\n" 
  [:chunk {:style :bold} "country: "] "Germany" [:spacer]])

但是,我想知道如何在pdf函数中使用此模板。当我使用

(pdf
  [[:heading "Heading 1"]
   [:table
    {:width 100  :border false  :cell-border false  :widths [30 70]  :offset 35}
    [[:cell {:align :right}
      (employee-template employees)]
     [:cell "dummy"]]]
   [:heading "Heading 2"]]
  "output.pdf")

我收到invalid tag例外。

如果我将(employee-template employees)更改为(first (employee-template employees)),则无法正常工作。使用模板的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

这很有效。为每个员工生成一个新单元格。

(pdf
  [[:heading "Heading 1"]
   [:table {:width 100  :border false  :cell-border false :widths [30 70]  :offset 35}
    (for [e (employee-template employees)]
      [:cell {:align :right} e])]
   [:heading "Heading 2"]]
  "output.pdf")

答案 1 :(得分:0)

使用apply和concat可能吗?

(pdf
    (apply concat
           [[:heading "Heading 1"]]
           (employee-template employees)
           [[:heading "Heading 2"]])
    "output.pdf")

我认为这样做应该有更好的选择,concat可能效率不高。