Fancy pants PDFs

I’ve always wondered how to easily create PDFs, haven’t you? Sure you have, right?

What I mean by “easily” is how to do it quickly and efficiently with code, which is to say that I don’t mean doing it via point-n-click. I mean by coding stuff up. If this puts you off, then exit right now, because this stuff assumes familiarity with computer science and programming.

I am writing this post because I can’t find anywhere online that combines all the conceptual overhead needed to do a project that makes a bunch of dynamically-generated PDFs, like the one I just completed does. (Making school reports for the end-of-semester grades and comments.)

Wouldn’t you know it, but you can make pixel-perfect PDFs if you already know the web stack: HTML/CSS. There is a command line tool called wkhtmltopdf.org that will take that stuff and churn out your pdf file, which just uses webkit to render it all, and there are plenty of wrappers around that for your favorite programming environment. For me, that’s python, which means pdfkit.

Using wrappers at first can be really perplexing, because there usually isn’t too much in the way of documentation. But there’s a reason for that, because the docs to be consulted should be of the thing being wrapped, not the pretty wrapping itself.

I always use Linux for my programming environment, using git, vagrant and virtual box, and never my actual Mac hosting system. So installing pdfkit/wkhtmltopdf took a bit of research, since installing it with a package manager like apt-get only gets you a crippled version of the software.

Crippled software? Boo. Triple boo, skip the second boo.

So here’s how to install the Real Thing:

  • Download the right one for your linux distro from the downloads page.
  • Install that, but instead of using apt-get package manager, use dpkg as shown here.
  • Now you should have the full version of wkhtmltopdf, which gives you features like headers and footers, and you can install pdfkit for your programming environment as usual. (Python: “pip install pdfkit”).

Now, you just have to format things using HTML and CSS, but here’s a key tip to make the dev process much faster:

  • Use “media=screen” and “media=print” to define two different CSS source files. I like this because that way you get a browser-friendly and PDF-friendly version in one go.
  • Debug the print version in your browser by using the emulation mode and selecting the print media. For Chrome, instructions are here.
  • In your code, be sure to tell wkhtmltopdf that the print media should be used to render, with the ‘print-media-type’ option. In Python, that means having a key with the name ‘print-media-type’ (an empty string for the value is fine) in the options dictionary.

This is enough to get started, but if you are going to be making a whole bunch of them based on any set patterns, then you’ll need a way to get them generated quickly. That’s where a web app comes in handy. I like pyramid best.

The idea is that you define a GET request with your web app so that it spits out different HTML depending on whatever data. This is the basic process:

  • Have your database contain the data you need, either a fancy SQL one or maybe just a csv text file. Whatever.
  • Make the GET request contain some key or other information that looks up the unique data in the above database, and then
  • Send that data to your template system of choice.
  • The template contains all your HTML/CSS and provides the additional ability to produce certain tags depending on the content. I use Chameleon just because it makes the most sense to my brain.

Okay, so conceptually, we know we can build the data needed to make the PDF, but then how to actually tie in that stuff with wkhtmltopdf? I did it this way:

  • The GET request doesn’t just spit stuff back to the browser, it actually saves it to disk first, and returns that saved file as the response.
  • Write a simple program that loops through all the possible unique urls that will generate the necessary PDFs.
  • Collect your PDFs wherever you saved those files.

In the end, I ended up being able to format, and re-format to no end, information as complex as student data and their reports, complete with nice-looking tables that explained their grades, school information, per-page headers and footers, the works.

The Finder is still broken, people.

Right. So over the Christmas break, some Mac OS X geeks were obviously in a reflective mood because developer blogs exploded with the meme that Apple software just ain’t what it used to be. It all started here, and the best-of-breed collection o’ links is here.

Gist? Apple software may be suffering from a bit of a lack of build and quality assurance, but it depends (on who you ask).

What did everyone miss? The Finder. How can people just ignore that sucky Finder? If you are wondering what the Finder is, look at your dock, the smiling blue thing on the left, or at the top. That’s the super sucky ugly Finder. What it does is let you navigate and get at your files to open, copy, and other stuff. What it also does is suck:

Way too many click involved to do common operations. We have internet- and cloud-based files coming at us left right and center, but the feature of sorting by “Date Added” is hiding behind a preference setting! Imagine your Downloads folder sorted by date added. Think about it. It should do that by default, people.

The other thing is that user interface idioms, like the Trash Can, have been around for a very long time, proving that improvements just aren’t a part of the deal. Features like LaunchPad, Mission Control, and the Dashboard* are just grafts on top as an afterthought. I could go on, but for me, the death knell that not enough iterative development has been done on the Finder is that the Save dialog box still acts like the Finder’s bastard son. Says the dutiful user: “I would like to please reorganize my folder structure so I can save this in the right place.” Save dialog box replies: “No, thanks, but my horrible Dad can do that. Please forget what you were doing and go there instead!”

