Home  >  Article  >  Backend Development  >  How to create PDF using PHP?

How to create PDF using PHP?

王林
王林Original
2024-04-20 11:39:021022browse

How to use PHP to create PDF. Install the required libraries: PHP 7.1 or above, mPDF library. Create PDF files: instantiate mPDF objects, write HTML content, and output PDF files. Practical case: Generate user invoices, including customer information, invoice information, product list and total amount.

如何使用 PHP 创建 PDF?

Create PDF using PHP

Required tools:

  • PHP 7.1 or above
  • mPDF library

Install mPDF library:

Install mPDF through Composer:

composer require mpdf/mpdf

Create PDF file:

<?php
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \mPDF();
$mpdf->WriteHTML('<h1>Hello, PDF!</h1>');

$mpdf->Output('hello-pdf.pdf', 'D');

Practical case: Generate user invoice

<?php
require_once __DIR__ . '/vendor/autoload.php';

$data = [
    'user' => [
        'name' => 'John Doe',
        'address' => '123 Main Street',
        'city' => 'Anytown',
        'zip' => '12345'
    ],
    'invoice' => [
        'number' => 'INV-001',
        'date' => '2023-03-08',
        'items' => [
            [
                'name' => 'Item 1',
                'price' => 10,
                'quantity' => 2
            ],
            [
                'name' => 'Item 2',
                'price' => 15,
                'quantity' => 1
            ]
        ]
    ]
];

$mpdf = new \mPDF();
$mpdf->WriteHTML(render_invoice($data));

$mpdf->Output('invoice.pdf', 'D');

function render_invoice($data) {
    $html = <<<HTML
    <h1>Invoice #{$data['invoice']['number']}</h1>
    <p>Date: {$data['invoice']['date']}</p>
    <hr>

    <p><strong>Customer:</strong></p>
    <ul>
        <li>{$data['user']['name']}</li>
        <li>{$data['user']['address']}</li>
        <li>{$data['user']['city']}, {$data['user']['zip']}</li>
    </ul>

    <table border="1">
        <thead>
            <tr>
                <th>Item</th>
                <th>Price</th>
                <th>Qty</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            {foreach $data['invoice']['items'] as $item}
            <tr>
                <td>{$item['name']}</td>
                <td align="right">{$item['price']}</td>
                <td align="right">{$item['quantity']}</td>
                <td align="right">{$item['price'] * $item['quantity']}</td>
            </tr>
            {/foreach}
        </tbody>
        <tfoot>
            <tr>
                <th colspan="3" align="right">Total:</th>
                <td align="right">{$total_amount}</td>
            </tr>
        </tfoot>
    </table>
HTML;
    return $html;
}

The above is the detailed content of How to create PDF using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn