Convert (or) Save current Webpage as PDF document Using PHP

By | September 11, 2012

Here we have to see how to convert (or) current webpage as pdf file. We already discuss the thing about conversion of webpage as MS-Word document file. Similar to this we have to convert current webpage as pdf  on current  webpage itself. Most of the  websites willing to provide their document as pdf instead MS-Word because in pdf the data can’t be changed by user. From this information from pdf is unique. So most of website add a link to download as pdf. Let see how it can developed in php.

For designing and import content of webpages  we have include the coding of pdf toolkit here.Download the pdf creator html2fpdf  add extract the files and  add few files in the html2fpdf folder.

  • The download link for pdf creator toolkit is html2fpdf
  • save this file as form.php in downloaded html2fpdf  folder.

[code type = php]

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>

<body>

<fieldset style=”background-color:#6C6CFF; height:400px; padding:20px;”>
<center><form method=”post” action=”pdf.php”>
<p>Here we have to see how to convert (or) current webpage as pdf file. We already discuss the thing about conversion of webpage as MS-Word document file. Similar to this we have to convert current webpage as pdf on current webpage itself. Most of the websites willing to provide their document as pdf instead MS-Word because in pdf the data can’t be changed by user. From this information from pdf is unique. So most of website add a link to download as pdf. Let see how it can developed in php.For designing and import content of webpages we have include the coding of pdf toolkit here.Download the pdf creator html2fpdf add extract the files and add few files in the html2fpdf folder.The download link for pdf creator toolkit is html2fpdf</p>
<input type=”submit” name=”submit” />
</form>
</center>
</fieldset>

</body>
</html>

[/code]

  • The above  form is  we have to downloaded as pdf.
  • so, now we have link a form.php to fpdf.php which is pdf format  maker (or) creator.
  • Create and save the file as pdf.php in html2pdf folder itself.

[code type=php]

<?php
require(“html2fpdf.php”);

$htmlFile = “form.php”;
$buffer = file_get_contents($htmlFile);

$pdf = new HTML2FPDF(‘P’, ‘mm’, ‘Letter’);
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output(‘test.pdf’, ‘F’);

?>

[/code]

  • By executing pdf.php file we get a current webpage as pdf format in the name of test.pdf.
  • Check the foder html2fpdf there is a test.pdf fle which contain the content of form.php file.

4 thoughts on “Convert (or) Save current Webpage as PDF document Using PHP

  1. Pingback: Automatically Display (or) Convert a Number into Word(String) Using PHP | Freeze Coders

  2. Sumanth

    Hi,

    Is there any option to save the pdf in different location rather than with in the root folder. If yes please let me know.

    Thanks in Advance.
    Sumanth C N

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *