I've some PDFs which are in Hindi, and have extractable text. I used <a href="https://github.com/pdfminer/pdfminer.six" rel="nofollow">pdfminer.six</a> for python 3.6, to do the extraction. The output looks like:<br /><a href="https://i.stack.imgur.com/cZGKz.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/cZGKz.png" data-original="https://i.stack.imgur.com/cZGKz.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
As one can see, there are a number of characters that are converted into the form "(cid :number)".
On further analysis, I found out that a PDF contains CMAPs which map character codes to glyph indices. So, a CID is a character identity for the glyph it maps to, inside the CMAP table.
But how are these character codes related to Unicode values? Basically, how is a PDF viewer able to show the glyph using this mapping?
Moreover, according to a comment to <a href="https://stackoverflow.com/q/22908556/5345646" rel="nofollow">this</a> similar question, this process may not be legal. But I'm not trying to steal someone's font. I want the text. How does this process become illegal?
Since there are many questions like this one, I want to clarify that I do not aim at solving the "cid" problem. I want to clarify the reasons for the problem and reasons for it's illegality.
<strong>EDIT:</strong> <a href="https://github.com/euske/pdfminer/issues/39" rel="nofollow">This <em>issues</em> page</a> for
pdfminer discusses this issue, where the author clearly says that there seems to be no reliable workaround for this issue. Is there some general, basic limitation (like, no access to font) that makes this issue continual?
But how are these character codes related to Unicode values? Basically, how is a PDF viewer able to show the glyph using this mapping?</blockquote>
The character codes one finds in the PDF content streams do not need to be related to Unicode values in any obvious way. In particular, a PDF viewer does not at all need a Unicode code point for a character code to show the matching glyph.
In a PDF a font has a mapping (or a sequence of mappings) from character code to glyph ID in the font program, and this mapping may be completely arbitrary.
E.g. in case of embedded font subsets the subset font program often is created by giving the first glyph used on a page a starting glyph id <em>n</em>, then giving the second, different glyph on that page id <em>n+1</em>, then the next, different glyph id <em>n+2</em> etc etc, and then the character codes often are identical to the glyph ids, i.e. the mapping above is the identity mapping. If there are no additional information anymore, a text extractor has no chance to properly do its job.<blockquote>
I want to clarify the reasons for the problem</blockquote>
Regular text extraction usually has the following options to find the Unicode value for a character code:<ul><li>
A PDF font <em>may</em> include a <strong>ToUnicode</strong> map (mapping from character code to Unicode) to support operations like searching strings or copy & paste in a PDF viewer. This map immediately provides the mapping the text extractor needs.
Beware, though: these <strong>ToUnicode</strong> maps can be incomplete and sometimes even contain intentionally incorrect mappings!</li> <li>
The PDF font encoding definition may contain the name of a pre-defined standard encoding (e.g. <strong>WinAnsiEncoding</strong> or <strong>GBpc-EUC-H</strong>) or a standardized character name (e.g. <strong>space</strong>, <strong>seven</strong>, or <strong>ntilde</strong>) for a given code. A text extractor merely needs to know the encoding represented by that encoding name or the code represented by that character name.
But the <strong>Encoding</strong> may also be an identity (<strong>Identity–H</strong> and <strong>Identity–V</strong> with <em>character code = glyph code</em>) which doesn't give away anything, and a character name may also be non-standardized (e.g. <strong>g17</strong>).</li> </ul>
The PDF specification says: <em>If these methods fail to produce a Unicode value, there is no way to determine what the character code represents in which case a conforming reader may choose a character code of their choosing.</em>
In case of your text extraction output I would guess the PDF font has an incomplete <strong>ToUnicode</strong> map.
Actually there are some more locations to look for additional information, e.g. the font program might include an own mapping of its glyphs to Unicode, but those additional information also are optional.<blockquote>
... and reasons for it's illegality.</blockquote>
In case of all the above options I don't see any sensible font license being violated, in particular as most of those options didn't even look into the font program (e.g. the *.ttf) itself, merely into the PDF metadata wrapping it.
On the other hand, if e.g. you had the idea to construct <strong>ToUnicode</strong> maps for those fonts missing such a map by drawing each glyph of the font onto a bitmap, nicely separated from anything else, and applying OCR to it, you as the recipient of the PDF suddenly would use the font program to draw something else than the original document, and this might be considered usage not covered by the license.