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

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!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>
  • 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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');

?>
  • 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.

About ram

ram kumar has written 7 post in this blog.

Engaged with Web Development

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

  1. [...] using PHP. We already discuss the things in  conversion of current webpage as MS-Word document and PDF file. Similar to this conversion we going to convert a number as word(string). It  is also [...]

  2. navdeep says:

    very helpful article,thank u ram

  3. Sumanth says:

    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

  4. shashank says:

    This was sumthng nice brother

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

tawse-lozenge