You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tika.apache.org by Andrew Skiba <an...@tikalk.com> on 2014/06/18 16:16:50 UTC

Patch: self-contained HTML using Data URI

Hi,

In the current code, the images from Word documents are referenced by
"embedded:xxx" links in the generated HTML. This causes the browsers
display "x" icon instead of the image.

The proposed patch encodes the images using Data URI, if there is
-Dtika.parsers.urlimages system property.

http://en.wikipedia.org/wiki/Data_URI_scheme

So the default behavior is the same, but users of the library can
optionally generate self-contained HTML with correct images.

Thank you,

Andrew.

Re: Patch: self-contained HTML using Data URI

Posted by Nick Burch <ap...@gagravarr.org>.
On Thu, 10 Jul 2014, Andrew Skiba wrote:
> Took some time, but I glued it all together, so now it works without 
> modifying Tika sources, only by using custom handler, extractor and 
> parser. It works with WordExtractor, although it is looking as a dirty 
> hack. As I could not override the behavior of WordExtractor, in the 
> handler I ignore elements <img> if the src is "embedded:xxx", and let 
> trough only images with src with data URI.

Hmm, I would've expected that you'd only want to change the embedded: 
ones, since those are the only ones where you'll be getting the image 
through as an embedded part, no?

(That's largely what the Alfresco example I pointed you at does - it 
catches the embedded urls and re-writes them, while storing the image data 
in somewhere it knows about + records so it knows what to re-write the 
embedded one too)

> The problem is – it does not work at all with OOXMLParser, PDFParser, 
> and probably others. I could not find in the code of these parsers 
> recursive handling of the embedded images, similar to the call to 
> handleEmbeddedResource in WordExtractor.handlePictureCharacterRun

Hmm, I would've expected it to work for .docx in the same way as .doc. We 
have some "matching" .doc and .docx test files, are you getting different 
behaviour for them?

Some of the other parsers may need updating to match the pattern from 
.doc, especially if you're the first person to try to work with embedded 
images from them...

Nick

Re: Patch: self-contained HTML using Data URI

Posted by Andrew Skiba <an...@tikalk.com>.
Nobody replied 4 days. I see the context of the message was lost - it is
about  https://issues.apache.org/jira/browse/TIKA-1344

On Thu, Jul 10, 2014 at 6:13 PM, Andrew Skiba <an...@tikalk.com> wrote:

