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
- Log in to your server with SSH.
- Go to your website folder. For example, type the command below.
This moves you into the folder where your website files live.cd ~/public_html - Install Dompdf with Composer.
This downloads the library into a folder namedcomposer require dompdf/dompdfvendor. - Create a new file named
make-pdf.phpin the same folder. - Put this code inside the file.
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.<?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'); - Open
yourdomain.com/make-pdf.phpin your browser. Replaceyourdomain.comwith your own website name. - 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.