-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Render letter templates as PNGs and PDFs #1037
Conversation
ca16d8f
to
8366c99
Compare
35a4f35
to
416eecd
Compare
Reasons here: quis/WeasyPrint@99718c2
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 think). A PDF is more appropriate because: - it can replicate the layout of the letter exactly - 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 [1] 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. 1. https://pythonhosted.org/Flask-WeasyPrint/
The PDF preview is all good, but it’s hard, finickeity 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[1], 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… 1. http://docs.wand-py.org/en/0.4.4/
Depends on and implements: - [x] alphagov/notifications-utils#84 - [ ] alphagov/notifications-utils#86
3ec93e7
to
17c99bf
Compare
Brings in: - [ ] alphagov/notifications-utils#94
The breaking change page was taking the rendered template and saving that if the user confirmed the change. This meant that templates could be saved with `<span class="placeholder">…</span>` in their subject line for example. This commit fixes it so that it uses whatever data the user submitted, not the rendered version of this.
@@ -3,6 +3,9 @@ Flask==0.10.1 | |||
Flask-Script==2.0.5 | |||
Flask-WTF==0.11 | |||
Flask-Login==0.3.2 | |||
Flask-WeasyPrint==0.5 | |||
git+https://github.com/quis/WeasyPrint.git@older-html5lib#egg=WeasyPrint==0.33 | |||
html5lib==0.9999999 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes i forgot about how braindead this library was
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah they've provided alternative versions. can you try: html5lib==1.0b8
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
html5lib version numbers look like 0.999, 0.9999, etc. This was making people unhappy, so they’ve added version numbers like 1.0b1, 1.0b2, etc which map to the numbers with lots of nines. The equivalent version for 0.9999999 (7 nines) is 1.0b8 😐
Includes: - [x] alphagov/notifications-utils#102
mock_has_permissions, | ||
fake_uuid | ||
): | ||
client.login(api_user_active) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just fyi i made logged_in_client
in my quest for better test fixtures to help remove some of these mocks :) it includes all the login decorators so you dont need to have them in your test (though it only currently works with api_user_active)
Depends on and implements:
Template
andRenderer
classes notifications-utils#84Template
andRenderer
classes notifications-utils#94Add an endpoint for generating a PDF of a letter
Previewing a letter is different to previewing an email or text message because:
For these reasons a HTML-only preview of the letter won’t be enough (we don’t reckon). A PDF is more appropriate because:
This commit makes use of 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:
template.png
(generated in admin)template.pdf
(generated in admin)Renderer
in utils)Template
instanceThe library used to convert the PDF to an image is Wand, 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
.It should work on Ubuntu/EC2 once this is merged: https://github.com/alphagov/notifications-aws/pull/106
Use PDF link renderer to preview letters
Shows the image preview on the page, and links to the PDF.