• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to include a WYSIWYG field including images in a PDF using iText 5?

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Summernote WYSIWYG edit on bootstrap to store formated text and images. The image is included in the text field and stored in the database (MySQL as mediumtext). There is no reference to the image. I am then using iText 5 on the server side (java) to create a PDF. The code works if there is no image included; however, when an image is included in the WYSIWIG input text field and stored on the databse then iText does not show the paragraph in the PDF, all other fields are included. How do I show the field corectly formated and including images please? My code is:




Kind reagrds,

Glyn

 
Saloon Keeper
Posts: 7590
177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What kind of content does "activityDtl.getPPDescription()" contain - HTML? Is "XMLWorkerHelper.parseToElementList" supposed to be able to convert all the HTML elements your data contains to structured PDF elements? I would imagine that there are limits as to what it can do...? Specifically, is it supposed to be able to handle images?
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"test" cannot "include an image". An image element and a text element are two very different types of elements.

PDFs originally could store images either in TIFF format or as scalable vector graphics (PostScript). Whether modern-day PDFs can directly support formats like JPEG or PNG is something that so far I've been too lazy to check, but regardless, to include an image on a PDF page you will need to have it converted (if necessary) to a compatible format, including scaling, if it's appropriate, and then insert it in the document using the proper API method.
 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim M and Tim H,

Within the html I have <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA ...>. I have found two issues:
1) Any tag without an ending (e.g., br, img) causes XMLWorkerHelper to throw an error
2) When I edit the original file in the database and add ending tags (e.g., </br>, </img>) the <img ...</img> is ignored (i.e., all other text before and after the image is in the PDF).

So I need to work out how to:
1) Parse the html when it is read in and add ending tags where they are missing (even though in html they are not required).
2) Display the image.

Your help would be greatly appreciated.

Kind regards,

Glyn
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh right, an XMLWorkerHelper wants XML, not HTML. Coincidentally there's another thread right now on the forum from somebody who tried using JTidy to convert their HTML to XHTML, which I've done myself, but they have the problem that their "image", which is actually a base-64 version of a QR code, isn't appearing in their PDF. But they aren't using that Summernote software so maybe you'll be more successful. At least JTidy should get you past the problem you have now.
 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

I have also found jTidy:



However, I need 'ppout' to be a string for:




How do I convert to string, or is this not the correct way?

Can you please provide a link to the other thread please?

Kind regards,

Glyn

 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glyndwr Bartlett wrote:How do I convert to string, or is this not the correct way?

Can you please provide a link to the other thread please?



There's no documentation on how Document's toString() method workis. I suppose you could try it and see what happens. The other thread's code uses parseDom() to output to a ByteArrayOutputStream instead, which it's clear what you should do with the output after that method finishes.

CONVERT HTML TO PDF JAVA
 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Mike's code worked a treat, thank you both. My issue is very close to Mike's with displaying the image.

Kind regards,

Glyn
 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To recap. I now clean the input to remove any MS Word tags that are not consistent with XHTML (e.g., <o:p>) and then convert the input HTML into XHTML. However when I use: the XMLWorkerHelper ignores the <img .... /> tag. My input is:

<p>Cooking instructions:</p>
<p><img
src="data:image/png;base64,iVBORw0KGgo ...RK5CYII="
alt="" /></p>
<p>Cook the fish.</p>

And:



Outputs



So the PDF has:



Instead of:



I have been researching and I have found this, which looks promising:

https://stackoverflow.com/questions/29194405/html-to-pdf-with-base64-images-throws-filenotfoundexception

Unfortunately, due to my limited programming capabilities, I am unable to implement the class:




I have extracted this code:



Which displays:




So the "img2" display is triggered.

So is:



The correct class to use and if yes how do I implement it? That is pass - ppDescription to it and then include the out put in:



To include the image in the PDF.


I have tried to share this with the other poster who has a very similar problem; however, their post is no longer available.


Kind regards,

Glyn

 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is what I need:

https://stackoverflow.com/questions/13843736/parsing-html-to-pdf-with-itext-image-base64/20938015#20938015

However, I am having issues with just getting started with it. For the line:

final HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new XMLWorkerFontProvider()));

How do I ignore css please (i.e., I do not have any css input).


Kind regards,

Glyn
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What went wrong when you didn't ignore the CSS that you don't have?

Okay, seriously. Seems to me that if you don't have any CSS then this "CSSApplier" thing would probably have no effect. And more seriously, what happened when you tried that code?
 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

OK, I have the code with just one error now. The issue is with the last two lines (in particular "parse") because I am using a variable instead of an address to the file:




The original has:



The error on "parse" is: The method parse(InputStream, Charset) in the type XMLParser is not applicable for the arguments (String, Charset)


The full set of code is:



 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glyndwr Bartlett wrote:The error on "parse" is: The method parse(InputStream, Charset) in the type XMLParser is not applicable for the arguments (String, Charset)



Well, yeah. You have a line of code which takes a String and makes an InputStream out of it. Then you have a line of code which wants an InputStream... but you don't give it the InputStream you went to the trouble of making.
 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Sorry about that. I have corrected the lines to:



I now have the following error occurring on the line:






How do I correctly pass the string "ppDescription" instead of a FileInputStream please?
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. You have a String containing the HTML. Then you don't want a FileInputStream because there's no File anywhere to be seen. Converting the String to an array of bytes and then putting that into a ByteArrayInputStream would be better. (Just like you did in your post from a week ago.)

Unless you can produce an array of bytes in the first place instead of a String. That would simplify the process of making a ByteArrayInputStream.
 
Glyndwr Bartlett
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Some success!



Outputs the text and the image. However, before I celebrate, I need the text and image to appear in the table where I currently have:




Currently it appears above the table.

As per attachment.

So I need to add each element into p6. I have a second field "ppRequirement" that is output to p7 that will have a similar output. How do I achieve this please.
Capture3.PNG
[Thumbnail for Capture3.PNG]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic