Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Make a PDF File with PHP

This guide shows you how to create a simple PDF file from a PHP script. You will use a free helper library called Dompdf.

How-To Guides2 min read7 steps

What is PHP and a PDF library?

PHP is a language that runs on a web server. It builds web pages before they reach your visitor. A PDF is a document that looks the same on every device. Making a PDF from scratch is hard. A library is a bundle of ready-made code that does the hard part for you. Dompdf is one such library. It turns simple HTML into a PDF.

Common uses are invoices, tickets and reports.

Before you start

  • You need a hosting plan that runs PHP. Ask Hostvento support if you are not sure.
  • You need Composer. Composer is a tool that downloads PHP libraries for you. It works from the command line, so you need SSH access. SSH is a safe way to type commands on your server from far away. Ask Hostvento support whether your plan includes it.

Steps

  1. Log in to your server with SSH.
  2. Go to your website folder. For example, type the command below.
    cd ~/public_html
    This moves you into the folder where your website files live.
  3. Install Dompdf with Composer.
    composer require dompdf/dompdf
    This downloads the library into a folder named vendor.
  4. Create a new file named make-pdf.php in the same folder.
  5. Put this code inside the file.
    <?php
    require __DIR__ . '/vendor/autoload.php';
    
    use Dompdf\Dompdf;
    
    $dompdf = new Dompdf();
    $dompdf->loadHtml('<h1>Hello</h1><p>This is my first PDF.</p>');
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();
    $dompdf->stream('hello.pdf');
    The first line loads the library. The next lines give it some HTML, pick the paper size, build the PDF and send it to the browser as a download.
  6. Open yourdomain.com/make-pdf.php in your browser. Replace yourdomain.com with your own website name.
  7. Your browser should now download a file named hello.pdf.

If something goes wrong

  • A blank page often means a PHP error. Check the error log in your control panel.
  • If Composer is not found, ask Hostvento support whether it is installed.
  • Dompdf needs some PHP extensions, such as mbstring and gd. An extension is a small add-on that gives PHP extra features. Support can tell you if they are switched on.
Tip: To save the PDF as a file on the server instead of downloading it, use file_put_contents('hello.pdf', $dompdf->output()); after the render line.

Quick recap

  • PHP needs a library to make PDFs. Dompdf is an easy choice.
  • Install it with Composer over SSH.
  • Give it HTML, call render, then stream the file.
  • Ask Hostvento support if Composer or an extension is missing.