How to Convert HTML to PDF With a PHP Script

Written by david a. wells
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert HTML to PDF With a PHP Script
(Jupiterimages/Photos.com/Getty Images)

Occasionally, webmasters need to convert an HTML document to a PDF document. PDF documents are easier to print and can be downloaded and saved more easily than an HTML file. The conversion can be accomplished with a PHP script; however, writing the script yourself is a complicated and difficult prospect. Fortunately, there is a free, open-source PHP class available that can perform the conversion for you. It's called FPDF, and it's easy to use.

Skill level:
Moderately Challenging

Other People Are Reading

Instructions

  1. 1

    Download the FPDF class from fpdf.org (see resources). Unzip the package and upload the class file and the font folder to your web server using the file management interface provided by your web host.

  2. 2

    Add the FPDF class to your script. This line of code makes the FPDF class available to your script.

    <?php

    require('fpdf.php');

    ?>

  3. 3

    Create a new FPDF class variable.

    <?php

    require('fpdf.php');

    $pdfdoc=new FPDF();

    ?>

  4. 4

    Add a page to your instance of the FPDF class which is represented by the $pdfdoc variable.

    <?php

    require('fpdf.php');

    $pdfdoc=new FPDF();

    $pdfdoc->AddPage();

    ?>

  5. 5

    Set the font for the pdf file. The SetFont() function will accept the font family, style and size parameters or it can be called up without specifying any parameters. In this example the font is set to Courier, the style is italics and the size is 12 point.

    <?php

    require('fpdf.php');

    $pdfdoc=new FPDF();

    $pdfdoc->AddPage();

    $pdfdoc->SetFont('Courier','I',12);

    ?>

  6. 6

    Print a cell to the pdf file output. The contents of each cell to be printed must be defined. Each cell represents a block of text or an image from the HTML page. The width, height and text parameters are required when calling the cell function. In this example the width is 40, the height is 10 and the text to be output is "Test line".

    <?php

    require('fpdf.php');

    $pdfdoc=new FPDF();

    $pdfdoc->AddPage();

    $pdfdoc->SetFont('Courier','I',12);

    $pdfdoc->Cell(40,10,'Test line');

    ?>

  7. 7

    Call the output() function to produce the pdf document. The output() function takes a name and a destination parameter. If no parameters are provided, the PDF file will output to the browser.

    <?php

    require('fpdf.php');

    $pdfdoc=new FPDF();

    $pdfdoc->AddPage();

    $pdfdoc->SetFont('Courier','I',12);

    $pdfdoc->Cell(40,10,'Test line');

    $pdfdoc->Output();

    ?>

Don't Miss

Resources

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.