2016-02-25 164 views
1

我創建了一個帶有多個字段的html表單,並且我想將該頁面導出爲pdf。 的HTML代碼如下:導出html格式爲pdf

<!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,但每當我點擊「下載爲PDF」按鈕,它只是表明我的PHP代碼文件。我有我的Web服務器運行在XAMP和HTML文件在htdocs。我需要進一步幫助將這個HTML轉換爲PDF格式。

PHP代碼我從網站上得到的是:使用FPDF您的問題

<?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); 

?> 
+0

我不確定'echo($ result);'部分。當你刪除它會發生什麼? – Maximus2012

+1

該代碼是正確的,應該創建沒有問題的PDF文件。但是,您的服務器中有關於PHP的問題。您的服務器不執行PHP文件。因此,您需要首先着重解決這個問題。正如我所說的,代碼是正確的。 – smozgur

+0

我測試了你的代碼,並且在這裏給出了一個鏈接,但是我不能保留一個鏈接,它將在SO中死掉,並且你沒有在它的生命中出現。只是爲了讓你知道 - 你的服務器可能沒有啓用PHP。也許你在安裝之後沒有重啓你的服務器或類似的東西。去你的Apache日誌,看看有什麼不對。 – smozgur

回答

0

嘗試。

樣品是在這裏如下:

的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(); 
?> 

修改此示例供您能製作自己喜歡的輸出。

+0

如何在瀏覽器上運行此操作?我需要幫助。正如我所說的,我有一臺XAMPP服務器正在運行,你能幫我解決這個問題嗎? – compcrk