PHP code example of kallefrombosnia / tinypdf-php

1. Go to this page and download the library: Download kallefrombosnia/tinypdf-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kallefrombosnia / tinypdf-php example snippets


use TinyPdf\TinyPdf;
use TinyPdf\TextOptions;
use TinyPdf\TextAlign;

$pdf = TinyPdf::create();

// Add a page (default US Letter: 612x792 points)
$pdf->page(function ($ctx) {
    // Add text
    $ctx->text('Hello World!', 50, 700, 24);

    // Text with options
    $ctx->text(
        'Centered text',
        50,
        650,
        16,
        new TextOptions(
            align: TextAlign::CENTER,
            width: 500,
            color: '#0066cc'
        )
    );

    // Draw a rectangle
    $ctx->rect(50, 600, 200, 100, '#ffcc00');

    // Draw a line
    $ctx->line(50, 580, 250, 580, '#000000', 2);
});

// Custom page size
$pdf->page(595, 842, function ($ctx) { // A4 size
    $ctx->text('A4 Page', 50, 700, 18);
});

// Build and save
$pdfContent = $pdf->build();
file_put_contents('output.pdf', $pdfContent);

$pdf = TinyPdf::create();

$pdf->page(function ($ctx) {
    $jpegData = file_get_contents('image.jpg');
    $ctx->image($jpegData, 50, 500, 200, 150);
});

file_put_contents('output.pdf', $pdf->build());

use TinyPdf\MarkdownConverter;

$markdown = <<<MD
# Document Title

This is a paragraph with **bold** text.

## Section Header

- List item 1
- List item 2
- List item 3

1. Numbered item
2. Another item

---

Another paragraph after a horizontal rule.
MD;

$converter = new MarkdownConverter(
    width: 612,    // US Letter width
    height: 792,   // US Letter height
    margin: 72     // 1 inch margins
);

$pdfContent = $converter->convert($markdown);
file_put_contents('document.pdf', $pdfContent);

use TinyPdf\LinkOptions;

$pdf = TinyPdf::create();

$pdf->page(function ($ctx) {
    $ctx->text('GitHub Repository', 50, 700, 12, new TextOptions(color: '#0066cc'));
    $ctx->link('https://github.com/kallefrombosnia/tinypdf-php', 50, 695, 115, 20, new LinkOptions(underline: '#0066cc'));

    $ctx->text('Check out Webflow agency', 50, 650, 12, new TextOptions(color: '#95a5a6'));
    $ctx->link('https://www.flowout.com/', 50, 645, 150, 20);
});

file_put_contents('output.pdf', $pdf->build());

use TinyPdf\TinyPdf;

$width = TinyPdf::measureText('Hello World', 24);
echo "Text width: {$width} points\n";
bash
composer 
bash
php example.php
bash
# Basic text and shapes
php examples/basic/basic.php

# Professional invoice
php examples/invoice/invoice.php

# Markdown to PDF
php examples/markdown/markdown.php

# Multi-page document
php examples/multipage/multipage.php