Core-13 / jsPDF-image-support

Adds image support to jsPDF when used in Titanium
Other
9 stars 6 forks source link

Android and images. #1

Open albinotonnina opened 10 years ago

albinotonnina commented 10 years ago

Hi, thanks for this! Anyway...with Android and the demo I have rendering problems, here's a screenshot: screenshot_2014-02-14-12-28-26

Looks like a strange problem...if I open that pdf via osx preview app the pdf is correct. With google drive or adobe reader on a pc I have funny results...

edit: so the problem is the original size of the images (1:1 images work fine) And I'm sure you know it, in fact: jspdf.plugin.addimage.js line 163 //TODO: this needs to use the original image width/height //var dims = getJpegSize(imageData);

albinotonnina commented 10 years ago

Google drive preview: schermata 2014-02-14 alle 12 34 32 schermata 2014-02-14 alle 12 34 23

cesar-ifg commented 10 years ago

Hi Albino, you solved the problem with this?

//var dims = getJpegSize(imageData);

albinotonnina commented 10 years ago

Hi, not yet. for iOS I can do this: var imageView = Ti.UI.createImageView({image:imageData, width:"auto", height:"auto"}); var imageW = imageView.toImage().width,imageH = imageView.toImage().height;

but that's not good for Android.

mobiomic commented 10 years ago

Hi Albino, how did you manage to fix this for iOS? Just by adding the code above? I'm having similar troubles in that my PDF is fine when viewed in OSX Preview and on the iPad itself, but when viewing in Acrobat on a Mac or PC the images are missing. It's driving me nuts!

albinotonnina commented 10 years ago

Exactly, just by adding the code above, try! But I don't have missing images, i have just distorted images (screenshot above). Android is still a problem...a very ugly fix is: create a imageview with your image, add it to a window, wait for some milliseconds, get size using toImage() function, then remove the imageview and then do the rest. very bad :)

mobiomic commented 10 years ago

All that's doing is crashing the app on the device - whereabouts are you adding it to? Underneath //var dims = getJpegSize(imageData);?

albinotonnina commented 10 years ago

yes!

schermata 2014-02-17 alle 15 09 18

schermata 2014-02-17 alle 15 10 14

mobiomic commented 10 years ago

Doing that crashes the app on the device, although it's fine on the simulator. I did notice though that when I made some of my images smaller, they did display (albeit it distorted like yours were) so maybe they're not actually missing, but are still there.

albinotonnina commented 10 years ago

I believe you, though it doesn't crash on my iphone 5s.

mobiomic commented 10 years ago

New iPad Mini I'm using. Some of my images are pretty large though and I'm shrinking them down quite a lot to make the text on the PDF nice and crisp. Don't know if that's the problem...

albinotonnina commented 10 years ago

Anyway Core (don't know your name)...I'm planning to move to uk...do you think it's a good choice? :P

mobiomic commented 10 years ago

Where do you live now?!

albinotonnina commented 10 years ago

Rome...

albinotonnina commented 10 years ago

15 years of experience (front end developing, mostly), i had a company now (numidia.it) but I worked as employee a lot too...

mobiomic commented 10 years ago

Lots of front-end jobs going, London mostly if you can handle the astronomical cost of living.

albinotonnina commented 10 years ago

Well, thanks for that! I think the same...I'd love to go to a smaller city like Bristol, Manchester, Liverpool etc but London has more possibilities...we'll see!

mobiomic commented 10 years ago

Manchester is the next best place outside London to be honest - a real digital hub. I live just near there actually. I'll do you a swap, always fancied living in Italy!!

albinotonnina commented 10 years ago

Italy is nice...if you are a tourist :) I'll look for Manchester...you have a free beer from me :)

mobiomic commented 10 years ago

If I could get this PDF working, the drinks would be on me all night!

albinotonnina commented 10 years ago

eheheh...I'll continue trying too

cesar-ifg commented 10 years ago

the original library (http://parall.ax/products/jspdf), mentioned that the image must be encoded to base 64 ... I was doing some tests so far have been unsuccessful ...maybe I'm missing something, any recommendations?

i try some changes, in jspdf.plugin.addimage.js line 163, modify this:

//TODO: this needs to use the original image width/height
//var dims = getJpegSize(imageData);

for this:

// TODO: this needs to use the original image width/height
((w=null) || (h==null)) ? var dims = getJpegSize(imageData) : var dims = [w,h];

and in app.js, change this:

var imgSample1 = Ti.Filesystem.resourcesDirectory + 'image1.jpg';
doc.addImage(imgSample1, 'JPEG', 10, 20, 128, 96);

for this:

var imgSample1 = Titanium.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + 'image1.jpg');
var readestImgSample1 = imgSample1.read();
var dataImgToAdd1 = 'data:image/jpeg;base64,'+Titanium.Utils.base64encode(readestImgSample1).toString(); 
//alert(dataImgToAdd1);
doc.addImage(dataImgToAdd1, 'JPEG', 10, 20, null, null);   
cesar-ifg commented 10 years ago

another attempt ... on this occasion got the measurements of the image on an Android device but the file is not created successfully.

in app.js change this:

btn.addEventListener
(
    'click', 
    function (e) 
    {
        //original code  ...blah blah blah!
    }
);

for this:

var imgToAdd1;
var tempImageView;

btn.addEventListener
(
    'click', 
    function (e) 
    {
        imgToAdd1 = Titanium.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + 'image1.jpg');
        tempImageView = Ti.UI.createImageView ( { } );
        tempImageView.addEventListener("load",funcCreatePDF);
        tempImageView.setImage(imgToAdd1);
        win.add(tempImageView);
    }
);

function funcCreatePDF()
{
    var doc = new _jsPDF();
    var blob = tempImageView.toBlob();
    (blob === null) ? Ti.API.info("unable to get image size") : alert(blob.width + "x" + blob.height);
    doc.addImage(imgToAdd1, 'JPEG', 10, 20, blob.width, blob.height);

    var timeStampName = new Date().getTime();
    if (_tempFile != null) {
        _tempFile.deleteFile();
    }
    _tempFile = Ti.Filesystem.getFile(Ti.Filesystem.getTempDirectory(), timeStampName + '.pdf');            
    doc.save(_tempFile);
    funcOpenFile(_tempFile); //function containing the remaining code to display the file generated
}
LazyMink commented 10 years ago

Hello, nice mod. Has this bug been resolved on android?

aCasasItexico commented 10 years ago

i have same issue on android, i can't see images but text it's working well... So, in iOS woks everything, somebody has the answer? thanks