World’s largest virtual agentic engineering & quality conference
Convert PHP source code to beautifully formatted PDF documents with line numbers, monospace font, and automatic page breaks — entirely in your browser, no server required.
Loading PDF Library…
A PHP to PDF converter transforms PHP source code files into professionally formatted PDF documents. This is invaluable for code reviews, documentation, archiving source code, and printing PHP files with proper formatting, line numbers, and monospace fonts that preserve code indentation and structure.
Under the hood, the tool uses pdfmake, a client-side PDF generation library loaded via CDN. Everything runs entirely in your browser, so your PHP code never leaves your device. The code is rendered in Courier monospace font with line numbers on A4 pages, complete with margins and automatic page breaks.
It helps to separate the two things people mean by "PHP to PDF." The first is turning a PHP source file into a readable document — exactly what this converter does. The second is generating a PDF programmatically from inside a running PHP application, which is handled by server-side libraries such as TCPDF, Dompdf, and FPDF. This tool focuses on the first job; the section below explains the library-based approach for developers who need the second.
Converting a PHP file to a clean PDF takes just a few seconds, and you can supply the code in whichever way suits you. Follow these steps.
The generated PDF is optimized for readability and printing:
| Setting | Value |
|---|---|
| Page Size | A4 (210 × 297 mm) |
| Font | Courier (monospace) |
| Font Size | 10pt |
| Line Height | 1.4 |
| Margins | 40pt on all sides |
| Line Numbers | Yes — left-aligned, gray |
| Page Numbers | Yes — centered in footer |
| Page Breaks | Automatic |
Turning PHP into a PDF is useful wherever code needs to leave the editor and live inside a shareable document. Below are the most common use cases.
As a tool built to make PHP code look right on the page, the converter offers several capabilities that keep the output clean and readable. The following are some of its features.
The converter on this page renders existing PHP source code as a document. If instead you want your PHP application to generate a PDF on the fly — an invoice, a report, a shipping label — you do that in server-side code with a dedicated library. The most widely used open-source options are below.
| Library | Best For | Approach |
|---|---|---|
| Dompdf | Converting existing HTML/CSS templates to PDF | HTML-to-PDF |
| TCPDF | Full control: custom layouts, barcodes, digital signatures | Canvas / API drawing |
| FPDF | Lightweight, dependency-free basic PDFs | Canvas / API drawing |
| mPDF | HTML/CSS with strong Unicode and right-to-left support | HTML-to-PDF |
| wkhtmltopdf | Pixel-accurate rendering of complex, JS-driven pages | Headless browser (external binary) |
For most projects that already have an HTML template, Dompdf is the quickest start. After installing it with Composer (composer require dompdf/dompdf), a working example is only a few lines.
<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>Invoice #1024</h1><p>Total: $250.00</p>');
$dompdf->render();
$dompdf->stream('invoice.pdf');TCPDF and FPDF instead draw the page cell by cell, which gives you precise positioning for barcodes and signatures at the cost of more code. Whichever you pick, the PDF is produced on your server — unlike this browser-based tool, which never uploads your code. To turn source files from other languages into PDFs, see our Code to PDF Converter.
Syntax highlighting is the color-coding that editors apply to code so that keywords, variables, strings, and comments each stand out. When you export PHP for a code review or documentation, highlighting makes the logic far easier to follow than a flat wall of text.
To keep everything running privately in your browser, this converter exports clean monospace output: indentation, alignment, and line numbers are preserved exactly, so the structure of your code stays readable even though individual tokens are not colored. That makes it ideal for faithful, printable records of your source.
If you specifically need colored tokens in the final PDF, generate the highlighted markup first with a library such as Prism.js or Highlight.js, then feed that styled HTML into an HTML-to-PDF library like Dompdf or mPDF. To simply clean up and re-indent messy PHP before exporting, our PHP Beautifier is a useful first step.
A common frustration is clicking a "download PDF" link on a website — for an invoice or a report — and receiving a file like invoice.php that will not open. This is not your fault, and the file is usually fine; the server just sent it with the wrong label.
When a PHP script is meant to output a PDF, it has to announce the correct MIME type by sending a Content-Type header of application/pdf before any output. If that header is missing or misconfigured, the browser treats the response as the raw PHP file and downloads it verbatim.
Quick fix (as a user): in most cases you can simply rename the downloaded file, changing its extension from .php to .pdf, and it will open normally in any PDF reader.
Proper fix (as the developer): set the headers before streaming the file, for example:
<?php
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="invoice.pdf"');
readfile('invoice.pdf');A PHP to PDF Converter takes PHP source code and generates a professionally formatted PDF document with line numbers, monospace font, and automatic page breaks — ideal for documentation, code reviews, and printing.
The tool uses pdfmake, a client-side PDF generation library. You paste, upload, or fetch PHP code, and the tool renders it into a PDF with Courier monospace font, line numbers, A4 page size, and proper margins — all in your browser.
Yes, this PHP to PDF converter is completely free to use with no registration or API keys required.
No. All conversion happens entirely in your browser using pdfmake. Your PHP code is never sent to any server, making it 100% private and secure.
Three methods: (1) paste PHP code directly into the text area, (2) upload a .php file from your device, and (3) enter a URL to a raw PHP file which is fetched client-side.
Yes. Since everything runs in the browser, file size is limited only by your device's memory. Files with thousands of lines work without issues.
Yes. The generated PDF includes line numbers alongside each line of code for easy reference during code reviews and documentation.
The PDF uses A4 paper, Courier monospace font at size 10, line height 1.4, 40pt margins on all sides, and automatic page breaks. Page numbers are included in the footer.
The tool accepts .php, .phtml, and .inc files. Other file types are rejected with an error message.
This happens when the website's server sends the file with the wrong MIME type — the PHP script did not set a Content-Type header of application/pdf before output, so the browser saved the raw .php file. In most cases you can simply rename the downloaded file's extension from .php to .pdf and it will open normally in any PDF reader.
"PDF PHP file" usually refers to one of two things: a PHP script that generates a PDF as its output (using a library like TCPDF or Dompdf), or a .php file you received because a server-generated PDF was mislabeled. It is not a special file format — a correctly configured PHP script simply produces a standard .pdf file.
In this context, PHP is the server-side language used to create the document, while PDF is the final file format. Developers use PHP libraries such as FPDF, TCPDF, Dompdf, and mPDF to build PDFs programmatically, or use a tool like this one to render PHP source code itself into a formatted PDF.
Yes. To build PDFs from within a PHP application, use a library such as Dompdf or mPDF (for HTML-to-PDF) or TCPDF or FPDF (for drawing the page directly). This browser-based tool is different: it converts your existing PHP source code into a readable, line-numbered PDF without any server-side setup.
Did you find this page helpful?
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance