The Thermal Printer Project: Printing hcards

Published on under the Thermal Printer category.

My hcard printed on thermal printer

I have been playing around a lot with my thermal printer. While I have accomplished the initial goals I had in mind when I purchased the printer (create a program that prints my webmentions and print myself a daily update), I know there is still more that I can do with this device. Seeing how creative the Little Printer community was, I have been thinking up new modules to add to my thermal printer.

I started to think about what it would be like to print out hcards, which are a way to mark up people and organisations in HTML. hcards are useful because they let programs parse information someone has posted on their website. Because hcards are standardized, I could use them to print information about those who have implemented the standard on their site. I don’t know about broader adoption of hcards but I know a lot of my internet friends use hcards on their website so I thought it would be nice to experiment with printing them.

Planning the project

I started this project by using the mf2py microformats parser and trying to retrieve some basic information from a hcard. This parser does all of the heavy lifting and gives me the data from the hcards that I will then print later. Using only a few lines of code, I was able to retrieve all of the microformats on a web page.

The initial idea was to print out the name, photo, URL, and note from a hcard. These are all valuable pieces of information on hcards, especially if I were to use this project to print cards about my blog that I could then give to other people (or I could send my code to anyone else gets a thermal printer and wants to print my hcard). I wrote statements to individually collect each property and then I realised needed to validate that each statement existed. Without validation, my program would fail every time it tried to retrieve a hcard attribute that was not present on a page.

Validation and retrieving attributes

To resolve this issue, I wrote a function that prints properties that I specify. This function will only print a property if it exists; otherwise, a message is displayed on the console telling me that the specified property cannot be retrieved. I wrote a special handler for “org” properties so that I could list every org someone was part of on their card:

    def property_to_print(hcard_item, property):
        # Special handling for org properties so I can create a list of properties
        if property == "org" and hcard_item["properties"].get("org"):
            org_list = []
            for item in hcard_item["properties"][property]:
                if item["properties"].get("name"):
                    org_list.append(item["properties"]["name"][0])
                elif item[0]:
                    org_list.append(item[0])
            org_list = ", ".join(org_list)
            printer.println(main.textWrapped(org_list))

        elif hcard_item["properties"].get(property):
            printer.println(main.textWrapped(hcard_item["properties"][property][0]))
        else:
            print("No {} available.".format(property))

The “if property == “org”“ code is invoked if I try to retrieve the “org” hcard attribute. This can be a hcard or some text and I check for and can handle both in the code above.

The code that prints out the image is separate because I need to save the image locally before I am able to print it. I might be able to download the contents and then use the result of the object from a request.get() request but I have not tried this. For now, I’m happy with the solution I have.

While there are many other microformats I could have supported (i.e. URLs for each organisation someone lists in their org attribute), I decided to support the following:

  • photo
  • nickname
  • job-title
  • org
  • email
  • url
  • locality
  • region
  • country-name
  • tel
  • note

I think these attributes are representative of what one might find on a business card. Choosing these attributes helped me keep my program simple and focused on what mattered: printing out key information from someone’s hcard that I might want to save for future reference. The list above shows the exact order in which the attributes are printed, assuming each one is available. If an attribute is not present on a page, the program skips over the attribute.

Finding the hcard and receiving a print

Toward the end of writing this program, I realised that I needed a way to find the hcard that I wanted to print out. I decided on a simple rule: I would only print the first hcard on a page. This saves on paper and I do not see many websites that have more than one hcard attribute for themselves. Although, there may be edge cases I have not taken into account yet.

After writing all of that code, and with a lot of testing, I finally had a print out of a hcard. Here’s mine:

I also tested my hcard print out on aaronparecki.com, a website which has many more microformats attributes present than I do. My printer returned this card:

Aaron Parecki’s hcard printed on paper

I was content with this hcard. The information was formatted correctly, the print out was not too long and all information was clearly visible. Notably, all information was ordered in a way that I thought was appropriate, with the name going first, org and contact information coming after, and the note coming last.

Wrapping up

I am not sure exactly how my code could be used. If the world adopts hcards more widely, this sort of project could be used to print out reminders of people you have met online. Or you could use this as a way to print your own business cards based on your hcard. This is a use case I find interesting. I could print out 5-10 of these to keep in my wallet because I don’t have a business card. I’m not sure if I’ll do this yet. I built this project for the fun of the challenge and also to explore what thermal printers can do. I have no doubt I’ll end up building more projects with the thermal printer soon.

You can see the script that powers this project on GitHub Gists.

Also posted on IndieNews.

Go Back to the Top