improve base css style support / options. add custom font support

This commit is contained in:
Gered 2017-04-02 15:16:48 -04:00
parent 17e8876070
commit 823c8f0e7c
3 changed files with 82 additions and 30 deletions

View file

@ -1,9 +1,3 @@
body {
font: 12pt sans-serif;
line-height: 1.2;
background: #fff;
color: #000;
}
a {
page-break-inside: avoid;
}

View file

@ -2,15 +2,32 @@
(:require
[clojure.java.io :as io]
[clojure.string :as string]
[clj-htmltopdf.css :as css])
[clj-htmltopdf.css :as css]
[clj-htmltopdf.utils :as utils])
(:import
[org.jsoup.nodes Document Element]
[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
{:logging? false
:base-uri ""
:include-base-css? true
:styles true
:page {:size :letter
:orientation :portrait
:margin "1.0in"}})
@ -48,25 +65,55 @@
(reduce #(assoc %1 (first %2) (second %2)) {}))]
[["@page" styles]]))
(defn append-page-options-style-tag!
^Element [^Element parent options]
(let [styles (-> (:page options)
(page-options->css)
(css/css->str))
element (.appendElement parent "style")]
(.attr element "type" "text/css")
(.text element styles)))
(defn append-stylesheet-link-tags!
[^Element parent stylesheets]
(doseq [stylesheet stylesheets]
(append-css-link-tag! parent stylesheet)))
(defn append-base-css-link-tag!
^Element [^Element parent]
(let [element (.appendElement parent "link")]
(.attr element "type" "text/css")
(.attr element "rel" "stylesheet")
(.attr element "href" (str (io/resource "htmltopdf-base.css")))))
(defn build-body-css-style
[styles]
[[:body
(merge
{:font-family "sans-serif"
: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!
[^Document doc options]
(let [head-tag (-> doc (.select "head") (.first))]
(if (:page options) (append-page-options-style-tag! head-tag options))
(if (:include-base-css? options) (append-base-css-link-tag! head-tag))
(let [head-tag (-> doc (.select "head") (.first))
styles (:styles options)]
(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))

View 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)))))