How to create pdf file with native php?

by filiberto , in category: PHP General , 2 months ago

How to create pdf file with native php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by ryan.murray , 2 months ago

@filiberto 

To create a PDF file using native PHP, you can use the TCPDF library. Here are the steps to follow:

  1. Download and install TCPDF library: Visit the TCPDF official website (https://tcpdf.org/). Download the latest version of TCPDF library. Extract the downloaded zip file and move the "tcpdf" folder to your project directory.
  2. Include the TCPDF library in your PHP file: require_once('tcpdf/tcpdf.php');
  3. Create a PDF object: $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8');
  4. Set document information: $pdf->SetCreator('Your Name'); $pdf->SetAuthor('Your Name'); $pdf->SetTitle('My PDF'); $pdf->SetSubject('Creating PDF with PHP'); $pdf->SetKeywords('PDF, PHP, TCPDF');
  5. Set default font: $pdf->SetFont('Helvetica', '', 12);
  6. Add a page: $pdf->AddPage();
  7. Write content to the PDF: $pdf->Cell(0, 10, 'Hello World!', 0, 1);
  8. Output the PDF to a file or the browser: To save the PDF to a file: $pdf->Output('path/to/output.pdf', 'F'); To display the PDF in the browser: $pdf->Output('output.pdf', 'I');


That's it! You have created a PDF file using native PHP with the help of the TCPDF library.