John Davidson

How to create pdf file with native php

0 comments
Message:


There are many libraries to create pdf files with php : fpdf, tcpdf, domPdf, ...


There is also a php extension PDFlib, but it's not free.


My quetion is how to create a simple pdf file using only native php ?



My Answer:
To create a simple PDF file using native PHP, you can use the `FPDF` library which is a free and open-source library for creating PDF files. Here's a step-by-step guide on how to create a simple PDF file using `FPDF`:

1. Download the `FPDF` library from the official website: http://www.fpdf.org/

2. Extract the downloaded zip file and copy the `fpdf.php` file to your project directory.

3. Create a new PHP file and include the `fpdf.php` file at the beginning of the file:

php
require('fpdf.php');


4. Create a new instance of the `FPDF` class:

php
$pdf = new FPDF();
$pdf->AddPage();


5. Set the font and font size for the PDF file:

php
$pdf->SetFont('Arial','B',16);


6. Add content to the PDF file:

php
$pdf->Cell(40,10,'Hello World!');


7. Output the PDF file to the browser or save it to a file:

php
$pdf->Output();
// or
$pdf->Output('example.pdf','D');


8. Run the PHP file in your browser and you should see a simple PDF file with the text "Hello World!".

This is a basic example of how to create a simple PDF file using native PHP and the `FPDF` library. You can explore the `FPDF` documentation for more advanced features and customization options.

Rate this post

4 of 5 based on 3773 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us