Hacking an e-Reader with React

Rate this content

I wanted to have a tea menu to show guests and for my own reference. Turns out e-Readers use so little power and can render HTML! I'll share how to generate an e-book with all your drinks with React using Deno, as well as rendering a custom cover page with SVG. Wow your friends by turning an old device into a smart home tea menu, and learn how to write your next book with React.

7 min
23 Oct, 2023

Video Summary and Transcription

React for eBooks? Learn how to hack an eReader to display a tea menu. Create images and write e-books using React. Use EPUB format to create chapters and include CSS. Use Pandoc and Dino to simplify the process and make quick updates.

Available in Español

1. Introduction to React for eBooks

Short description:

React for eBooks? Learn how to hack an eReader to display a tea menu. My name is Tiger Oaks, a software engineer at Microsoft. I'll show you how to create an image and e-book with React, and load them onto the e-reader.

React for websites is cool and all, but how about we use it for eBooks? I'm here to show you how to hack an eReader to make it show whatever you want, such as a tea menu. My name is Tiger Oaks. I'm a software engineer working at Microsoft. I previously worked on three different web browsers. You can find me at Mountainwoods on all the socials, as in, not a certain golfer.

Besides for my cool name, if there's one thing to know about me, it's that I like tea quite a lot. I've bought tea from different shops around the world, and I've gotten lots of tea as gifts from friends, and I just went to a tea festival last month. Basically, I now have about 30 different tea flavors in my apartment. But when I have guests over, I just have too many teas to list. By the time I get about halfway through, their eyes have already glazed over. The solution? Putting my collection onto a menu. Now, I can just hand guest a list that they can peruse as they like. Rather than reprinting a piece of paper whenever my collection changes, I can just update an e-reader. Now, I'll show you how you can do this yourself, creating an image and e-book with React, and then loading them onto the e-reader.

2. Creating Images and Writing E-books

Short description:

Let's start by creating an image for the menu using a custom background and SVG for the foreground text. We can move the text elements around the image by setting different x and y values. The SVG file can be converted to a png image for an e-reader. By writing our own e-book in EPUB format, we can use the menu image as the cover of the book. E-books are similar to websites, with each HTML file representing a page.

Let's start by breaking down how to make an image to later display. First, we start with a background to make the menu look a little nicer. I took inspiration from a Dungeons and Dragons-themed tea shop and created a custom background on the paper texture.

Then, I moved on to creating the foreground text using SVG. We frequently use SVG to create vector art on our websites, and it resembles HTML and lets us use React and CSS. For a single tea flavor in the menu, I created text elements for the tea name and description, as well as a line to separate them. I then wrapped it in an SVG group, like a div.

I wrote a small program in TypeScript to read the database with my tea, and then write SVG files using React. This SVG snippet has been turned into a React component, so you can translate a data object into a rendered image using text elements and SVG positioning. Then I can move each of these groups using transform-translate, like a CSS transformation or using position-absolute. By setting different x and y values, I can reuse the same text and line elements, but move them around the image.

With the top section nicely laid out, I can then wrap all these elements in a group and repeat the same process, running the component with a different translation for the bottom section. SVG is very flexible, so you can make your own customizations. You could switch out the font to CSS or display different number of items and show a menu for other things in your house. It's also easy to swap the background image around to anything you want. For an e-reader, I convert the SVG file into a png image, which you can do using free tools like Inkscape and Squoosh. Some e-readers let you just upload a custom lockscreen image, I can connect it to my computer, then drag this image onto its hard drive in a screensaver folder. But other e-readers do not have this feature. However, most of them will show the cover of whatever book you're reading when locked. We can abuse this by writing our own e-book, using the menu image as the cover of the book.

Now, when I say e-book, you probably think of a digital version of a big book like this with lots of characters. But our e-book is going to be more like a pamphlet. If you have at least one page, you can write an e-book. E-books are written in a format called EPUB. This is a standardized format used by all e-readers except Kindles, but Amazon lets you easily convert from EPUB into Kindle's custom format. EPUBs are actually just zip files that have their extension renamed. Just like websites, e-books need to be responsive since you can have different e-reader screen sizes. As a result, writing an e-book is very similar to writing a website. You use familiar HTML, CSS, and images to create each chapter of the book. In a website, each HTML file is a page on your site.

3. Creating EPUB Files and Using Dino

Short description:

In an e-book, each HTML file is a chapter, and EPUB uses a stricter version of HTML called XHTML. You can include CSS and use the same tags. EPUBs need a content.opf file that acts like a sitemap, and there are EPUB generators to simplify writing ebooks. I used Pandoc to convert a markdown file into an EPUB file, and Dino to run JavaScript with built-in features and a huge standard library. Creating a menu like this is a fun learning process and allows for quick updates.

But in an e-book, each HTML file is a chapter of your book. EPUB uses a stricter version of HTML called XHTML. You need to make sure that your self-closing tags have a slash and that your tag names are all lowercase. This might sound familiar, because these are the exact same restrictions that JSX has. But other than that, everything is the same.

You can include CSS and use the same tags like h1, and p, and image. EPUBs also need a file called content.opf, which acts like a sitemap. This file includes data like the title of the book, every file in a zip folder, the book chapters in order, and bookmarks to some important pages. Here's an example. The content file is written in XML. Let's hide the unimportant parts. You can see it broken down into a few tags, similar to how an HTML file is broken down into head and body tags. Metadata includes the title of the book and the cover image. Manifest includes all the files in the book. Some files have been assigned an ID that can be referenced in upper sections. Spying indicates the order of the chapters. And guide points to important pages like the table of contents.

