将html表单导出为pdf

时间:2016-02-25 22:27:22

标签: php html export

我创建了一个包含许多字段的html表单,我想将页面导出为pdf。 html代码如下:

<!DOCTYPE html>
<html>
<body>
<form name="htmlform" method="post" action="html_form_send.php">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name">First Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
 </td>
</tr>

<tr>
 <td valign="top"">
  <label for="last_name">Last Name *</label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email">Email Address *</label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="telephone">Telephone Number</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments">Comments *</label>
 </td>
 <td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
 </td>

</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit">  
 </td>
</tr>
</table>
</form>
<form action="myphpfile.php" method="post">
  <input type="submit" value="Download as pdf!">
</form>
</body>
</html>

我有php代码从http://freehtmltopdf.com/将表单导出为pdf,但每当我点击&#34;下载为pdf&#34;按钮它只是显示文件中的PHP代码。我的网络服务器在XAMP上运行,html文件在htdocs中。我需要进一步帮助将此html转换为pdf。

我从网站获得的PHP代码是:

<?php

    $url = 'http://freehtmltopdf.com';
    $data = array(  'convert' => '', 
            'html' => '<html><head><title>My Title</title><link rel="stylesheet" type="text/css" href="relativepath.css"></head><body><h1>Web Page</h1><p>This is my content.</p></body></html>',
            'baseurl' => 'http://www.myhost.com');

    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    // set the pdf data as download content:
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="webpage.pdf"');
    echo($result);

?>

1 个答案:

答案 0 :(得分:0)

尝试使用FPDF解决您的问题。

示例如下:

<强>的index.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>How to create Contact Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<style>
    .header {
        color: #36A0FF;
        font-size: 27px;
        padding: 10px;
    }
</style>
</head>
<body>
<div class="container">
<div class="page-header">
    <h1>Convert HTML to PDF in PHP with fpdf</h1>
</div>
<!-- Contact Form - START -->
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="well well-sm">
//action page where pdf will be create and shown into the browser
                <form class="form-horizontal" method="post" action="createpdf.php">
                    <fieldset>
                        <legend class="text-center header" style="width:70%;">Contact us<span><img src="images/logo.png"></span></legend>
                        <div class="form-group">
                            <div class="col-md-8">
                               <input id="name" name="name" type="text" placeholder="Name" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-8">
                                <input id="email" name="email" type="text" placeholder="Email Address" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">

                            <div class="col-md-8">

                                <input id="phone" name="phone" type="text" placeholder="Phone" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-12 text-center">
                                <button type="submit" class="btn btn-primary btn-lg">Submit</button>
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- Contact Form - END -->
</div>
</body>
</html>

我的php是。

<强> createPDF.php来

<?php
require('WriteHTML.php');
$pdf=new PDF_HTML();
$pdf->AliasNbPages();

////add page page automatically for its true parameter

$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();
//add images or logo which you want
$pdf->Image('images/logo.png',18,13,33);
//set font style
$pdf->SetFont('Arial','B',14);
$pdf->WriteHTML('<para><h1>Click4Knowledge Web Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL and Demos</h1><br>
Website: <u>www.click4knowledge.com</u></para><br><br>How to Convert HTML to PDF with fpdf example');

//set the form of pdf

$pdf->SetFont('Arial','B',7);

//assign the form post value in a variable and pass it.

$htmlTable='<TABLE>
<TR>
<TD>Name:</TD>
<TD>'.$_POST['name'].'</TD>
</TR>
<TR>
<TD>Email:</TD>
<TD>'.$_POST['email'].'</TD>
</TR>
<TR>
<TD>Phone:</TD>
<TD>'.$_POST['phone'].'</TD>
</TR>
</TABLE>';
//Write HTML to pdf file and output that file on the web browser.
$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
$pdf->Output();
?>

修改此示例,以便能够生成您喜欢的输出。