Show an image in a pdf generated from a xsl - java

I am trying to pass a 64 base image to my xsl via parameters.
based in this question (XSLT show a base64 as a image) I wrote:
<fo:external-graphic src="data:image/png;base64,$sign_2"/>
and any image is output (to a PDF)
I also tried
<fo:external-graphic src="url('data:image/png;base64,$sign_2')"/>
with same results!!! (No image output in my pdf)
Any hint for this ?
TIA,
Yamil

Related

InvalidPDFException when viewing the Chinese content PDF using Primeface DefaultStreamedContent

Using Primefaces 6.0 and
JAVA 1.8
Using below code to view the PDF document using DefaultStreamedContent.
<pe:documentViewer locale="en" height="600" value="#{documentMBean.pdfFile}" id="pdfDocViewer" >
Below code to get the Stream as pdfFile.
byte[] documentData = null;
setPdfFile(new DefaultStreamedContent());
documentData =//Getting the byte array from DB
getPdfFile().setStream(new ByteArrayInputStream(documentData));
getPdfFile().setContentType("application/pdf");
getPdfFile().setContentEncoding("UTF-8");
When I upload and store a PDF file which contents Chineese charactes getting below exception in PDF Viewer.
PDF.js v1.0.21 (build: f954cde) Message: InvalidPDFException
Note: Plain text PDF which contents English characters working fine and able to view the PDF.
I have tried to set different character-encoding such as UTF-8,UTF-16
Please assist me how to resolve above exception. Where I can find PDF.js for further analysis ?

Keep font when converting DOCX to PDF using Docx4j

Here's a code to convert docx to pdf
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
Docx4J.toPDF(wordMLPackage, baos);
The problem here is that the generated PDF font is always Times New Roman, which is not the case, the docx template font is actually different, Garamond.
What could be missing or wrong here?
If a font is not installed on the system or embedded in the document, MS Word performs a silent font substitution. To check whether this is happening for your document, open it in Word, go to Word Options > Advanced > Show Document Content and press the Font Substitution button. (Word 2007)
If the font is embedded in the document, or you already have the font installed on your system, then I do not know what to tell you because ideally docx4j should use that font.
If that isn't the case, i.e. Word is using font substitution (which you can determine using the steps above), you can embed the Garmond font in the document using Word Options > Save > Embed Fonts in File. Run through your conversion program and check again.

Control the origin of images in an SWT browser

I'm programming a desktop application using SWT and I use the browser in parts of the interface because of the flexibility.
I easily can introduce external images. An image in the file system:
<img src="/home/user/image.jpg" />
Or an image on the web:
<img src="http://some.cl/image.jpg" />
Can I obtain the images from a stream? In some place of my code I want to program something like this:
OutputSteam getExternaResource(String resourcePath)
I want to arbitrarily control the origin of the request.
I don't know of a direct way to do this, all I can think of is using javascript to set the image data as base64 string into the src of the image.
Using org.eclipse.swt.browser.Browser.execute(String) or maybe use org.eclipse.swt.browser.BrowserFunction.
The images should have an id which can be used in javascript:
<img id="image1" />
Edit: on the other hand, maybe it's easier to just parse the HTML previously and set the image base64 string there.
Depending on how you get the HTML you could do:
if you create the HTML yourself, just use <img src="data:image/png;base64.... convert the image to base64 and put it in the src attribute
if you read the HTML from an external source, you could use JSoup to parse the HTML and replace the image src attribute with a base64 string. afterwards use Browser.setText(String) to set the HTML of the browser, be aware that in that case relative paths (in links or images) don't work.
String html = "html";
Document doc = Jsoup.parse(html);
Elements img = doc.getElementsByTag("img");
for (Element element : img) {
String src = element.attr("src");
// READ image using the existing src, convert to base64 (using java.util.Base64)
String base64 = "";
element.attr("src", "data:image/png;base64,"+);
}
String newHtml = doc.html();
browser.setText(newHtml);
If you have control over the HTML page, i.e. it is generated by your code, possibly from a template, then you can embed the image.
The bytes of the image need to be base64 encoded and appended to the src attribute of the image tag like described here: http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/

Is it possible to show Image object in a jTextEditor as HTML tag?

I have java.awt.Image img, And want to add it to myjTextEditor as HTML tag, the known way to imbed image by img src=.... , didnt work:
String Html="<img src=\""+thumb+"\"/><br/>";// thumb is an Image Object
myjTextEditor.setText(Html);
That code will work fine if thumb is a file... is it possible to do that? (i.e imbed Image object)??
Well if you can base64 encode thumb data you can use like this:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
The section after image/png;base64, is base64 encoded binary image data.

Text to Captcha Image using java

How to convert text to captcha image.
EDIT
i looked at jcaptcha. It creates a random text for a id. But i need
a image with the given text.
Jcaptcha provides classes to convert a text into image.
sample code:
WordToImage image = ComposedWordToImage(fontGenerator, background,textPaster);
BufferedImage image = image.getImage("text");
api doc of WordToImage interface

Categories