Hi Will,
What you suggest sounds reasonable. The Buffer (Appendable, a Writer in the
end) used by the freemarker renderer can be easily replaced, for example.
I have done something similar in 2 situations (but never as impressive as
rendering a PDF):
1) My resources-scss module (available on forge) does first freemarker and then
SASS (SCSS) parsing before returning a CSS resource. To do this it parses the
freemarker to a temporary buffer (StringWriter) --> so there is some example
code there which wraps the rendering in a temporary buffer.
2) We have a newsletter module which renders newsletters either to HTML
(webpages, newsletter "microsite") or MIME (email with HTML and text and inline
images). This is implemented as template variations, with the standard template
rendering to HTML and a variation template which renders to MIME. So that
sounds similar to what you want to do with PDF. All the newsletter templates
are implemented in freemarker, I found it to be no problem to produce a MIME
multipart-related message using freemarker to output it. Even though some of
the mime-parts (images) are binary attachments, by using base64 content
encoding this was no great difficulty.
Note that the solution depends on careful writing of the template, and some
model support. For example, when rendering the HTML, I render the image links
using a custom model function, not stk.imageLink(). When rendering the standard
version, the image link is rendered normally. When rendering the HTML mime-part
of the email, the image-link is rendered as eg ("cid:7383030303" - ie an
internal mime-part reference). Also, the image and the ID is remembered.
Afterwards, all the images can be attached to the mime-message as base64 using
another model function.
--> I think a PDF could be generated in much same way.
You can set the mime type, I believe, by configuring the variation for a
certain extension. if that extension has a mime-type configured in magnolia the
content-type should be set correctly automatically.
Regards from Vienna,
Richard
> -----Ursprüngliche Nachricht-----
> Von: [email protected] [mailto:user-list-owner@magnolia-
> cms.com] Im Auftrag von Will Scheidegger (via Magnolia Forums)
> Gesendet: Freitag, 20. Juni 2014 13:46
> An: Magnolia User List
> Betreff: [magnolia-user] Generating PDF from page with template variation
> and custom renderer
>
> Dear Magnolians
>
> I would like to render a magnolia page as PDF. Of course I could create my
> own filter which then gets the HTML-code using a HTTP client, then convert it
> to a PDF with one of the HTML-2-PDF libraries. But as Magnolia is built in a
> super flexible and modular way I would like to do it properly:
>
> - create a template variation
> - create a custom PDF renderer used by this variation
> - return the generated PDF just like you would return the HTML page making
> full use of all the Magnolia features like cache and ACLs.
>
> My current guess is to have a "onRender" method in my renderer which will
> render the content regularly as HTML, but will not write into the render
> context but rather a buffer. From there the HTML should be converted to a
> PDF (with the help of Flying Saucer or something alike) and then write the
> PDF to the render context.
>
> - Does this seem like a good plan?
> - As I only have access to the render context and not to the response
> directly, here can I set the mime type (or is it set automatically)?
> - Has anyone already developed something like this and would like to share?
>
> Thanks!
> -will
>
> --
> Context is everything: http://forum.magnolia-
> cms.com/forum/thread.html?threadId=f0b05b4e-814c-4317-af2c-
> 8fef2203950a
>
>
> ----------------------------------------------------------------
> For list details, see http://www.magnolia-cms.com/community/mailing-
> lists.html
> Alternatively, use our forums: http://forum.magnolia-cms.com/
> To unsubscribe, E-mail to: <[email protected]>
> ----------------------------------------------------------------
----------------------------------------------------------------
For list details, see http://www.magnolia-cms.com/community/mailing-lists.html
Alternatively, use our forums: http://forum.magnolia-cms.com/
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------