> Hi Nick,
>
> Took some time, but I glued it all together, so now it works without
> modifying Tika sources, only by using custom handler, extractor and parser.
> It works with WordExtractor, although it is looking as a dirty hack. As I
> could not override the behavior of WordExtractor, in the handler I ignore
> elements <img> if the src is "embedded:xxx", and let trough only images
> with src with data URI.
>
> The problem is – it does not work at all with OOXMLParser, PDFParser, and
> probably others. I could not find in the code of these parsers recursive
> handling of the embedded images, similar to the call to
> handleEmbeddedResource in WordExtractor.handlePictureCharacterRun
>
> So my questions are:
>
> 1. Does my handler, parser and extractor do what you meant?
> 2. Did I miss the call to ParsingEmbeddedDocumentExtractor in OOXMLParser?
> I found img generating code in XWPFWordExtractorDecorator, but the code is
> deep in private functions call tree, and XWPFWordExtractorDecorator is
> pretty much hardwired to OOXMLParser via OOXMLExtractorFactory, so I did
> not see an easy way to inject my code.
>
> Thank you very much.
>
> Andrew.
>
>
> On Wed, Jun 25, 2014 at 12:39 PM, Nick Burch <ap...@gagravarr.org> wrote:
>
>> On Wed, 25 Jun 2014, Andrew Skiba wrote:
>>
>>> Let me check I understand you right. WordExtractor will continue to
>>> create
>>> <img src="embedded:filename.jpg"/>
>>>
>>
>> Yes, as will (should..) the other parsers which find embedded resources
>>
>>
>>  and call the ImageParser once for every file name.
>>>
>>
>> No. It'll call your code, as you'll have registered your code as the
>> EmbeddedDocumentExtractor to call for embedded resources like images.
>>
>> (If there isn't one, then a ParsingEmbeddedDocumentExtractor is used,
>> which calls the default parser, which is how it ends up in ImageParser if
>> you're recursing)
>>
>> Nick
>>
>
>

Re: Patch: self-contained HTML using Data URI

Posted by Andrew Skiba <an...@tikalk.com>.
Hi Nick,

Took some time, but I glued it all together, so now it works without
modifying Tika sources, only by using custom handler, extractor and parser.
It works with WordExtractor, although it is looking as a dirty hack. As I
could not override the behavior of WordExtractor, in the handler I ignore
elements <img> if the src is "embedded:xxx", and let trough only images
with src with data URI.

The problem is – it does not work at all with OOXMLParser, PDFParser, and
probably others. I could not find in the code of these parsers recursive
handling of the embedded images, similar to the call to
handleEmbeddedResource in WordExtractor.handlePictureCharacterRun

So my questions are:

1. Does my handler, parser and extractor do what you meant?
2. Did I miss the call to ParsingEmbeddedDocumentExtractor in OOXMLParser?
I found img generating code in XWPFWordExtractorDecorator, but the code is
deep in private functions call tree, and XWPFWordExtractorDecorator is
pretty much hardwired to OOXMLParser via OOXMLExtractorFactory, so I did
not see an easy way to inject my code.

Thank you very much.

Andrew.


On Wed, Jun 25, 2014 at 12:39 PM, Nick Burch <ap...@gagravarr.org> wrote:

> On Wed, 25 Jun 2014, Andrew Skiba wrote:
>
>> Let me check I understand you right. WordExtractor will continue to create
>> <img src="embedded:filename.jpg"/>
>>
>
> Yes, as will (should..) the other parsers which find embedded resources
>
>
>  and call the ImageParser once for every file name.
>>
>
> No. It'll call your code, as you'll have registered your code as the
> EmbeddedDocumentExtractor to call for embedded resources like images.
>
> (If there isn't one, then a ParsingEmbeddedDocumentExtractor is used,
> which calls the default parser, which is how it ends up in ImageParser if
> you're recursing)
>
> Nick
>

Re: Patch: self-contained HTML using Data URI

Posted by Nick Burch <ap...@gagravarr.org>.
On Wed, 25 Jun 2014, Andrew Skiba wrote:
> Let me check I understand you right. WordExtractor will continue to create
> <img src="embedded:filename.jpg"/>

Yes, as will (should..) the other parsers which find embedded resources

> and call the ImageParser once for every file name.

No. It'll call your code, as you'll have registered your code as the 
EmbeddedDocumentExtractor to call for embedded resources like images.

(If there isn't one, then a ParsingEmbeddedDocumentExtractor is used, 
which calls the default parser, which is how it ends up in ImageParser if 
you're recursing)

Nick

Re: Patch: self-contained HTML using Data URI

Posted by Andrew Skiba <an...@tikalk.com>.
Nick, thanks for the reply.

Let me check I understand you right. WordExtractor will continue to create
<img src="embedded:filename.jpg"/>, and call the ImageParser once for every
file name. But the ImageParser will save the image contents somewhere (in
the metadada?) and when XHTMLContentHandler.startElement is called on "img"
by existing WordParser, it will replace the image src with the data URI.

Is that what you meant? If yes - how do I change the configuration to cause
customized content handler and Image Parse to be used instead of the
current ones?

Thank you very much.

Andrew.


On Tue, Jun 24, 2014 at 7:45 PM, Nick Burch <ap...@gagravarr.org> wrote:

> On Tue, 24 Jun 2014, Andrew Skiba wrote:
>
>> I started with org.apache.tika.parser.microsoft.WordExtractor and
>> immediately saw that it already makes a recursive call to the
>> org.apache.tika.parser.image.ImageParser. But ImageParser currently only
>> enriches metadata, and does not create <img> element itself. This is done
>> in the WordExtractor and respective handlers for types other, than MS Word.
>>
>
> A would've thought it would only trigger ImageParser if you set the
> AutoDetectParser on the parse context, did you?
>
> My idea was that you'd have a content handler + recursing parse class /
> pair, the handler would re-write the img tag when it came through, and the
> recursing parser would capture the image when that triggers to get the
> image data suitable for the re-write. (This is largely what the Alfresco
> class does that I suggested you look at)
>
> You shouldn't be changing anything in the Word Parser itself, you want to
> be writing something that applies equally to all parsers.
>
> (It might be that you find that one parser is being non-standard about how
> it reports embedded images, in which case you'll need to fix that to follow
> the others, but ideally you shouldn't be touching the built in parsers
> beyond that)
>
> Nick
>

Re: Patch: self-contained HTML using Data URI

Posted by Nick Burch <ap...@gagravarr.org>.
On Tue, 24 Jun 2014, Andrew Skiba wrote:
> I started with org.apache.tika.parser.microsoft.WordExtractor and 
> immediately saw that it already makes a recursive call to the 
> org.apache.tika.parser.image.ImageParser. But ImageParser currently only 
> enriches metadata, and does not create <img> element itself. This is 
> done in the WordExtractor and respective handlers for types other, than 
> MS Word.

A would've thought it would only trigger ImageParser if you set the 
AutoDetectParser on the parse context, did you?

My idea was that you'd have a content handler + recursing parse class / 
pair, the handler would re-write the img tag when it came through, and the 
recursing parser would capture the image when that triggers to get the 
image data suitable for the re-write. (This is largely what the Alfresco 
class does that I suggested you look at)

You shouldn't be changing anything in the Word Parser itself, you want to 
be writing something that applies equally to all parsers.

(It might be that you find that one parser is being non-standard about how 
it reports embedded images, in which case you'll need to fix that to 
follow the others, but ideally you shouldn't be touching the built in 
parsers beyond that)

Nick

Re: Patch: self-contained HTML using Data URI

Posted by Andrew Skiba <an...@tikalk.com>.
Hello again.

I created an issue https://issues.apache.org/jira/browse/TIKA-1344 for this
patch and got an advise to implement this in a content handler. So I
learned the idea behind RecursiveMetadata and started to look how to move
my change into a handler according to what Nick advised me.

I started with org.apache.tika.parser.microsoft.WordExtractor and
immediately saw that it already makes a recursive call to
the org.apache.tika.parser.image.ImageParser. But ImageParser currently
only enriches metadata, and does not create <img> element itself. This is
done in the WordExtractor and respective handlers for types other, than MS
Word.

So my question is - do I have to move the creation of <img> to ImageParser
and remove it from WordExtractor?

Thank you.
Andrew.


On Wed, Jun 18, 2014 at 5:16 PM, Andrew Skiba <an...@tikalk.com> wrote:

> Hi,
>
> In the current code, the images from Word documents are referenced by
> "embedded:xxx" links in the generated HTML. This causes the browsers
> display "x" icon instead of the image.
>
> The proposed patch encodes the images using Data URI, if there is
> -Dtika.parsers.urlimages system property.
>
> http://en.wikipedia.org/wiki/Data_URI_scheme
>
> So the default behavior is the same, but users of the library can
> optionally generate self-contained HTML with correct images.
>
> Thank you,
>
> Andrew.
>