gpt4 book ai didi

clojure - clj-pdf 中的模板

转载 作者:行者123 更新时间:2023-12-04 00:28:38 25 4
gpt4 key购买 nike

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

例如,给定一个 map 向量,如:

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

我遇到了一个无效标签异常。

如果我将 (employee-template employees) 更改为 (first (employee-template employees)),它会按我的预期工作。模板的正确使用方法是什么?

最佳答案

这行得通。为每个员工生成一个新单元格。

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

关于clojure - clj-pdf 中的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23456672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com