Good to know. Thanks!
Morning!
π
Good morning
Β‘mawning
π
Morning All π
Hey I was wondering if anyone in here has any experience with converting HTML to PDF using Clojure?
Thx so much this is really awesome π
Specifically I want to render a PDF and send it as a web response from a reitit handler...
(I've got the actual render working with a clojure library that wraps the Java standard approach, but it's quite opinionated and wants to write the output straight to disk)
brb - morning standup
Morn'
Morning
I've done this fairly recently actually, I'd like to type it up but I'm basically in the meeting dimension for the foreseeable. From memory instead of writing to disk I was passing around the output of PDFBox, and there was an example from the book "Web Development with Clojure" which had the bulk of what I ended up doing. It was Liberator and not reitit but it should be pretty similar. If I get a chance to not be in meetings for more than 5 mins today I'll see if I can find the code
if I remember correctly finding the right java type to return that would actually convince the browser to render it as a pdf was the tricky bit
That's a bunch of useful insights and confirmations - particularly the Java type issue for returning a pdf straight to the browser. Thx If you do manage to find the code (srsly no pressure) that would be great, but thanks for this already π
morning
no problem, I can't guarantee it'll be today but I'll see what I can find π
:thumbsup:
so the main bulk of it is this:
(defn html->pdf-stream [html]
(with-open [out (ByteArrayOutputStream.)]
(let [builder (doto (PdfRendererBuilder.)
(.withHtmlContent html (str (io/resource ".")))
(.toStream out))]
(.run builder))
out))
that we then converted like so: (.toByteArray stream)
later on
and I think that's basically what we ended up returning
we were using pdfbox as the java pdf converter bit
unsure how much that helps but I hope it does a bit
you're likely to also have to set a media type header of "application/pdf"
but I think that's pretty much it for the hard parts