如何从 html 页面生成 PDF [英] How to generate PDF from an html page

查看:26
本文介绍了如何从 html 页面生成 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用我的自定义插件显示的表单.下面给出了使用插件显示表单的代码:

I have a form which is displayed using my custom plugin. Below given is code to display form using plugin:

$question = $wpdb->get_results("SELECT * FROM $table_name1 WHERE group_id=$id");
$instruction = $question[0]->instructions;
$html = "<h2>Instructions</h2><p>".$instruction."</p><br><button>Download</button>";
$arr_size = sizeof($question);
$html .= "<form method='POST' action=''>";
for ($i=0; $i < $arr_size ; $i++) { 
    $sid = $question[$i]->s_id;
    $html .= "<b>Question:</b><i><span>".$question[$i]->questions."</span></i><br>";
    $answer = $wpdb->get_results("SELECT * FROM $table_name2 WHERE u_id=$userid AND s_id=$sid");
    $size = sizeof($answer);
    $option = $question[$i]->options;
    for ($j=1,$k=0; $j <= $option; $j++,$k++) { 
        $html .= "<input type='hidden' value='".$answer[$k]->a_id."' name='aid".$sid.$j."'>";
        $html .= "<textarea name='ans".$sid.$j."' id='ans".$sid.$j."'>".$answer[$k]->answer."</textarea><br>";
    }
}
$html .= "<input type='submit' value='submit' name='submit2'></form>";
return $html;

以前提交的值将在文本框中显示.当我点击下载按钮而不是表单提交时,我需要下载表单的内容.

Previous values submitted will be their in the text boxes. I need to download the contents of the form when I click the download button not on form submission.

推荐答案

我终于找到了问题的答案.首先要做的是下载您正在使用的pdf文件夹.这里我使用 MPDF.这是我的按钮 html 代码:

Finally I found answer for my question. First thing to do is download the pdf folder that you are using. Here I use MPDF. Here my html code for button:

<button class='pdfupload' id='".$userid."' dataoffset='".$group_id."'>Download</button>

$userid$group_id 我们将得到显示的表单数据.在按钮点击一个ajax调用被触发.

From $userid and $group_id we will get the displayed form datas. In button click an ajax call is triggered.

jQuery(document).ready(function(){
        jQuery('.pdfupload').click(function(){
            var group_id = jQuery(this).attr('dataoffset');
            var ID = jQuery(this).attr('id');
            jQuery.ajax({
                type:'POST',
                url:"<?=site_url()?>/uploadpdf.php",
                data:{group_id:group_id, ID:ID},
                success:function(result){
                    window.location.replace('<?=site_url()?>/filegen.php?name='+result);
                }
            });
        });
    });

在ajax页面中将生成pdf.要生成 pdf 文件,需要包含 mpdf.php.下面给出的是代码:

In ajax page the pdf will be generated. To generate pdf file mpdf.php need to be included. Below given is the code:

include('MPDF57/mpdf.php');
include 'wp-load.php';
global $wpdb;
$date = date("Ymdhis");
$filename = $id.'trialbasic'.$date.'.pdf';
$path = dirname(__FILE__).'/pdfs/'.$filename;
$html = "Your pdf content"; //Here in my code the details using `$userid` and `$group_id` will be fetched 
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output($path,'F');
echo $filename;

这里将生成 PDF 文件并存储在 $path 中.ajax 返回的将是 PDF 文件的名称.在成功部分调用 filegen.php 以直接点击下载 PDF 文件.ajax的结果会传递给filegen.phpfilegen.php中的代码如下:

Here PDF file will be generate and stored in $path. The return of ajax will be the name of the PDF file. In the success part filegen.php is called in order to directly download the PDF file on click. The result of ajax will be passed to filegen.php The code in filegen.php is given below:

include 'wp-load.php';
$name = $_GET['name']; 
$path = dirname(__FILE__).'/pdfs/'.$name;
header('Content-Disposition: attachment; filename=' . $name);  // Make the browser display the Save As dialog
readfile($path);  //this is necessary in order to get it to actually download the file, otherwise it will be 0Kb

最后感谢大家抽出时间来帮助我.

Finally thanks all for sparing your time to help me.

这篇关于如何从 html 页面生成 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