0

I'm developing a Chrome App, and i would like to allow the user to export as a PDF a few HTML Table. I made some research and i found jsPdf as the plugin i needed. And now comes my issue. jsPDF and its function "fromHTML" use the method "document.writeln()" which is not available in Chrome Packaged App.

"Uncaught Error: document.writeln() is not available in packaged apps."

Console error as jspdf tries to use document.writeln()

I've made a ton a research over the internet, and found that there are some ways to counter that issue, by adding some "document.create". But while i don't know how jspdf works, i don't really know what to create.

There is the part of jspdf that's causing the issue :

var $frame, $hiddendiv, framename,  visuallyhidden;

framename = "jsPDFhtmlText" + Date.now().toString() + (Math.random() * 1000).toFixed(0);

visuallyhidden = "position: absolute !important;" + "clip: rect(1px 1px 1px 1px); /* IE6, IE7 */" + "clip: rect(1px, 1px, 1px, 1px);" + "padding:0 !important;" + "border:0 !important;" + "height: 1px !important;" + "width: 1px !important; " + "top:auto;" + "left:-100px;" + "overflow: hidden;";

$hiddendiv = document.createElement('div');
$hiddendiv.style.cssText = visuallyhidden;
$hiddendiv.innerHTML = "<iframe style=\"height:1px;width:1px\" name=\"" + framename + "\" />";
document.body.appendChild($hiddendiv);

$frame = window.frames[framename];
$frame.document.open();
$frame.document.writeln(element);//This causes "Uncaught Error: document.writeln() is not available in packaged apps."
$frame.document.close();
return $frame.document.body;

And here is my call of the jspdf library, maybe i made a mistake here :

$('#exportMonth').click(function(){
    console.log(TAG + "Starting rendering as PDF");

    var doc = new jsPDF();
    specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        // here i've nothing to bypass, so nothing is 'bypassme'
        '#bypassme': function(element, renderer) {
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    };
    //the id 'month' refers to a div containing a html Table
    doc.fromHTML($('#month').html(),15,15 ,{
        width:600,
        elementHandler:specialElementHandlers
    });
    doc.save('Tst.pdf');

    console.log(TAG + "Should be done");
});

I'll continue my research but i'm really lost rigth now, i really need your help !

I may have forgotten to say some things, just let me know and i'll try to be as quick as possible fixing that.

Many thanks !

Kwarthys
  • 11
  • 5
  • You can check this link: http://stackoverflow.com/questions/16954731/google-closure-and-chrome-packaged-apps-compatibility – abielita Jun 30 '16 at 09:23
  • @abielita Thanks for considering my issue. I've already seen that post a few times, and my point is that i don't know what jspdf want to write, so i can't replace it by document.create('i dunno what'). – Kwarthys Jun 30 '16 at 13:14

1 Answers1

0

After many research and a few rageQuits i found a solution. I don't know really why it works but it does, and i'll share this here in case one of you run into this issue. I guess it's more like a trick / hack rather than a real solution.

Replacing this :

doc.fromHTML($('#month').html(),15,15 ,{
    width:600,
    elementHandler:specialElementHandlers
});
doc.save('Tst.pdf');

By this :

doc.fromHTML($('#month')[0],15,15 ,{ //ONLY CHANGE IS HERE
    width:600,
    elementHandler:specialElementHandlers
});
doc.save('Tst.pdf');

will work. No more Error generated and pdf successfully downloaded to local file.

If someone knows why this works i'll be happy to learn, cause this is kindda disturbing.

Hoping that will aid somneone one day,

Thanks a lot 'n' see you later !

Kwarthys
  • 11
  • 5