如何从html页面生成PDF

时间:2017-12-29 10:10:54

标签: php html forms pdf

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

$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;

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

3 个答案:

答案 0 :(得分:0)

请检查该代码

https://www.phpgang.com/how-to-convert-html-to-pdf-in-php-with-fpdf_447.html?doing_wp_cron=1514545193.9442338943481445312500

您必须下载fpdf库并将其包含在您设置的PHP文件中以及如何在pdf文件中显示标签,字体和图像。使用fpdf库,我们使用了程序员提供的HTMLparser库以及此处提供的所有其他库,您可以根据自己的要求下载和使用。

index.html:

<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<title>How to create Contact Form using Bootstrap  | PGPGang.com</title>
</head>

<body>
<h2>How to create Contact Form using Bootstrap example.&nbsp;&nbsp;&nbsp;=> <a href="https://www.phpgang.com/">Home</a> | <a href="http://demo.phpgang.com/">More Demos</a></h2>
<div class="container">
      <form class="contact-us form-horizontal" action="actionpdf.php" method="post">
        <legend>Fill Form and submit to generate PDF</legend>        
        <div class="control-group">
            <label class="control-label">Name</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-user"></i></span>
                    <input type="text" class="input-xlarge" name="name" placeholder="Name">
                </div>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Email</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-envelope"></i></span>
                    <input type="text" class="input-xlarge" name="email" placeholder="Email">
                </div>
            </div>    
        </div>
        <div class="control-group">
            <label class="control-label">Url</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-globe"></i></span>
                    <input type="text" id="url" class="input-xlarge" name="url" placeholder="http://www.example.com">
                </div>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Comment</label>
            <div class="controls">
                <div class="input-prepend">
                <span class="add-on"><i class="icon-pencil"></i></span>
                    <textarea name="comment" class="span4" rows="4" cols="80" placeholder="Comment (Max 200 characters)"></textarea>
                </div>
            </div>
        </div>
        <div class="control-group">
          <div class="controls">
            <button type="submit" class="btn btn-primary">Submit</button>
            <button type="button" class="btn">Cancel</button>
          </div>    
        </div>
      </form>
</div>
</body>
</html>

actionpdf.php

require('WriteHTML.php');

$pdf=new PDF_HTML();

$pdf->AliasNbPages();
$pdf->SetAutoPageBreak(true, 15);

$pdf->AddPage();
$pdf->Image('logo.png',18,13,33);
$pdf->SetFont('Arial','B',14);
$pdf->WriteHTML('<para><h1>PHPGang Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br>
Website: <u>www.phpgang.com</u></para><br><br>How to Convert HTML to PDF with fpdf example');

$pdf->SetFont('Arial','B',7); 
$htmlTable='<TABLE>
<TR>
<TD>Name:</TD>
<TD>'.$_POST['name'].'</TD>
</TR>
<TR>
<TD>Email:</TD>
<TD>'.$_POST['email'].'</TD>
</TR>
<TR>
<TD>URl:</TD>
<TD>'.$_POST['url'].'</TD>
</TR>
<TR>
<TD>Comment:</TD>
<TD>'.$_POST['comment'].'</TD>
</TR>
</TABLE>';
$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
$pdf->Output(); 
?>

答案 1 :(得分:0)

我不推荐这个超级简单任务的fpdf。 试试https://wkhtmltopdf.org/

在您的代码中

将很简单:

$htmlFileName = '/tmp/file.html';
$pdfFileName = '/tmp/file.pdf';
file_put_contents($htmlFileName, '<html>');
exec("wkhtmltopdf portrait $htmlFileName $pdfFileName");

答案 2 :(得分:0)

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

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

$userid$group_id我们将获得显示的表单数据。在按钮单击中,将触发ajax调用。

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。下面给出的是代码:

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.php filegen.php中的代码如下:

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

最后感谢大家不遗余力地帮助我。