Skip to content
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

Merged
merged 8 commits into from
Dec 14, 2016
Merged

Render letter templates as PNGs and PDFs #1037

merged 8 commits into from
Dec 14, 2016

Conversation

quis
Copy link
Member

@quis quis commented Nov 28, 2016

Depends on and implements:


image

image

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 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, 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.

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/
@quis quis force-pushed the pdf-preview branch 3 times, most recently from 3ec93e7 to 17c99bf Compare December 14, 2016 13:49
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
Copy link
Contributor

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

Copy link
Contributor

@leohemsted leohemsted Dec 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

html5lib/html5lib-python#282

ah they've provided alternative versions. can you try: html5lib==1.0b8?

Copy link
Member Author

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 😐
mock_has_permissions,
fake_uuid
):
client.login(api_user_active)
Copy link
Contributor

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)

@quis quis merged commit ecf7f5a into master Dec 14, 2016
@quis quis deleted the pdf-preview branch December 14, 2016 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants