PHPackages                             madarlan/pdf-bridge-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. madarlan/pdf-bridge-php

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

madarlan/pdf-bridge-php
=======================

Universal PHP package for converting text, HTML, DOC/DOCX, XLS/XLSX, CSV to PDF using multiple libraries (TCPDF, mPDF, LibreOffice)

v1.0.0(6mo ago)20MITPHPPHP ^8.1CI failing

Since Oct 24Pushed 6mo agoCompare

[ Source](https://github.com/madarlan/pdf-bridge-php)[ Packagist](https://packagist.org/packages/madarlan/pdf-bridge-php)[ RSS](/packages/madarlan-pdf-bridge-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (10)Versions (2)Used By (0)

PHP PDF Bridge
==============

[](#php-pdf-bridge)

[![PDF Bridge Cover](https://camo.githubusercontent.com/92d228618b7a94816d49702d7a64dc31b158b272e4fc07073e142a9b0779a1c3/68747470733a2f2f692e6962622e636f2f6b564e57676b42782f6d616461726c616e2d7064662d6272696467652d7068702e706e67)](https://camo.githubusercontent.com/92d228618b7a94816d49702d7a64dc31b158b272e4fc07073e142a9b0779a1c3/68747470733a2f2f692e6962622e636f2f6b564e57676b42782f6d616461726c616e2d7064662d6272696467652d7068702e706e67)

[![Latest Version on Packagist](https://camo.githubusercontent.com/30143454430d3975e396807f6713d8583a4763c4d68fe52252b88d2375a70e7d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d616461726c616e2f7064662d6272696467652d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/madarlan/pdf-bridge-php)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/9dece405dfd849f039663b810dcbca3ae2359f512df5f9f67336f049040cf87e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d616461726c616e2f7064662d6272696467652d7068702f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/madarlan/pdf-bridge-php/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![PHP Version Require](https://camo.githubusercontent.com/9b5084d9a8bb170bc4962078b36ee5ddebd5edd05ddf8178660cfb7ce2091a47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d616461726c616e2f7064662d6272696467652d7068702e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/madarlan/pdf-bridge-php)

A powerful and universal PHP/Laravel package for converting various document formats to PDF using multiple converters ( TCPDF, mPDF, LibreOffice). Features robust validation, comprehensive logging, and support for 15+ file formats with Laravel 8-12 support.

Description
-----------

[](#description)

PHP PDF Bridge provides a unified interface for document conversion to PDF using several powerful libraries:

- **[TCPDF](https://github.com/tecnickcom/TCPDF)** - for text, HTML and CSV conversion
- **[mPDF](https://github.com/mpdf/mpdf)** - for advanced HTML and CSS processing
- **LibreOffice** (via [ncjoes/office-converter](https://github.com/ncjoes/office-converter)) - for DOC/DOCX/XLS/XLSX conversion

Supported Formats
-----------------

[](#supported-formats)

### Input formats:

[](#input-formats)

- **Text**: `.txt`, plain text files
- **HTML**: `.html`, `.htm`, HTML markup
- **CSV**: `.csv`, tabular data
- **Microsoft Word**: `.doc`, `.docx`
- **Microsoft Excel**: `.xls`, `.xlsx`
- **Microsoft PowerPoint**: `.ppt`, `.pptx`
- **OpenDocument**: `.odt`, `.ods`, `.odp`
- **Rich Text**: `.rtf`

### Output format:

[](#output-format)

- **PDF** - all conversions produce PDF files

✨ Features
----------

[](#-features)

- 🔄 **Universal conversion**: 15+ formats → PDF (Text, HTML, CSV, DOC/DOCX, XLS/XLSX, PPT/PPTX, ODT, ODS, RTF, etc.)
- ⚡ **Multiple converters**: TCPDF, mPDF, LibreOffice with intelligent auto-selection
- 🛡️ **Input validation**: File size limits, format validation, content verification
- 📊 **Comprehensive logging**: Detailed operation tracking with PSR-3 compatibility
- 🔧 **Flexible configuration**: Individual settings for each converter with environment support
- 🔍 **Converter diagnostics**: Built-in tools for checking converter availability and LibreOffice installation
- 🎯 **Smart error handling**: Detailed exception handling with validation feedback
- 🚀 **Artisan commands**: Powerful CLI tools for conversion and diagnostics
- 🏗️ **Laravel integration**: Service provider, facades, and dependency injection ready
- 🧪 **Fully tested**: Comprehensive test suite with PHPUnit
- 📐 **Modern architecture**: Interfaces, contracts, and SOLID principles
- 🎨 **PHP 8.1+ ready**: Modern syntax with match expressions and typed properties

Installation
------------

[](#installation)

### 1. Install the package

[](#1-install-the-package)

```
composer require madarlan/pdf-bridge-php
```

### 2. Publish configuration (optional)

[](#2-publish-configuration-optional)

```
php artisan vendor:publish --provider="MadArlan\PDFBridge\Laravel\PDFBridgeServiceProvider"
```

### 3. Install converters

[](#3-install-converters)

#### TCPDF (installed automatically)

[](#tcpdf-installed-automatically)

```
# Already included in the package
```

#### mPDF

[](#mpdf)

```
composer require mpdf/mpdf
```

#### LibreOffice (for DOC/DOCX/XLS/XLSX)

[](#libreoffice-for-docdocxxlsxlsx)

```
# Install LibreOffice
# Ubuntu/Debian:
sudo apt-get install libreoffice

# CentOS/RHEL:
sudo yum install libreoffice

# Windows: Download from https://www.libreoffice.org/
# macOS: brew install --cask libreoffice

# Install PHP package
composer require ncjoes/office-converter
```

Advanced Features
-----------------

[](#advanced-features)

### Conversion Parameter Configuration

[](#conversion-parameter-configuration)

```
$options = [
    'converter' => 'tcpdf',           // Force converter selection
    'format' => 'A3',                // Page format
    'orientation' => 'L',            // Orientation (P/L)
    'font_size' => 14,               // Font size
    'font_family' => 'helvetica',    // Font family
    'title' => 'Document Title',
    'author' => 'Document Author',
    'subject' => 'Document Subject',
    'keywords' => 'key, words',
    'margins' => [
        'left' => 20,
        'right' => 20,
        'top' => 30,
        'bottom' => 30,
    ],
];

$pdf = PDFBridge::convertHTML($html, null, $options);
```

### Working with CSV

[](#working-with-csv)

```
$csvContent = "Name,Age,City\nJohn,25,New York\nMary,30,Los Angeles";

$options = [
    'csv_delimiter' => ',',
    'csv_has_header' => true,
    'font_size' => 10,
];

$pdf = PDFBridge::convertCSV($csvContent, 'table.pdf', $options);
```

### Checking Converter Availability

[](#checking-converter-availability)

#### Getting Information About All Converters

[](#getting-information-about-all-converters)

```
$converters = PDFBridge::getAvailableConverters();

foreach ($converters as $name => $info) {
    if ($info['available']) {
        echo "✓ {$name}: " . implode(', ', $info['formats']) . "\n";
        if (isset($info['version'])) {
            echo "  Version: {$info['version']}\n";
        }
    } else {
        echo "✗ {$name}: {$info['error']}\n";
    }
}

// Example output:
// ✓ tcpdf: text, html, csv
// ✓ mpdf: text, html, csv
// ✓ libreoffice: doc, docx, xls, xlsx
//   Version: LibreOffice 7.4.7.2 40(Build:2)
```

#### Checking Specific Converter

[](#checking-specific-converter)

```
// Check TCPDF availability
try {
    $tcpdfConverter = new \MadArlan\PDFBridge\Converters\TCPDFConverter();
    if ($tcpdfConverter->isAvailable()) {
        echo "TCPDF is available\n";
    }
} catch (\MadArlan\PDFBridge\Exceptions\ConverterNotAvailableException $e) {
    echo "TCPDF unavailable: " . $e->getMessage() . "\n";
}

// Check mPDF availability
try {
    $mpdfConverter = new \MadArlan\PDFBridge\Converters\MPDFConverter();
    if ($mpdfConverter->isAvailable()) {
        echo "mPDF is available\n";
    }
} catch (\MadArlan\PDFBridge\Exceptions\ConverterNotAvailableException $e) {
    echo "mPDF unavailable: " . $e->getMessage() . "\n";
}
```

#### Special LibreOffice Check

[](#special-libreoffice-check)

```
use MadArlan\PDFBridge\Converters\OfficeConverter;
use MadArlan\PDFBridge\Exceptions\ConverterNotAvailableException;

// Check with automatic LibreOffice detection
try {
    $officeConverter = new OfficeConverter();

    if ($officeConverter->isAvailable()) {
        echo "✓ LibreOffice is available\n";

        // Get LibreOffice version
        $version = $officeConverter->getVersion();
        if ($version) {
            echo "  Version: {$version}\n";
        }

        // Supported formats
        $formats = $officeConverter->getSupportedFormats();
        echo "  Formats: " . implode(', ', $formats) . "\n";
    }

} catch (ConverterNotAvailableException $e) {
    echo "✗ LibreOffice unavailable: " . $e->getMessage() . "\n";

    // Possible reasons:
    // - LibreOffice not installed
    // - Incorrect LibreOffice path
    // - Missing ncjoes/office-converter package
}

// Check with specified LibreOffice path
$config = [
    'libreoffice_path' => '/usr/bin/libreoffice', // Specify correct path
    'temp_dir' => '/tmp',
    'timeout' => 120
];

try {
    $officeConverter = new OfficeConverter($config);
    echo "LibreOffice found at specified path\n";
} catch (ConverterNotAvailableException $e) {
    echo "LibreOffice not found: " . $e->getMessage() . "\n";
}

// Check via main PDFBridge class
$pdfBridge = new \MadArlan\PDFBridge\PDFBridge();
$converters = $pdfBridge->getAvailableConverters();

if ($converters['libreoffice']['available']) {
    echo "LibreOffice ready to work\n";
    echo "Version: " . ($converters['libreoffice']['version'] ?? 'unknown') . "\n";
} else {
    echo "LibreOffice problem: " . $converters['libreoffice']['error'] . "\n";
}
```

Usage
-----

[](#usage)

### Artisan Command for Quick Conversion

[](#artisan-command-for-quick-conversion)

The package includes a convenient Artisan command for quick file conversion from the command line:

```
# Basic command
php artisan pdf:convert {input} [options]

# Usage examples:

# Convert text file
php artisan pdf:convert document.txt

# Convert HTML file with output specification
php artisan pdf:convert index.html --output=result.pdf

# Convert Word document with specific converter
php artisan pdf:convert document.docx --converter=libreoffice

# Convert CSV with settings
php artisan pdf:convert data.csv --config='{"delimiter":";""encoding":"utf-8"}'

# Convert text directly (without file)
php artisan pdf:convert "Hello, World!" --type=text

# Convert HTML code
php artisan pdf:convert "TitleContent" --type=html
```

#### Available Command Options:

[](#available-command-options)

- `--output` - Output PDF file (default: input.pdf)
- `--type` - Input data type (auto|text|html|csv|doc|docx|xls|xlsx)
- `--converter` - Preferred converter (tcpdf|mpdf|libreoffice)
- `--config` - JSON configuration for converter
- `--check` - Check converter availability
- `--diagnose` - LibreOffice diagnostics
- `--list-formats` - Show supported formats
- `--list-converters` - Show available converters

#### Diagnostics and Checking Examples:

[](#diagnostics-and-checking-examples)

```
# Check all converter availability
php artisan pdf:convert --check

# LibreOffice diagnostics
php artisan pdf:convert --diagnose

# List supported formats
php artisan pdf:convert --list-formats

# List available converters
php artisan pdf:convert --list-converters
```

#### Automatic Type Detection:

[](#automatic-type-detection)

The command automatically detects input data type:

- By file extension (.txt, .html, .csv, .doc, .docx, .xls, .xlsx)
- By content (HTML tags, CSV delimiters)
- Defaults to text

#### Configuration Examples:

[](#configuration-examples)

```
# TCPDF with settings
php artisan pdf:convert document.html --converter=tcpdf --config='{"orientation":"L","format":"A4"}'

# mPDF with settings
php artisan pdf:convert document.html --converter=mpdf --config='{"margin_left":15,"margin_right":15}'

# LibreOffice with timeout
php artisan pdf:convert document.docx --converter=libreoffice --config='{"timeout":300}'
```

### In Laravel (via Facade)

[](#in-laravel-via-facade)

```
