admin管理员组

文章数量:1396787

I have a PDF where I have a question which is in darker text colour and the answer is in lighter text colour. Other than text colour there is no other logic I can use to separate the question & answer texts.

I'm using tabulapdf to extract the content from the PDF, but in PHP application

Appreciate any suggestions or guidance. Thanks!

I have a PDF where I have a question which is in darker text colour and the answer is in lighter text colour. Other than text colour there is no other logic I can use to separate the question & answer texts.

I'm using tabulapdf to extract the content from the PDF, but in PHP application

Appreciate any suggestions or guidance. Thanks!

Share Improve this question asked Mar 26 at 17:37 Saumini NavaratnamSaumini Navaratnam 8,8905 gold badges46 silver badges74 bronze badges 4
  • tabulapdf appears to be a Java app, right? I can't find a PHP specific version of it, just a wrapper around the Java version. If I'm wrong, please provide a link. If this is the Java version, PHP is just passing arguments to the jar, but you'd need to determine what those outside of PHP first. – Chris Haas Commented Mar 26 at 18:14
  • Yeah correct its a Java package. Using the jar package & executing the command in PHP. For example - java -jar -Dfile.encoding=utf-8 ../java/tabula-1.0.5-jar-with-dependencies.jar --no-spreadsheet -f TSV -a 120.63,224.98,145.26,384.23 -p 1 "/private/tmp/sample.pdf" – Saumini Navaratnam Commented Mar 26 at 18:17
  • Tabula doesn't have an option to extract colour. Doesn't need to be Tabula, any other packages I could use will be helpful too. – Saumini Navaratnam Commented Mar 26 at 18:19
  • Unfortunately, asking for recommendations for other tools would be out of scope for this site. That said, in another life I used to use iText and iTextSharp, both open source, and there is/was the ability to do it there: stackoverflow/a/9161899/231316 – Chris Haas Commented Mar 26 at 18:56
Add a comment  | 

1 Answer 1

Reset to default 1

Tabula like many PDF text extractions will not see the PDF text with its surface colours but simply the core monochromatic plain text.

The main reason is that PDF colouring can be applied many ways unlike say HTML where the style is cascading so may be defined purely for text in an area/object via tags in a .css file or a header <style> or inline <p style="color: rgb(255,0,0)"> or older pre HTML5 <font color="red">.

The nice thing about HTML is the colour is text related and defined before the text it applies to.

The Problem with PDF is the colour may be after the text and is NOT only text related. So more like colour all objects in a division.

What about the shown text? Well it is actually, in this one case, fairly simple to see the colours. HOWEVER the page objects are first set to red = 1 0 0 rg before any Begin Text starts thus is the default color for the first line. Then black = 0 0 0 rg for the next line. Green = 0 1 0 and then blue binary text is different type of colour 0 0 1 scn. Thus we are lucky for this simple plain text file. We look for a colour then can extract the text after it.

stream
q /GS0 gs 1 0 0 1 222 775.68008 cm 1 0 0 rg
BT
.001 Tc /F0 12.0005 Tf <0037004c0057004f0048> Tj
0 0 0 rg 1 0 0 1 36.0072 0 Tm <000300200003> Tj
0 1 0 rg 1 0 0 1 57.611497 0 Tm <002b0048004f004f0052> Tj
0 0 0 rg 1 0 0 1 93.6187 0 Tm <000f0003> Tj
0 0 1 scn 1 0 0 1 108.0216 0 Tm <003a00520055004f0047> Tj
0 0 0 scn 0 Tc 1 0 0 1 144.0288 0 Tm <0004> Tj
ET

The simplest way to see what text is what colour is convert PDF into HTML which is easier to reverse so as to trace the colour differences.

There are many utilities to convert PDF to HTML in one single command.

Poppler does not work in this case:

But MuTool does (PyMuPDF would too). >mutool convert -o colours.html colours.pdf

<p style="top:56.6pt;left:222.0pt;line-height:12.0pt"><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#ff0000">Title</span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000"> = </span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#00ff00">Hello</span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000">, </span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#0000ff">World</span></tt><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000">!</span></tt></p>
<p style="top:69.5pt;left:70.9pt;line-height:12.0pt"><tt><span style="font-family:F1,monospace;font-size:12.0pt;color:#000000"># Tabula</span></tt></p>

Yet again, no method is perfect as you need to note the source was Courier (monospaced) Fonts: CIDFont+F1 (TrueType (CID); Identity-H; embedded) but the Browser HTML default.css style is SansSerif (for font-family:F1,monospace;).

Others may fail to see the colours correctly. So remember I said the default line colour was Red! I wont mention names but here is another apps attempt!

本文标签: phpExtract text colour from PDFStack Overflow