Web Development with Clojure by Dmitri Sotnikov

Web Development with Clojure by Dmitri Sotnikov

Author:Dmitri Sotnikov
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf


Generating the Reports

The clj-pdf library uses syntax similar to Hiccup’s to define the elements in the document. The document itself is represented by a vector. The document vector must contain a map representing the metadata as its first element. The metadata is followed by one or more elements representing the document’s content.

Let’s create a namespace called reporting-example.reports and look at a few examples of creating PDF documents. We’ll use the pdf function to create the reports and the template macro to format the input data. We’ll also reference the reporting-example.db.core namespace so that we can call the read-employees function later on.

reporting-example/src/clj/reporting_example/reports.clj

​ (ns reporting-example.reports

​ (:require [reporting-example.db.core :as db]

​ [clj-pdf.core :refer [pdf template]]))

The pdf function accepts two arguments. The first can be either a vector representing the document or an input stream from which the elements are read. The second can either be a string representing the output file name or an output stream.

Let’s generate our first PDF by running the following code in the reporting-example.reports namespace:

​ (pdf

​ [{:header ​"Wow, that was easy"​}

​ [:list

​ [:chunk {:style :bold} ​"a bold item"​]

​ ​"another item"​

​ ​"yet another item"​]

​ [:paragraph ​"I'm a paragraph!"​]]

​ ​"doc.pdf"​)

As you can see, the report consists of vectors, each starting with a keyword identifying the type of element, followed by the metadata and the content. In the preceding report we have a list that contains three rows, followed by a paragraph. The PDF will be written to a file called doc.pdf in our project’s root. The contents of the file should look like the following figure:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.