generate a pdf file in php

To generate a PDF file in PHP, you can use a library called FPDF. FPDF is a free PHP class that allows you to generate PDF files with pure PHP, without needing external libraries.

Here's an example code to help you get started with FPDF:

main.php
<?php
require('fpdf.php');

// Create a new PDF document
$pdf = new FPDF();

// Add a new page
$pdf->AddPage();

// Set the font and size
$pdf->SetFont('Arial','B',16);

// Write some text
$pdf->Cell(40,10,'Hello World!');

// Output the PDF
$pdf->Output();
?>
261 chars
19 lines

In this example, we first include the FPDF library with require('fpdf.php'). Then we create a new PDF document with $pdf = new FPDF().

We add a new page to the document with $pdf->AddPage(). Then we set the font and size with $pdf->SetFont('Arial','B',16).

We write some text to the document with $pdf->Cell(40,10,'Hello World!'). This writes the string 'Hello World!' to the PDF, with a width of 40 and a height of 10.

Finally, we output the PDF document to the browser with $pdf->Output().

Of course, this is just a basic example. You can do much more with FPDF, such as adding images, headings, tables, and more. You can find the full documentation on the FPDF website: http://www.fpdf.org/.

related categories

gistlibby LogSnag