You probably don't want to deal with maintaining a file like this, but don't worry. Just like HTML has static site generators to simplify writing a website, there are EPUB generators to simplify writing ebooks. I used Pandoc, a program that can convert a markdown file into an EPUB file. The front matter, at the top of the markdown file, is used to populate the EPUB metadata. Each top level heading in a markdown also gets converted into an EPUB chapter. So this makes it easy to include images and descriptions of each t-flare as EPUB chapters. Then I ran it all with Dino, which is a really useful tool to run JavaScript on your desktop, like node, but with many more built-in features. It has built-in support for TypeScript and JSX, so I didn't need a build step. It also included the built-in lenture formatter so I didn't need ESLint or Prettier. And it has a huge standard library, so you don't even need that many npm modules. I switched over to using Dino when writing monast scripts, because it's just so much easier to quickly build something with it.

With that, we have all the building blocks you need to create a menu like this yourself. You can also find all the code I wrote on GitHub. You might ask, why go through all this trouble when you could say, use a pen and paper? Well, it's more fun. This is also a great learning process for me to better understand how e-readers work, and whenever we change our tees out, it's much faster to just re-run the script. Thank you all for listening, and feel free to get in touch with me if you have any questions.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Let’s face it: technical debt is inevitable and rewriting your code every 6 months is not an option. Refactoring is a complex topic that doesn't have a one-size-fits-all solution. Frontend applications are particularly sensitive because of frequent requirements and user flows changes. New abstractions, updated patterns and cleaning up those old functions - it all sounds great on paper, but it often fails in practice: todos accumulate, tickets end up rotting in the backlog and legacy code crops up in every corner of your codebase. So a process of continuous refactoring is the only weapon you have against tech debt.In the past three years, I’ve been exploring different strategies and processes for refactoring code. In this talk I will describe the key components of a framework for tackling refactoring and I will share some of the learnings accumulated along the way. Hopefully, this will help you in your quest of improving the code quality of your codebases.

React Summit 2023React Summit 2023
24 min
Debugging JS
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.
React Advanced Conference 2022React Advanced Conference 2022
22 min
Monolith to Micro-Frontends
Top Content
Many companies worldwide are considering adopting Micro-Frontends to improve business agility and scale, however, there are many unknowns when it comes to what the migration path looks like in practice. In this talk, I will discuss the steps required to successfully migrate a monolithic React Application into a more modular decoupled frontend architecture.
React Advanced Conference 2023React Advanced Conference 2023
22 min
Power Fixing React Performance Woes
Next.js and other wrapping React frameworks provide great power in building larger applications. But with great power comes great performance responsibility - and if you don’t pay attention, it’s easy to add multiple seconds of loading penalty on all of your pages. Eek! Let’s walk through a case study of how a few hours of performance debugging improved both load and parse times for the Centered app by several hundred percent each. We’ll learn not just why those performance problems happen, but how to diagnose and fix them. Hooray, performance! ⚡️

Workshops on related topic

React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.
JSNation 2022JSNation 2022
41 min
Build a chat room with Appwrite and React
API's/Backends are difficult and we need websockets. You will be using VS Code as your editor, Parcel.js, Chakra-ui, React, React Icons, and Appwrite. By the end of this workshop, you will have the knowledge to build a real-time app using Appwrite and zero API development. Follow along and you'll have an awesome chat app to show off!
GraphQL Galaxy 2021GraphQL Galaxy 2021
164 min
Hard GraphQL Problems at Shopify
At Shopify scale, we solve some pretty hard problems. In this workshop, five different speakers will outline some of the challenges we’ve faced, and how we’ve overcome them.

Table of contents:
1 - The infamous "N+1" problem: Jonathan Baker - Let's talk about what it is, why it is a problem, and how Shopify handles it at scale across several GraphQL APIs.
2 - Contextualizing GraphQL APIs: Alex Ackerman - How and why we decided to use directives. I’ll share what directives are, which directives are available out of the box, and how to create custom directives.
3 - Faster GraphQL queries for mobile clients: Theo Ben Hassen - As your mobile app grows, so will your GraphQL queries. In this talk, I will go over diverse strategies to make your queries faster and more effective.
4 - Building tomorrow’s product today: Greg MacWilliam - How Shopify adopts future features in today’s code.
5 - Managing large APIs effectively: Rebecca Friedman - We have thousands of developers at Shopify. Let’s take a look at how we’re ensuring the quality and consistency of our GraphQL APIs with so many contributors.
JSNation 2023JSNation 2023
57 min
0 To Auth In An Hour For Your JavaScript App
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.js backend + Vanilla JS frontend) to authenticate users with One Time Passwords (email) and OAuth, including:
- User authentication – Managing user interactions, returning session / refresh JWTs- Session management and validation – Storing the session securely for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.
JSNation 2023JSNation 2023
87 min
Build a Collaborative Notion-Like Product in 2H
You have been tasked with creating a collaborative text editing feature within your company’s product. Something along the lines of Notion or Google Docs.
CK 5 is a feature-rich framework and ecosystem of ready-to-use features targeting a wide range of use cases. It offers a cloud infrastructure to support the real-time collaboration system needs. During this workshop, you will learn how to set up and integrate CK 5. We will go over the very basics of embedding the editor on a page, through configuration, to enabling real-time collaboration features. Key learnings: How to embed, set up, and configure CK 5 to best fit a document editing system supporting real-time collaboration.
Table of contents:- Introduction to the CK 5 ecosystem.- Introduction to a “Notion-like” project template.- Embedding CK 5 on a page.- Basic CK 5 configuration.- Tuning up CK 5 for a specific use case.- Enabling real-time editing features.