Question

I'm using clj-pdf to generate pdf files. As the README file suggests, the library provides some rudimentary templating options.

For example, given a vector of maps, such as:

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

and a template

(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]]))

The following output will be produced:

(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]])

However, I'm wondering how to use this template in pdf function. When I use

(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")

I got a invalid tag exception.

If I change (employee-template employees) to (first (employee-template employees)), it works however not as I expect. What is the correct way to use a template?

Was it helpful?

Solution

This works. Generate a new cell for each employee.

(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")

OTHER TIPS

Use apply and concat maybe?

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

I think there should be a better choice to do this,the concat may not be efficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top