1. Go to this page and download the library: Download madarlan/pdf-bridge-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/ */
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";
}
use MadArlan\PDFBridge\Laravel\PDFBridge;
class DocumentController extends Controller
{
public function convertText()
{
$text = "Hello, World!\nThis is a test document.";
// Convert to string
$pdfContent = PDFBridge::convertText($text);
// Save to file
$filePath = PDFBridge::convertText($text, storage_path('app/document.pdf'));
return response($pdfContent)
->header('Content-Type', 'application/pdf')
->header('Content-Disposition', 'attachment; filename="document.pdf"');
}
public function convertHTML()
{
$html = '<h1>Title</h1><p>This is <strong>HTML</strong> document.</p>';
$options = [
'converter' => 'mpdf', // Force mPDF usage
'title' => 'My Document',
'author' => 'Author',
];
return PDFBridge::convertHTML($html, null, $options);
}
public function convertDocument(Request $request)
{
$file = $request->file('document');
$inputPath = $file->store('temp');
try {
$pdfPath = PDFBridge::convertFile(storage_path('app/' . $inputPath));
return response()->download($pdfPath)->deleteFileAfterSend();
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 400);
}
}
}
use MadArlan\PDFBridge\PDFBridge;
class DocumentService
{
protected PDFBridge $pdfBridge;
public function __construct(PDFBridge $pdfBridge)
{
$this->pdfBridge = $pdfBridge;
}
public function processDocument(string $content, string $type): string
{
switch ($type) {
case 'text':
return $this->pdfBridge->convertText($content);
case 'html':
return $this->pdfBridge->convertHTML($content);
case 'csv':
return $this->pdfBridge->convertCSV($content);
default:
throw new \InvalidArgumentException("Unsupported type: {$type}");
}
}
}
adArlan\PDFBridge\PDFBridge;
// Create instance with configuration
$config = [
'default_converter' => 'mpdf',
'mpdf' => [
'format' => 'A4',
'default_font_size' => 14,
],
];
$pdfBridge = new PDFBridge($config);
// Convert text
$text = "Sample text for PDF conversion";
$pdfContent = $pdfBridge->convertText($text);
file_put_contents('output.pdf', $pdfContent);
// Convert HTML
$html = '<h1>Title</h1><p>HTML content</p>';
$pdfBridge->convertHTML($html, 'output.pdf');
// Convert file
$pdfBridge->convertFile('document.docx', 'converted.pdf');
// Get available converters info
$converters = $pdfBridge->getAvailableConverters();
print_r($converters);
use MadArlan\PDFBridge\PDFBridge;
$pdfBridge = new PDFBridge();
// Simple text conversion
$text = "Hello World!\nThis is a multi-line text document.";
$pdfPath = $pdfBridge->convertText($text, 'hello.pdf');
// Return PDF as string (for download)
$pdfContent = $pdfBridge->convertText($text);
return response($pdfContent, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="document.pdf"'
]);
use MadArlan\PDFBridge\Exceptions\ConverterNotAvailableException;
use MadArlan\PDFBridge\Exceptions\ConversionException;
try {
$pdfBridge->convertFile('document.docx', 'output.pdf');
} catch (ConverterNotAvailableException $e) {
// LibreOffice not installed or not found
echo "LibreOffice is heck if file exists and is readable
if (!file_exists('document.docx')) {
echo "File does not exist.\n";
} elseif (!is_readable('document.docx')) {
echo "File is not readable.\n";
} else {
echo "File may be corrupted or contain unsupported features.\n";
}
}
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Create custom logger
$logger = new Logger('pdf-bridge');
$logger->pushHandler(new StreamHandler('pdf-conversions.log', Logger::INFO));
$pdfBridge = new PDFBridge($config, $logger);
// All operations will be logged to pdf-conversions.log
$pdfBridge->convertText('Hello World!', 'output.pdf');
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use MadArlan\PDFBridge\PDFBridge;
class GeneratePDFJob implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
private string $htmlContent,
private string $outputPath
) {}
public function handle(PDFBridge $pdfBridge): void
{
$pdfBridge->convertHTML($this->htmlContent, $this->outputPath);
// Notify user or perform additional actions
}
}
// Dispatch the job
GeneratePDFJob::dispatch($htmlContent, $outputPath);