So I’ve now reached a point where I’m moderately satisfied with the number of features that one can use to customize and change their lists as they need. While I would like to go back and smooth over some rough edges, it’s time to move on to a different area of work and continue my march to feature completeness.
It’s time to enable exporting of the list. While it’s true that I’m designing this as a mobile-friendly web app, I feel that some users might want a simpler way to access their list when it actually comes time to use it. To that end, I want to have several ways to export the list. The most important of these are the ability to export to a pdf, and the ability to email the list. If possible, I would also like to give the user the ability to text the list to themselves, but I’m not as certain of how to do that.
But let’s start with the pdf. After some googling and searching, I found this tutorial, which makes use of wkhtmltopdf and pdfkit to turn a rendered template into a pdf. After installing modules and adding wkhtmltopdf to my PATH, I began to code.
First I created a new route that would render the list in a pdf:
This route renders a jinja template the same way any of the other routes do, except it then turns that template into a pdf. From there, it configures the client to expect a pdf as a response, and returns the response. Here’s the template that I first used:
I didn’t use my layout.html template because I didn’t want the navbar, but I did include bootstrap (for now), mainly because I wanted to keep everything the same style. I gave the program a test, and the template rendered successfully:
Excellent. Now, I could have been done here, but I decided that I wanted to add a little extra customization to how the pdf was generated. Plus, I needed to add a way to access the /print route from the main list page.
I went into my forms.py folder and created a new form, ExportToPDFForm, that contained a few checkboxes for different aspects of the data that a person might want to include in their grocery list, such as the recipes and whether or not the program should display checked lines.
Then, I added this new form to the list page, attaching it to a modal. I seem to be using a lot of these, but can you blame me? I like the way they look and they add additional features without cluttering up the main page too much. If it becomes overbearing I may change it, but the relentless pace of progress stops for no one.
I attached the modal to a “Print” <button> and placed it underneath the list name:
This created a nice button…
…which, when clicked, produces a nice modal…
Now that I had the data, I just needed to do something with it. As you may have noticed in the form data above, I set the form to redirect to my /print route. Then I rewrote the route so that it fetched more of the necessary data, including the RawLines and the RecipeLists associated with I also made a quick rewrite to my list sorting function, moving it to utils.py so that I could reorder the list on the print screen.
Finally, I went back into my pdf template and changed the code so that it displayed the proper information, but only if the relevant boxes had been checked:
A lot of templating going on here, and I’m still not totally satisfied with how the printed list looks. But the pieces are in place, and I plan to give it another pass when I come back through for beautification.