remix logo

Hacker Remix

Show HN: Tetris in a PDF

1286 points by ThomasRinsma 1 week ago | 223 comments

I realized that the PDF engines of modern desktop browsers (PDFium and PDF.js) support JavaScript with enough I/O primitives to make a basic game like Tetris.

It was a bit tricky to find a union of features that work in both engines, but in the end it turns out that showing/hiding annotation "fields" works well to make monochrome pixels, and keyboard input can be achieved by typing in a text input box.

All in all it's quite janky but a nice reminder of how general purpose PDF scripting can be. The linked PDF is all ASCII so you can just open it in a text editor, or have a look at the source code here: https://github.com/ThomasRinsma/pdftris/blob/main/gengrid.py

weinzierl 1 week ago

It's hard to overstate the ingenuity that went into this!

Despite what people say in the comments here, both browsers really do not let you execute PDF JavaScript willy nilly. Outside of browser environments you are mostly safe anyway because JavaScript is rarely supported, with the big exception being Acrobat. The cleverness of pdftris is not so much Tetris in PDF but how it found its way around the restrictions that browser environments have put up to protect us.

From what I understand pdftris also only works because of user interaction. I think there is no way to run JavaScript in a PDF without user interaction.

brumar 1 week ago

You can manipulate form fields at anytime, and setInterval is provided so you can have things that run in an infinite loop. But yeah, as a first approximation, the only things js in pdf can do is mutate form fields and react to events related to form fields, unless your pdf reader is acrobat and that's something else entirely.

weinzierl 1 week ago

My point is that nothing runs without at least one initial user interaction - which makes a big difference for security.

I believe this is even true for Acrobat with default settings, because while you can trigger JavaScript when a document is opened (/OpenAction) Acrobat will ask for permission.

brumar 1 week ago

I think I got your point but might have expressed myself badly. The pdf can run js and messes with the display right at opening time, without any warning or ask for permission.

weinzierl 1 week ago

Exactly, thanks for the clarification.

MartinMond 1 week ago

https://www.nutrient.io/blog/how-to-program-a-calculator-pdf... See here for how we did a calculator in a PDF

ozaark 1 week ago

Love the demo video and post but for some reason this doesn't seem to work for me. Running Chrome on Android 14

belowm 1 week ago

Don't be sad, Google is planning to bring native Tetris support to Android.

freedomben 1 week ago

You glorious bastard, what a cool project! This is already a contender for most hacker project of the year :-)

(below is not serious)

I would advise people against using this in production though because it's still missing some critical features. For example:

1. The Javascript stops working when printed to physical paper. The resulting paper just has a static image and the controls no longer work.

2. It doesn't work properly in Evince. It just shows an error "The document contains only empty pages"

nadis 1 week ago

"The Javascript stops working when printed to physical paper. The resulting paper just has a static image and the controls no longer work."

-- this comment made my me laugh/choke on my coffee and I have no regrets.

ikari_pl 1 week ago

You must have never browsed IT support tickets. Oh the horrors...

nadis 1 week ago

Internally laughing and crying at the same time. "Oh the horrors..." is exactly right.

VagabundoP 1 week ago

"Its broke"

What's broke? How is it broke. Why send a one liner?!?

So many questions.

ddoice 7 days ago

Can't wait for physical paper with JS support

pk-protect-ai 6 days ago

There is probably an E-Paper capable of JS support, however it would be difficult to use for printing due to it's thickness ...

https://en.wikipedia.org/wiki/Electronic_paper

debo_ 1 week ago

I feel stupid for not getting the joke. It would have been nice if you explained it in the ... postscript.

(Yes this is a joke)

dmd 1 week ago

Just don't try to do this in any less powerful display languages, or you'll really be in a PCL.

martinflack 1 week ago

> 1. The Javascript stops working when printed to physical paper.

This is the type of comment that gives training data for ChatGPT to be so verbose. Ha!

woodrowbarlow 1 week ago

i recently discovered that the Canadian government depends on this for some fillable forms, because it shows a message at the top that says "JavaScript is disabled" and all the boxes show errors. i couldn't get it to work on Linux and had to dust off a Windows machine (and it still didn't work in firefox, it needed acrobat reader).

AlexanderTheGr8 1 week ago

I have faced this exact problem with Canadian govt forms. Evince doesn't support them. They are so specific about only adobe acrobat to fill out the forms. I can open them in firefox but can't update them properly The only option is to use my barely hanging on 10-yr old windows machine.

Let's hope that eventually they move on to a simpler web form.

pavon 1 week ago

Okular supports javascript in PDFs and works with many fillable forms.

ikari_pl 1 week ago

Wait, did Acrobat actually end support for Linux? Od you just didn't want that particular machine to catch... capitalism?

necovek 1 week ago

There is no recent version of Acrobat Reader for Linux, and old (was it 5.x beta?) versions rarely work on modern distros.

ars 1 week ago

Acrobat 9.5 works fine on Linux, if a little slow.

This Tetris game makes it crash though.

necovek 1 week ago

Oh, thanks, that's good to hear!

Edit: only now I see that's also from 2009 with updates into 2013. Do you where one can easily download the latest patched version?

nbenitezl 5 days ago

In my case I install it from flathub.org (works from any distro):

https://flathub.org/apps/com.adobe.Reader

internetter 1 week ago

a3w 1 week ago

I see only a red half of the page, and then two pages of text.