The gist is that we rightfully expect more from Apple software, and the fact of the matter is that it is Apple’s reason for existence: Take something that could obviously be way better and design the hell out of it. I’m kinda shocked that the whole FTFF thang has dropped out of the conversation.

 

* If you don’t know what those are, then my point is proved.

My Grandma, RIP

Twirl… THWAP went the towel, inspired by boy locker rooms. But there weren’t any showers or sweat, just the wide-gapped jaws of gleeful teenagers getting the Grandma treatment. She was at it again, my strong Grandma, probably saying “No junk!” or somesuch epithet that she conjured up that very moment; and me, marvelling at the spectacle. She passed this week, nearly ten years after one of her good days, our wedding day. She was an inspiration at interactions, people just wanted to talk to her everywhere she went; that the universe contains irony demonstrated by her ten plus year battle with the horrific Alzheimer’s. Memories? Plenty. I was in high school and some guy whose name escapes me but whose words remain implanted as an oral souvenir I file under “This is a day that I grew up” remarked that “Grandma” was the most popular kid at school. Before that day, I knew her as just Grandma, and then I realized she had become everyone’s.

Towards a local, testable, debuggable GAS scripting environment

Okay, so since Friday afternoon I’ve been playing with the very very interesting Google Apps Scripting (GAS), just because it was sitting there and I didn’t understand any of it, and I definitely saw its potential.

Then I went to a GAFE summit conference (I’m an ESL teacher by day) and saw what cool stuff cool people were doing, and I figured it was worth a bit of digging around. So I made a script. It was a pretty simple one, it took info out from an API and added rows in a sheet.

It took me three days to write. I blamed the development environment. I asked the author of the insanely great and probably insanely-difficult-to-maintain Docopus, if there was any legitimate options out there that allowed unit tests and proper debugging.

So I set out to figure out A Better Way. I hated that I had to click on a play button and not see immediate logs from my running program. I’m used to writing python code and seeing results immediately, no waiting around. Maybe it was because I am an amateur JS coder. Whatever. I hated it.

First of all, GAS scripting at the moment, as it is now, from within the browser feels a tick more mature than GUI scripting with AppleScript. Have you ever tried GUI Scripting with AppleScript? Just don’t. It’s a horrible environment, filled with traps and pitfalls. There’s like three guys in the world that do it all the time, and they know everything, and they dominate the help-focused mailing lists, okay so I’m exaggerating, but not all that much.

Okay so there’s no “second of all.” Anyway, so I sought out to figure out a way where I could code up things in JavaScript on my local machine, not on the browser, and when I was done could copy and paste the script. I’ll leave the problem of git-like push/pull for a later date.

Here it is:

You can install node.js and use its require() function in which you define dummy objects that have the same name as those that you use in the GAS API, such as Logger, and define methods that do the terminal equivalent. So that Logger object’s log method would just call console.log. So a Spreadsheets object whose getActiveSpreadsheet method returns a Sheets object which is just a fancy container for an array. That Sheets object’s appendRow() function just pushes to the contained array structure. Simple as.

This will also allow unit testing and other nice things, because making dummy data that just tests integrity of the logic and the assumptions relatively painless. And since you are on the local machine, typing is fast :) Also, since you’re using node.js, you have full access to a completely functional command-driven debugger.

Fascinating, is that in “pure” javascript there’s no way to include files, because that’s usually done through the html layer. And if javascript was ever going to grow up and be a Modern Programming Language it would have to solve that hiccup. Nice to see that node.js has.

Now. Not sure how this will scale for scripts that use all sorts of GAS API objects. I reckon it wouldn’t be too much fun to have to make new ones for each project, but this has to be better than waiting around for a Google Doc to open just so I can click on Script Editor just so I can choose the play button and wait 10 seconds to launch, and wait for it to finish before I see any log items.

Also. It’s not strictly copy-n-paste because you still need some variable assignment boilerplate, but the define function removes the vast majority of the magical “on-the-local-machine-only” code away from that blasted browser.

Best pieces of software out there.

ScreenFlow 5 is out in the Mac App Store, on sale cheap. If you ever do a screencast, this is like the iTunes for screencasts. Yes please.

I need to stop making my kids sit

Oh man. There is a lot of truth in this:

Think back to a multi-day conference or long PD day you had and remember that feeling by the end of the day – that need to just disconnect, break free, go for a run, chat with a friend, or surf the web and catch up on emails. That is how students often feel in our classes, not because we are boring per se but because they have been sitting and listening most of the day already. They have had enough.

Two years ago I went on a conference that was heavy on information overload and so instead on the last day I “had enough”, and instead spent the day in a coffee shop where I met other teachers who were using some online tools that I knew something about and we shared. I remember the comments, and even took some of those and developed stuff based on what I saw. They were English teachers using Moodle, and it was the best thing that happened to me during the whole conference, and it happened when I literally walked away from it.

