I am generating a pdf file using jspdf library and I am getting the error “jspdf is not defined”

房东的猫 提交于 2019-12-12 03:17:34

问题


I 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.


回答1:


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');
}";


来源:https://stackoverflow.com/questions/32057559/i-am-generating-a-pdf-file-using-jspdf-library-and-i-am-getting-the-error-jspdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!