Why does copying text from this PDF give an N-1 Caesarean shift?

41 Views Asked by At

This is my first post here, but I was absolutely bewildered, and I need to know why it happens:

I was experimenting with pypdf, hoping to use it for a larger text analysis project, and in lieu of any relevant PDFs, I chose a few I had sitting on my desktop.

With most of them, I was able to extract the expected text. But then I remembered one particular PDF I'd had a problem copying and pasting from (Googling homework answers), so I decided to extract from that:

from pypdf import PdfReader

reader = PdfReader("HW-W6.pdf")
page = reader.pages[0]
print(page.extract_text())

Which printed out:

HWWe16mkWrmoe5..Ptoonrdsgdl×mlVsqhw,gVrsgdenql,8,0,1.vgdqd,0hrVmnmrhmftiVqlVsqhwnechldmrhnmm×mVmc,1hrVmVqahsqVqxlVsqhwnechldmrhnmlm(×m-LqnudsgVs},}1≤},00}1.5.1Fds,adVml×mlVsqhw-TrdsgdrsdoradinvsnrgnvsgVsVudbsnqu∈irVshrdr,u8+heVmcnmixhe,,u8+-SghrvhiirgnvsgVsAnbb,(8Anbb,,(-V(PgnvsgVshe,u8+)sgdm,,u8+:a(Ptoonrd,,u8+-PgnvsgVs,u8+-5.2PgnvsgdUVmcdqlnmcdlVsqhwhmfi..-1(hrmnmrhmftiVqVrinmfVrsgdonhmsr{uT{Vqdchr,shmbs-5.3;hmcVidVrsrnitshnmenqsgdeniinvhmfoqnaidlr4V(,805010006.N80105a(,8022030061.N83235.4;hmcsgddptVshnmx8+)0unesgdidVrs,rptVqdrihmdsgVsadrssrsgdcVsVonhmsr1.0()3.1()6.2(Vmc7.2(-

At first, it looked like complete gibberish, and I thought that must have been some kind of obfuscation (though I'm not entirely familiar with that, anyway). But, focus on the string, Ptoonrd. Compare that to the expected text, "Suppose."

I noticed this was way too "regular" to be complete obfuscation; in fact, it looked like a letter-shift, so I checked, and by trial and error, got an N-1 Caesarean Shift.

It's obviously not a perfect shift, and that can be seen more clearly with other copy-pastes.

For example.

My only clue is that this document might have been compiled using LaTeX, but I don't know enough to investigate any further. Even as much as opening the document on a browser causes it to omit certain characters.

Does anybody know why this occurs?

The Offending PDF.

1

There are 1 best solutions below

0
David van Driessche On

This is not anything deliberate as the shift you're talking about. Whatever writes the text has to decide how to encode it in the PDF file. Typically only those characters used in the PDF are put in the PDF file, so you end up with a partial font (the technical term is that the font is subsetted).

I could imagine that this process leaves you with a text representation that looks like there is a deliberate attempt at obfuscation or encryption. Still, in all likelihood this is just by coincidence.

In general, the text (character codes) you find in a page description in PDF have nothing to do with actual character codes. They are through various mechanisms mapped to what is drawn for them (the glyphs used).

PDF can use a mechanism that is either an encoding or a ToUnicode table that then also maps these character codes found in the page description to Unicode characters from which meaning can be derived. But this is optional and only required when the PDF claims to be compliant with certain standards (such as ISO PDF/A (certain flavors only) or ISO PDF/UA).