1

I have created an html form with a number of fields and I want to export the page to pdf. The html code is as follows:

<!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>

I have got the php code to export the form to pdf from http://freehtmltopdf.com/ but whenever I click on the"Download as pdf" button it simply shows me the php code in the file. I have my webserver running on XAMP and the html file is in htdocs. I need further help in converting this html to pdf.

The php code I got from the site is:

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

?>
David van Driessche
  • 6,602
  • 2
  • 28
  • 41
compcrk
  • 411
  • 4
  • 9
  • 18
  • I am not sure about `echo($result);` part. What happens when you remove it? – Maximus2012 Feb 25 '16 at 22:28
  • 1
    The code is correct and supposed to create the PDF file without problems. However, you have a problem about PHP in your server. Your server doesn't execute PHP files. Therefore you need to focus on fixing this part first. As I said, the code is correct. – smozgur Feb 25 '16 at 22:34
  • I tested your code and also gave a link here but I can't keep a link that will be dead in SO and you didn't show up in its lifetime. Just to let you know - PHP is likely not enabled in your server. Perhaps you didn't restart your server after installation or something like that. Go to your Apache logs and see what is wrong. – smozgur Feb 25 '16 at 22:48
  • Try using FPDF on converting your problem. – Lee Balino Feb 26 '16 at 07:45

1 Answers1

0

Try using FPDF for your problem.

Sample is here below:

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>

and my php is.

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

Modify this sample for you to able to produce the output you like.

Lee Balino
  • 490
  • 2
  • 12
  • How do I run this on my browser? I need help with that. As I said, I have a XAMPP server running, can you help me on how to run this? – compcrk Feb 26 '16 at 11:11