improve base css style support / options. add custom font support
This commit is contained in:
parent
17e8876070
commit
823c8f0e7c
|
@ -1,9 +1,3 @@
|
||||||
body {
|
|
||||||
font: 12pt sans-serif;
|
|
||||||
line-height: 1.2;
|
|
||||||
background: #fff;
|
|
||||||
color: #000;
|
|
||||||
}
|
|
||||||
a {
|
a {
|
||||||
page-break-inside: avoid;
|
page-break-inside: avoid;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,18 +2,35 @@
|
||||||
(:require
|
(:require
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[clojure.string :as string]
|
[clojure.string :as string]
|
||||||
[clj-htmltopdf.css :as css])
|
[clj-htmltopdf.css :as css]
|
||||||
|
[clj-htmltopdf.utils :as utils])
|
||||||
(:import
|
(:import
|
||||||
[org.jsoup.nodes Document Element]
|
[org.jsoup.nodes Document Element]
|
||||||
[org.jsoup.parser Tag]))
|
[org.jsoup.parser Tag]))
|
||||||
|
|
||||||
|
(defn append-style-tag!
|
||||||
|
^Element [^Element parent css-styles]
|
||||||
|
(let [element (.appendElement parent "style")]
|
||||||
|
(.attr element "type" "text/css")
|
||||||
|
(.text element
|
||||||
|
(if (string? css-styles)
|
||||||
|
css-styles
|
||||||
|
(css/css->str css-styles)))))
|
||||||
|
|
||||||
|
(defn append-css-link-tag!
|
||||||
|
^Element [^Element parent href]
|
||||||
|
(let [element (.appendElement parent "link")]
|
||||||
|
(.attr element "type" "text/css")
|
||||||
|
(.attr element "rel" "stylesheet")
|
||||||
|
(.attr element "href" (str href))))
|
||||||
|
|
||||||
(def default-options
|
(def default-options
|
||||||
{:logging? false
|
{:logging? false
|
||||||
:base-uri ""
|
:base-uri ""
|
||||||
:include-base-css? true
|
:styles true
|
||||||
:page {:size :letter
|
:page {:size :letter
|
||||||
:orientation :portrait
|
:orientation :portrait
|
||||||
:margin "1.0in"}})
|
:margin "1.0in"}})
|
||||||
|
|
||||||
(defn get-final-options
|
(defn get-final-options
|
||||||
[options]
|
[options]
|
||||||
|
@ -48,25 +65,55 @@
|
||||||
(reduce #(assoc %1 (first %2) (second %2)) {}))]
|
(reduce #(assoc %1 (first %2) (second %2)) {}))]
|
||||||
[["@page" styles]]))
|
[["@page" styles]]))
|
||||||
|
|
||||||
(defn append-page-options-style-tag!
|
(defn append-stylesheet-link-tags!
|
||||||
^Element [^Element parent options]
|
[^Element parent stylesheets]
|
||||||
(let [styles (-> (:page options)
|
(doseq [stylesheet stylesheets]
|
||||||
(page-options->css)
|
(append-css-link-tag! parent stylesheet)))
|
||||||
(css/css->str))
|
|
||||||
element (.appendElement parent "style")]
|
|
||||||
(.attr element "type" "text/css")
|
|
||||||
(.text element styles)))
|
|
||||||
|
|
||||||
(defn append-base-css-link-tag!
|
(defn build-body-css-style
|
||||||
^Element [^Element parent]
|
[styles]
|
||||||
(let [element (.appendElement parent "link")]
|
[[:body
|
||||||
(.attr element "type" "text/css")
|
(merge
|
||||||
(.attr element "rel" "stylesheet")
|
{:font-family "sans-serif"
|
||||||
(.attr element "href" (str (io/resource "htmltopdf-base.css")))))
|
:font-size "12pt"
|
||||||
|
:line-height "1.3"
|
||||||
|
:background-color "#fff"
|
||||||
|
:color "#000"}
|
||||||
|
(if (map? styles)
|
||||||
|
(dissoc styles :styles :fonts)))]])
|
||||||
|
|
||||||
|
(defn build-font-face-styles
|
||||||
|
[styles]
|
||||||
|
(if-let [fonts (seq (:fonts styles))]
|
||||||
|
(mapv
|
||||||
|
(fn [{:keys [font-family src]}]
|
||||||
|
["@font-face"
|
||||||
|
{:font-family font-family
|
||||||
|
:src (str "url(\"" (utils/string->url-or-file src) "\")")}])
|
||||||
|
fonts)))
|
||||||
|
|
||||||
|
(defn build-base-css-styles
|
||||||
|
[styles]
|
||||||
|
(vec
|
||||||
|
(concat
|
||||||
|
(build-body-css-style styles)
|
||||||
|
(build-font-face-styles styles))))
|
||||||
|
|
||||||
|
(defn build-and-append-base-css-styles!
|
||||||
|
[^Element parent styles]
|
||||||
|
(append-style-tag! parent (build-base-css-styles styles))
|
||||||
|
(if-let [additional-styles (:styles styles)]
|
||||||
|
(cond
|
||||||
|
(sequential? additional-styles) (append-stylesheet-link-tags! parent additional-styles)
|
||||||
|
(string? additional-styles) (append-stylesheet-link-tags! parent [additional-styles]))))
|
||||||
|
|
||||||
(defn inject-options-into-html!
|
(defn inject-options-into-html!
|
||||||
[^Document doc options]
|
[^Document doc options]
|
||||||
(let [head-tag (-> doc (.select "head") (.first))]
|
(let [head-tag (-> doc (.select "head") (.first))
|
||||||
(if (:page options) (append-page-options-style-tag! head-tag options))
|
styles (:styles options)]
|
||||||
(if (:include-base-css? options) (append-base-css-link-tag! head-tag))
|
(if (:page options) (append-style-tag! head-tag (page-options->css (:page options))))
|
||||||
|
(cond
|
||||||
|
(sequential? styles) (append-stylesheet-link-tags! head-tag styles)
|
||||||
|
(string? styles) (append-stylesheet-link-tags! head-tag [styles])
|
||||||
|
(or (true? styles) (map? styles)) (build-and-append-base-css-styles! head-tag styles))
|
||||||
doc))
|
doc))
|
||||||
|
|
11
src/clj_htmltopdf/utils.clj
Normal file
11
src/clj_htmltopdf/utils.clj
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
(ns clj-htmltopdf.utils
|
||||||
|
(:import
|
||||||
|
[java.io File]
|
||||||
|
[java.net URL MalformedURLException]))
|
||||||
|
|
||||||
|
(defn string->url-or-file
|
||||||
|
[^String s]
|
||||||
|
(try
|
||||||
|
(URL. s)
|
||||||
|
(catch MalformedURLException _
|
||||||
|
(File. (str "file:" s)))))
|
Loading…
Reference in a new issue