0

enter image description hereI am just trying to create a pdf file and I am getting the error "jspdf is not defined"

Here is my code

<?php

        $doc = JFactory::getDocument();
        $doc->addScript(JUri::base() .'js/jquery-1.7.1.min.js',true);
        $doc->addScript(JUri::base() .'js/jspdf.debug.js',true);
        $doc->addScript(JUri::base() .'js/basic.js',true);
        $doc->addScript(JUri::base() .'js/png.js',true);
        $doc->addScript(JUri::base() .'js/png_support.js',true);
        $doc->addScript(JUri::base() .'js/zlib.js',true);
        $doc->addScript(JUri::base() .'js/FileSaver.js',true);
        $doc->addScript(JUri::base() .'js/tableExport.js',true);
        $doc->addScript(JUri::base() .'js/jquery.base64.js',true);
        $doc->addScript(JUri::base() .'js/html2canvas.js',true);
        $doc->addScript(JUri::base() .'js/standard_fonts_metrics.js',true);
        $doc->addScript(JUri::base() .'js/split_text_to_size.js',true);
        $doc->addScript(JUri::base() .'js/from_html.js',true);
    $script = "function loadtable(){
    var doc = new jsPDF();
    doc.text(20, 20, 'Hello world.');
    doc.save('Test.pdf');}";
    $doc->addScriptDeclaration($script);
    echo "<button id='buttonMU' class='gen_btn' onclick='loadtable()'>Load Pdf</button>";
    ?>

But I do have the javscript included in my php code. Please advise.

Learner2011
  • 287
  • 2
  • 6
  • 25

1 Answers1

0

Check the source code of your output page. You're probably calling your javascript function before the DOM has finished loading.

Try ensuring your code runs after the page has loaded;

object.onload=function(){myScript};

$script = "window.onload = function(){
    function loadtable(){
    var doc = new jsPDF();
    doc.text(20, 20, 'Hello world.');
    doc.save('Test.pdf');
}";
Martyn Shutt
  • 1,671
  • 18
  • 25
  • No, he's not calling it too early, since it is triggered by a button's `onclick` event listener. In fact, waiting for the document to be loaded will result in the `loadtable` function being undefined at DOM parsing time. – blex Aug 17 '15 at 18:58
  • 1
    @blex ah, fair enough. Just wanted to rule it out. It's a common issue I've come across in Joomla. – Martyn Shutt Aug 17 '15 at 19:01
  • @MartynShuttSeems to me like a Joomla issue.I was able to generate the pdf file in a html page but in a php page it is failing. – Learner2011 Aug 17 '15 at 19:02
  • @Learner2011 Joomla can be very particular, and hard to debug sometimes. – Martyn Shutt Aug 17 '15 at 19:04