Sitting around all day taking in information is hard. I often complain to my wife about it when it happens, so I always think that I’m probably a bit more sensitive than others are, but I think we can all relate to having a PD day where it didn’t actually felt like you did anything, and when it’s over you are so completely wiped out from it all anyway.

Hopefully my use of humor in the classroom breaks up things quite a bit. But still, this post has opened my eyes a  bit. That’s it. I’m going to do a lot more drama in my lessons from now on. <grrr></grrr>

Google can’t be everywhere forever

On any given day, at my workplace, I use google services probably billions upon billions of times. Non-existing numbers like that is nice to have, though … isn’t it? … because what is really meant is that I use Google for very nearly every click of the mouse, a hit of the return, and I quickly lose count, and that’s the utility of using silly numbers (c.f. “zillions“).

Anyway. So I use Google services quite a bit. Our internal DNS server just forwards to Google DNS, so then I’m probably using Google Services 20 times per web access at least. In case that stuff right there just went over your head, I’m just pointing out that every time I visit a website (=1 web access) there are 20 additional “websites” that are accessed after that, because any one website has dependences on other services.

Which brings me (finally) to the article I just read, “Peak Google“, which points out that Google may well be like IBM back in its heyday, being really profitable and really scary, but in fact with its business model it actually probably has peaked, and we all know what happens when things peak. It’s a more sophisticated argument than that, so be sure to take a close read yourself.

It’s fascinating to me because to my mind the only truly sustainable software model is that of Open Source. I mean, they recently found a bug that was 25 years old and everyone just sort of laughs it off. Yes I know that’s a pretty unsophisticated summary of what happened with the Shellshock bug, but that vulnerability really is an 11 out of ten, but the world goes on because generally it’s a sustainable model. Imagine if Apple software had a bug like that. Babam.

With Open Source, no one gets fired, no stocks to worry about, just fix it, and get everyone to patch it. Probably it’ll cause adjustments in the community so that such a thing can happen again (alas, but it will!). On the contrary there are a zillion lines of Google code locked behind a company wall and what happens to those if-slash-when Google is just an IBM-of-today rather than the IBM-of-the-eighties?

Weighty stuff.

There are loads of “stupid” people

Early days, and all that, but one of the main threads that is coming out in the aftermath of the flight MH17 is that Putin provided these pro-Russian forces with some pretty advanced weaponry. Take a look at Josh Marshall, for example, whose key paragraph is:

The audio tapes posted by The New York Times might as well be from some future Russia-based version of Waiting for Guffman or Best in Show, a comical rendering of rustics and morons stumbling into an event of vast carnage and international consequence mainly because they’re hotheads and idiots – the kind of people no one in their right minds would give world class weaponry to. It’s like finding some white supremacist/militia types on their little compound in the inter-Mountain west and giving them world class missile launchers and heavy armaments.

Not sure what evidence there is that these guys are morons and/or hotheads, and that no one in their right minds would give weapons to, but America has been giving some pretty stupid people, like, say, Saddam Hussein, and any other pro-Whatever forces some weapons to do some stupid things with.

I’ll leave that train of thought, first because America didn’t directly give Hussein actual weapons, but instead arranged to make sure he had some, and secondly because the bigger train of thought is this:

All this weaponry that is laying around, eventually will be put into the hand of a “moron” or just plain troublemaker, who could do some pretty stupid things with it, including the initial act of a war that results in the death of our very planet.

Apple’s iWatch: My $0.02 Guess

So people are thinking that there will be an iWatch out from Apple come this October. I haven’t seen a picture of anything that I think:

  • I would wear, or
  • That someone else would wear, either (for more than a week)

If Apple is doing an iWatch, given their design process and philosophy, this is what it will be:

  • It will look more like clothes than a gadget.
  • Bracelet, or necklace-like. In other words, not a watch.
  • Water proof to x feet. Think swimming and showering with the thing.
  • No camera at all. Not in version 1.
  • Connects to your other devices, an extension of your other devices.
  • Minimal user interface, if any at all.

I don’t for a second think that Apple is going to release a product that will have an X or Y feature that everyone will want. Apple designs from the ground up, in other words, first and foremost it will be something smart that is worth wearing once or twice, and then after that there will be some killer functions that people will keep wearing it for.

“Intelligent life has just gotten started”

Mind, blown.

Turns out that there are real reason to cast doubt on the idea that the universe is probably teeming with intelligent life. Sure, maybe it has loads and loads of microbes, but human-like species? We may well be the first, and almost certainly the only one in our galaxy.

The argument comes from the observation that there just aren’t any self-replicating robots anywhere to be found in space.

Mind, blown.