modesty / pdf2json

converts binary PDF to JSON and text, for server-side PDF processing and command-line use.

Home Page:https://github.com/modesty/pdf2json

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Word wrapping

tengotengo opened this issue · comments

{
"x": 7.9914062500000025,
"y": 3.984375000000001,
"w": 1292.917,
"clr": 0,
"A": "left",
"R": [
{
"T": "SECTION%3A%20CON",
"S": -1,
"TS": [3, 182, 0, 0]
}
]
},
{
"x": 19.10241171875,
"y": 3.984375000000001,
"w": 984.6790000000001,
"clr": 0,
"A": "left",
"R": [
{
"T": "TACT%20LENS",
"S": -1,
"TS": [3, 182, 0, 0]
}
]
},

This was the "SECTION: CONTACT LENS" string in reference PDF.

This problem is a direct reflection of PDF text data stream structure: the string is in 2 text blocks in PDF, although it "appears" as one visually. Currently, I don't have enough time to work on an "auto word merge" or "auto word concatenation" during parsing, if anybody wants to take it on, a pull request is welcomed.

Looking at the JSON above, shouldn't attribute w refer to the width of text and if so, x + w for the first text be less or equal to x of the second? If so, they appear to be in different units.

Short answer is no. Because there might be kernings or spacings between them. But you are right about the unit, w should be converted by lib/pdfunit. Because the w value is not always accurate so it's not used in client renderer, I may forget to process it. I'll take a look at it when I get some time.

fixed in v1.0.7

I'm still experiencing the unit issue on v1.1.7.
In my case the original text says:

"4)narzędzia i urządzenia, których używanie może zagrażać życiu lub zdrowiu:"

while the JSON output is:

{
  "x": 4.183,
  "y": 4.811,
  "w": 21.323999999999998,
  "sw": 0.32553125,
  "clr": 0,
  "A": "left",
  "R": [
     {
       "T": "4)na",
       "S": -1,
       "TS": [
         0,
         15,
         0,
         0
       ]
     }
   ]
 },
{
  "x": 6.486,
  "y": 4.811,
  "w": 354.54,
  "sw": 0.32553125,
  "clr": 0,
  "A": "left",
  "R": [
    {
      "T": "rz%C4%99dzia%20i%20urz%C4%85dzenia%2C%20kt%C3%B3rych%20u%C5%BCywanie%20mo%C5%BCe%20zagra%C5%BCa%C4%87%20%C5%BCyciu%20lub%20zdrowiu%3A",
      "S": -1,
      "TS": [
        0,
        15,
        0,
        0
      ]
    }
  ]
}

It doesn't seem to add up.
The text from the first object is a part of the first word from the second object (don't mind the "4)" - let's just treat it as one word), so x + w from the first object should be somewhere between x and x + sw from the second object, right?

Am I doing something wrong?