HTML to PDF conversion with images - android

I am having an issue to parse an image within HTML structure to a PDF file, I have tried using the following line to gather the image , however the PDF file is being created without the image. I am using iText with xmlworker libraries, the html is all stored in a string then parsed into an input stream as shown below in code.
.....
<img class='top' src='file:///android_res/drawable/logo.png' height='100px' width='200px'/>
........
........
InputStream is = new ByteArrayInputStream(str.getBytes()); //contains the html string
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);

You should probably use your own ImageProvider to fetch the images. (http://api.itextpdf.com/xml/com/itextpdf/tool/xml/pipeline/html/ImageProvider.html)
This interface has 4 methods:
getImageRootPath()
reset()
retrieve(String src)
store(String src, Image img)
You'll need to implement the retrieve method to return the Image from your file system.
Documentation on XMLWorker: http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html#itextdoc-menu-10

Related

android Picasso load image from storage

i am using Picasso for load image from web and local storage
for load image from web
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
and for load image from storage i add file:// before path for load image
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
but when file path have utf-8 charchter with file name its not load the image like file:///android_asset/۲۰۱٧.png
any idea how to solve it every time i want to use picaso its must string path not Uri or File because its saved in sqlite as string
Try this code:
String url = "file:///android_asset/۲۰۱٧.png";
URLEncoder.encode(url, "UTF-8");
Picasso.with(context).load(url).into(imageView2);
try doing
URIUtil.encodeQuery(url)
see the issue at:
https://github.com/square/picasso/issues/652

Using webview to load image from rest server in Android

I have a rest server hosted in Amazon written in Delphi with a method returning a jpg Stream image.
I need download the image and show in webview.
The method Rest Server method is something as:
//delphi code
function TSrvServerMetodos.ImagePac(pront:integer): TStream;
var blob:TStream;
begin
...
Result := CreateBlobStream(fieldbyname('PHOTO'),bmRead);
end;
where the Result is a jpeg Stream with a image.
To access the remote rest method I am trying to use the next:
String url = Common.getServerUrl() +"/"+ "\"ImagePac\"";
String URL=url+"/"+"23";
webView.loadUrl(URL);
But no Image is showing in webview.
My image is comming from a rest server and no a website. I have no way to call something as webview.loadUrl("http://www.myserver.com/myimage.jpg");
Is possible load that image with webview?
Regards, Luiz
Yes, It is. To solve the problem I had to save the image to sdcard and create a html string to load the image with webview.
For the new offspring with the same trouble. Just encode image in base64 and place it in src attribute of img tag

Can I make Android WebView support other image formats (e.g. TIFF)?

For example, how to make the following HTML page actually display the image, in an Android WebView?
<html>
<body>
<img src="http://www.alternatiff.com/sample.tif">
</body>
</html>
Any (hacking) suggestion is appreciated. I am only an app developer and cannot control the whole system, so modifying the OS source code is not applicable.
I haven't tried this, but this is what I would do:
Make a subclass of WebViewClient
Override shouldInterceptRequest() to check the URL and see if a TIFF was requested. If it was not a TIFF, return null to tell the WebView to handle the request itself.
If a TIFF was requested, open HttpURLConnection to the TIFF url and read the data, convert the TIFF to a JPEG or PNG ex. How to convert TIFF to JPEG/PNG in java and set up an InputStream to read the JPEG/PNG image bytes.
Return a WebResourceResponse with the mime type (i.e. image/jpeg) and the InputStream you created to read the image data.
Call setWebViewClient on the webview with an instance of your WebViewClient subclass.
Rather than converting on the device using a JNI library, I think I would convert the image on a server and open the HttpURLConnection to the pre-converted image stream, i.e. http://example.com/convert_tiff?url=http%3A%2F%2Fwww.alternatiff.com%2Fsample.tif&fmt=JPEG and then return that InputStream in the WebResourceResponse. I guess it depends on how cheap the bandwidth and server resources are for you.

adding a image from cam to a dynamically made html string

I want to add a image that i take from phones camera
and add it to a html that is build dynamically from user input.
so i cant get the html file from assets.
all the question/answers i found are for local images and html from assets.
Android - local image in webview
Android Development: Using Image From Assets In A WebView's HTML
any suggestions?
Thanks
One of the answers in the links you showed, suggests to use Base64 embedded image:
(https://stackoverflow.com/a/9000691/1271435)
<img src="data:image/jpg;base64,xxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
xxxxx = base64 encoded string of images bytes
This sounds like a good idea, and one way to do it is something like this:
After taking the image with camera you should get back an imageUri of the photo.
then:
1.Get the input stream of that image:
InputStream stream = getContentResolver().openInputStream(imageUri);
2.Fill in the stream into a ByteArrayOutputStream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fillStream(stream, baos); // dummy method, provide implementation
3.Convert the byte array to Base64:
String base64Image = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
Then you can use the base64 string as the data for your image in HTML.

Can a html img tag read base64 files?

Short question: Can a html img tag read a base64 encoded image file?
<img src='path-to-file/image.base64'>
Because I'm having trouble. It fires the onerror event but doesn't give a reason (well at least it doesn't say it couldn't find the file)
Oh! Some context: I'm trying to cache images to sdcard on android as base64 encoded files. That part works, but when I try to load the cached image it fails :(
The syntax to source the image from inline data is:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="your image">
Okay it turns out that base64 is only for inline use (or if you have the string you can set it with javascript)
So this works:
var image = new Image();
image.src = base64_string;
But it doesn't work when you point the src to a file containing the base64_string
var image = new Image();
image.src = "path/base64.txt";
PS: Thanks for the comments, this pointed me in the right direction

Categories

Resources