Ho to use jsPDF in Mobile SDK ?

You must Login before you can answer or comment on any questions.

Hi, I would like to generate some pdf file with jsPDF. Any idea of how to use/modify it for mobile application ? Regards

5 Answers

Accepted Answer

Armindo,

This was pretty simple actually. I'd really never heard of jsPDF until I read this post, but I was able to created a plugin with an example app that generates PDFs using the library fairly quickly. You can grab the example app and the plugin from my GitHub repo.

Happy Coding ...

Great Anthony !! It works fine and using it with Airprint module will be great :-) Regards

Armindo

I've seen a problem with JSPDF, it seems that it doesn't support unicode, French accent show strange char :-(

Anthony, I have found another one more complete, but exactly the same in terms of function calls. https://github.com/Marak/pdf.js I have tryed to tweak it to make it compatible with Ti but I have some font error, so my tweak may be bad. May be it could be usefull to add this one also in your plugin. The only difference for using it is

var doc = new pdf();
instead of
var doc = new jsPDF();
the other stuff is the same, of course more function in this one like the possibility to draw lines, rectangles etc...

Regards

I recently started looking a jspdf. jsPDF seems to be in active development and their latest version is awesome. I am attempting to create a pdf with attached images, which is possible to do in a browser, using canvas , canvas.toDataURL , and atob() . Unfortunately this doesn't work out of the box in Titanium. When I read a JPEG file , the returned blob cannot be read by jsPDF , as it's not a string. Ti.Utils.base64encode and decode are not totally useful , mainly because de decode method returns a TI.Blob. I'm now trying to Ti.Utils.base64encode to get a base64 encoded string, and then use another js base64 decoder to return a JavaScrip string. If this works, we can say that we are now able to also add images to pdf's. I'll keep you posted. Help with this is also welcome.

— answered 10 months ago by Richard Lustemberg
answer permalink
3 Comments
  • Hi , are you able to add image in pdf , if , then can you please provide me piece of code to get it done. Thanks .

    — commented 7 months ago by Moiz Chhatriwala

  • I managed to get the raw data using streams as in the following example. There are no errors thrown however jspdf just creates an empty box without displaying the image:

    var fileContent = Ti.Filesystem.getFile('myfile.jpg').read();
            var CHUNK_SIZE = 10;
     
            var read_buffer = Titanium.createBuffer(
            {
                length: fileContent.length * 2 // just to be sure the buffer is big enough
            });
     
            var stream = Titanium.Stream.createStream(
            {
                mode: Titanium.Stream.MODE_READ,
                source: fileContent
            });
     
            var length = 0;
            var bytes_read = 0;
     
            while ((length = stream.read(read_buffer, bytes_read, CHUNK_SIZE)) > 0) 
            {
                bytes_read += length;
            }
     
            var str = Ti.Codec.decodeString(
            {
                source: read_buffer,
                charset : Ti.Codec.CHARSET_ASCII,
                length: bytes_read,
                position: 0
            });
     
            jspdf.addImage(str, 'JPEG', 10, 10, 50, 50);
    Note that jspdf is able to get the correct image size and the rest of the binary data seems to be okay, too.

    — commented 6 months ago by Christopher Fox

  • Yes , you are right Christopher , even I am not getting correct output , Is there any alternative to generate pdf including images. Thanks.

    — commented 6 months ago by Moiz Chhatriwala

Your Answer

Think you can help? Login to answer this question!