Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用MathJax转换数学公式以保存在PDF中_Javascript_Php_Wordpress_Mathjax_Mpdf - Fatal编程技术网

Javascript 使用MathJax转换数学公式以保存在PDF中

Javascript 使用MathJax转换数学公式以保存在PDF中,javascript,php,wordpress,mathjax,mpdf,Javascript,Php,Wordpress,Mathjax,Mpdf,到目前为止-我们在WP上有在线考试web应用程序。我正在将问题纸保存为PDF格式,而不以HTML格式打开它 如果我在网页上显示问题纸,由于mathjax.js,它会适当地转换所有数学公式。当数据(问题纸)出现在网页上时,mathjax.js可以工作。但是,如果我试图保存相同的问题文件作为PDF文件,而不打开任何网页,这是行不通的。因为mathjax.js需要显示网页上的数据 我的实验- 1.当我从DB接收数据(问题纸)时,我尝试加载mathjax.js。我认为这将把公式转换回原始状态。但事实并非

到目前为止-我们在WP上有在线考试web应用程序。我正在将问题纸保存为PDF格式,而不以HTML格式打开它

如果我在网页上显示问题纸,由于mathjax.js,它会适当地转换所有数学公式。当数据(问题纸)出现在网页上时,mathjax.js可以工作。但是,如果我试图保存相同的问题文件作为PDF文件,而不打开任何网页,这是行不通的。因为mathjax.js需要显示网页上的数据

我的实验- 1.当我从DB接收数据(问题纸)时,我尝试加载mathjax.js。我认为这将把公式转换回原始状态。但事实并非如此。因为,mathjax需要在网页上显示数据

  • 如果我尝试打印数据(问题纸),它会转换公式,而不是保存为PDF。因为,这次数据在网页上
  • 我想要什么-我希望数学公式以PDF格式显示。仅使用mathjax转换公式。所以,我只能使用mathjax将其重新转换为原始状态

    如何才能做到这一点

    <?php
    //db connection
    $dbConn = mysql_connect ("localhost", "username", "password") or die ('MySQL connect failed. ' . mysql_error());
    
    //db selection
    mysql_select_db("db_name") or die('Cannot select database. ' . mysql_error());
    
    $id = $_REQUEST['id'];
    
    if($_REQUEST['action'] == "print") {
        mysql_query('SET character_set_results = utf8');
        $result = mysql_query("SELECT question_id FROM wpc6_emp_exam_questions WHERE exam_id = '$id' ");
        $exam_details = mysql_fetch_assoc(mysql_query("SELECT name, duration, passing_percent, negative_marking  FROM wpc6_emp_exams WHERE id = '$id' "));
        $num = mysql_num_rows($result);
        while($row = mysql_fetch_assoc($result))
        {
            $results[] = $row['question_id'];
            $question_id = $row['question_id'];
            $result1 = mysql_query("SELECT question, option1, option2, option3, option4 FROM wpc6_emp_questions WHERE id = '$question_id' ");
            while($row1 = mysql_fetch_assoc($result1))
            {
                $results1[] = $row1;
            }
    
        }   
    }
    ?>
    
    <!-- Mathjax Script Start -->
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({extensions: ["tex2jax.js"],jax: ["input/TeX", "output/HTML-CSS"],tex2jax: {inlineMath: [["$", "$"],["\\(", "\\)"]]}});</script>
    <!-- Mathjax Script End -->
    
    
    <?php
    
    $html .= '
        <style>
        p, div { font-family: freeserif}
        </style>
        <div style="text-align: center;"><img src="logo_3.png"></div>
        <br/><hr><br/>
    
        <div style="float: left; width: 70%;">Exam - '.$exam_details['name'].'</div>
        <div style="float: right; width: 30%;">Duration - '.$exam_details['duration'].' Mins</div>
        <div style="float: left; width: 70%;">Passing Percent - '.$exam_details['passing_percent'].'</div>
        <div style="float: right; width: 30%;">Negative Marking - '.$exam_details['negative_marking'].'</div><hr>';
    
    
    for($i=1; $i<=count($results1); $i++) {
        $question = $i.'. '.$results1[$i-1]['question'];
        $question = str_replace(array("\\","[","]"),"",$question);
        $question = str_replace("(","<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(",$question);
        $option1 = $results1[$i-1]['option1'];
        $option1 = str_replace(array("\\","[","]"),"",$option1);
        $option2 = $results1[$i-1]['option2'];
        $option2 = str_replace(array("\\","[","]"),"",$option2);
        $option3 = $results1[$i-1]['option3'];
        $option3 = str_replace(array("\\","[","]"),"",$option3);
        $option4 = $results1[$i-1]['option4'];
        $option4 = str_replace(array("\\","[","]"),"",$option4);
    
        $html .= '<p style="font-size: 12pt; font-family: freeserif "><div style="float: left; width: 100%; margin-bottom:5px; font-family: freeserif">'.$question.'</div>';
    
        if(strlen($option1)>40 || strlen($option2)>40 || strlen($option3)>40 || strlen($option4)>40 ) {
            $html .= '<div>(a) '.$option1.'<br/>'
                .'(b) '.$option2.'<br/>'
                .'(c) '.$option3.'<br/>'
                .'(d) '.$option4.'<br/>
            </p>';
        } else if(strlen($option1)>15 || strlen($option2)>15 || strlen($option3)>15 || strlen($option4)>15 ) {
            $html .= '<div style="float: left; width: 50%;">(a) '.$option1.'</div>'
                .'<div style="float: left; width: 50%;">(b) '.$option2.'</div>'
                .'<div style="float: left; width: 50%;">(c) '.$option3.'</div>'
                .'<div style="float: left; width: 50%;">(d) '.$option4.'</div>
            </p>';
        } else if(strlen($option1)<=15 || strlen($option2)<=15 || strlen($option3)<=15 || strlen($option4)<=15 ) {
            $html .= '<div style="float: left; width: 25%;">(a) '.$option1.'</div>'
                .'<div style="float: left; width: 25%;">(b) '.$option2.'</div>'
                .'<div style="float: left; width: 25%;">(c) '.$option3.'</div>'
                .'<div style="float: left; width: 25%;">(d) '.$option4.'</div>
            </p>';
        }
    }
    
    $html .= '<br/><br/><br/><div style="text-align: center"> --------- O &nbsp;&nbsp;Best of Luck&nbsp;&nbsp; O --------- </div>';
    
    include("../mpdf.php");
    
    $mpdf=new mPDF('');
    $html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');
    $mpdf->SetWatermarkImage('logo_1.png');
    $mpdf->showWatermarkImage = true;
    $mpdf->WriteHTML($html);
    
    ?>
    

    为什么不直接传递生成的HTML?谢谢您的评论。您能详细说明一下吗?一种方法是将生成的HTML传递给mathjax节点,并将返回的HTML传递给mpdf。您只需了解如何从PHP.Ok调用NodeJS工具(或CLI工具)。谢谢我试试看。