Render letter templates as PNGs and PDFs
Depends on and implements:
- [ ] alphagov/notifications-utils#84
- [ ] alphagov/notifications-utils#86
## Add an endpoint for generating a PDF of a letter
Previewing a letter is different to previewing an email or text message because:
- a letter has a layout
- the layout is fixed, ie it doesn’t depend on the user’s device
- the ‘send yourself a test’ feature won’t be as useful because it has a lead time, so the feedback loop will be much longer
For these reasons a HTML-only preview of the letter won’t be enough (we don’t reckon). A PDF is more appropriate because:
- it can replicate the layout of the letter exactly
- it has a fixed size
- it is a print format, so the user could even print themselves a copy locally to get a feel for how it will look
This commit makes use of [Flask WeasyPrint](https://pythonhosted.org/Flask-WeasyPrint/) to take a HTML representation of the letter, convert it to a PDF and serve it back to the user.
The actual work to generate the HTML and specify the layout is done in utils, same as we do for rendering other messages.
## Add endpoint for generating an image of a letter
The PDF preview is all good, but it’s hard, finickity, and feels dirty to embed a PDF in a web page. It’s a more natural thing to embed an image in a web page.
So this commit adds another endpoint to return an image of a letter template. It generates this image from the PDF preview, so the stack looks like:
1. `template.png` (generated in admin)
2. `template.pdf` (generated in admin)
3. HTML preview (generated by a `Renderer` in utils)
4. `Template` instance
5. serialised template from API
6. Template stored in database
The library used to convert the PDF to an image is [Wand](http://docs.wand-py.org/en/0.4.4/), which binds to ImageMagick underneath. So in order to get this working locally on a Mac you will probably need to do: `brew install imagemagick ghostscript cairo pango`.
To get it working on Ubuntu/EC2 is an exercise left to the reader…
## Move code for rendering messages/templates → utils
Utils is better structured to handle the logic of what thing to show for what template type, especially now that what we show for different template types in different contexts has diverged significantly.
See alphagov/notifications-utils@6b39c1a for an example of this code moving into utils.
This means that we automagically get the image preview shown in admin, linked to from the image of the PDF.